]>
Commit | Line | Data |
---|---|---|
4d59bff9 GG |
1 | /* |
2 | * os_win32/syslogevt.c | |
3 | * | |
4 | * Home page of code is: http://smartmontools.sourceforge.net | |
5 | * | |
6 | * Copyright (C) 2004-6 Christian Franke <smartmontools-support@lists.sourceforge.net> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2, or (at your option) | |
11 | * any later version. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * (for example COPYING); if not, write to the Free | |
15 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
16 | * | |
17 | */ | |
18 | ||
19 | static char rcsid[] = "$Id: syslogevt.c,v 1.4 2006/04/12 14:54:29 ballen4705 Exp $"; | |
20 | ||
21 | #include <stdio.h> | |
22 | #include <string.h> | |
23 | #include <process.h> | |
24 | ||
25 | #define WIN32_LEAN_AND_MEAN | |
26 | #include <windows.h> | |
27 | ||
28 | #ifdef _DEBUG | |
29 | #include "syslogevt.h" | |
30 | #endif | |
31 | ||
32 | ||
33 | static int usage() | |
34 | { | |
35 | puts( | |
36 | "syslogevt $Revision: 1.4 $ Copyright (C) 2004-6 Christian Franke\n" | |
37 | "Home page is http://smartmontools.sourceforge.net/\n" | |
38 | "\n" | |
39 | "Usage: syslogevt [-ru] name [ident ...]\n" | |
40 | "\n" | |
41 | "Creates registry files \"name-r.reg\" and \"name-u.reg\" to (un)register\n" | |
42 | "this program as an event message file for message source(s) \"ident\".\n" | |
43 | "If \"ident\" is ommited, \"name\" is used. Options:\n" | |
44 | "\n" | |
45 | " -r run \"regedit name-r.reg\" after creating files\n" | |
46 | " -u run \"regedit name-u.reg\" after creating files\n" | |
47 | "\n" | |
48 | "Examples:\n" | |
49 | "\n" | |
50 | "syslogevt smartd (Create smartd-r.reg and smartd-u.reg)\n" | |
51 | "regedit smartd-r.reg (Register syslogevt.exe for smartd messages)\n" | |
52 | "\n" | |
53 | "syslogevt -r smartd (Same as above in one step)\n" | |
54 | "\n" | |
55 | "regedit smartd-u.reg (Undo the registration)\n" | |
56 | "\n" | |
57 | "CAUTION: A registry entry of an existing event source with the same \"ident\"\n" | |
58 | " will be overwritten by regedit without notice." | |
59 | ); | |
60 | return 1; | |
61 | } | |
62 | ||
63 | main(int argc, char ** argv) | |
64 | { | |
65 | int regedit, a1, ai; | |
66 | char name1[30+1], name2[30+1], mypath[MAX_PATH+1]; | |
67 | const char * ident; | |
68 | FILE * f1, * f2; | |
69 | ||
70 | #ifdef _DEBUG | |
71 | if (!(MSG_SYSLOG == 0 && MSG_SYSLOG_01 == 1 && MSG_SYSLOG_10 == 10)) { | |
72 | puts("Internal error: MSG_SYSLOG_n != n"); return 1; | |
73 | } | |
74 | #endif | |
75 | ||
76 | if (argc < 2) | |
77 | return usage(); | |
78 | ||
79 | a1 = 1; | |
80 | regedit = 0; | |
81 | if (!strcmp(argv[a1], "-r")) { | |
82 | regedit = 1; a1++; | |
83 | } | |
84 | else if (!strcmp(argv[a1], "-u")) { | |
85 | regedit = -1; a1++; | |
86 | } | |
87 | ||
88 | for (ai = a1; ai < argc; ai++) { | |
89 | ident = argv[ai]; | |
90 | if (!(ident[0] && strlen(ident) < sizeof(name1)-10 | |
91 | && strcspn(ident, "-.:/\\") == strlen(ident) )) { | |
92 | return usage(); | |
93 | } | |
94 | } | |
95 | ||
96 | if (!GetModuleFileName(NULL, mypath, sizeof(mypath)-1)) { | |
97 | fputs("GetModuleFileName failed\n", stderr); | |
98 | return 1; | |
99 | } | |
100 | ||
101 | ident = argv[a1]; | |
102 | strcpy(name1, ident); strcat(name1, "-r.reg"); | |
103 | strcpy(name2, ident); strcat(name2, "-u.reg"); | |
104 | ||
105 | if (!(f1 = fopen(name1, "w"))) { | |
106 | perror(name1); return 1; | |
107 | } | |
108 | if (!(f2 = fopen(name2, "w"))) { | |
109 | perror(name2); unlink(name1); return 1; | |
110 | } | |
111 | ||
112 | fputs("REGEDIT4\n\n", f1); | |
113 | fputs("REGEDIT4\n\n", f2); | |
114 | ||
115 | for (ai = (argc > a1+1 ? a1+1 : a1); ai < argc; ai++) { | |
116 | int i; | |
117 | ident = argv[ai]; | |
118 | fputs("[HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Eventlog\\Application\\", f1); | |
119 | fputs(ident, f1); fputs("]\n\"EventMessageFile\"=\"", f1); | |
120 | for (i = 0; mypath[i]; i++) { | |
121 | if (mypath[i] == '\\') | |
122 | fputc('\\', f1); | |
123 | fputc(mypath[i], f1); | |
124 | } | |
125 | fputs("\"\n\"TypesSupported\"=dword:00000007\n\n", f1); | |
126 | ||
127 | fputs("[-HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Eventlog\\Application\\", f2); | |
128 | fputs(ident, f2); fputs("]\n\n", f2); | |
129 | } | |
130 | ||
131 | fclose(f1); | |
132 | fclose(f2); | |
133 | ||
134 | if (GetVersion() & 0x80000000) { | |
135 | puts("Warning: Event log not supported on Win9x/ME\n"); | |
136 | if (regedit) | |
137 | return 1; | |
138 | } | |
139 | ||
140 | if (regedit) { | |
141 | if (spawnlp(P_WAIT, "regedit", "regedit", (regedit > 0 ? name1 : name2), (const char *)0) == -1) { | |
142 | fputs("regedit: cannot execute\n", stderr); | |
143 | return 1; | |
144 | } | |
145 | } | |
146 | else { | |
147 | fputs("Files generated. Use\n\n regedit ", stdout); | |
148 | puts(name1); | |
149 | fputs("\nto register event message file, and\n\n regedit ", stdout); | |
150 | puts(name2); | |
151 | fputs("\nto remove registration later.\n\n" | |
152 | "Do not remove this program when registered.\n", stdout); | |
153 | } | |
154 | ||
155 | return 0; | |
156 | } |