]>
Commit | Line | Data |
---|---|---|
3d6e1202 MT |
1 | /* SmoothWall libsmooth. |
2 | * | |
3 | * This program is distributed under the terms of the GNU General Public | |
4 | * Licence. See the file COPYING for details. | |
5 | * | |
6 | * (c) Lawrence Manning, 2001 | |
7 | * Contains library functions. | |
8 | * | |
9 | * $Id: main.c,v 1.6.2.9 2005/12/09 22:31:41 franck78 Exp $ | |
10 | * | |
11 | */ | |
12 | ||
13 | #include "libsmooth.h" | |
14 | ||
15 | extern FILE *flog; | |
16 | extern char *mylog; | |
17 | ||
18 | extern char **ctr; | |
19 | ||
20 | /* reboot(). reboots. */ | |
21 | void reboot(void) | |
22 | { | |
23 | mysystem("/etc/halt"); | |
24 | } | |
25 | ||
26 | /* stripnl(). Replaces \n with \0 */ | |
27 | void stripnl(char *s) | |
28 | { | |
29 | char *t = strchr(s, '\n'); | |
30 | if (t) *t = '\0'; | |
31 | } | |
32 | ||
33 | /* Little wrapper. */ | |
34 | int mysystem(char *command) | |
35 | { | |
36 | char mycommand[STRING_SIZE]; | |
37 | ||
38 | snprintf(mycommand, STRING_SIZE, "%s >>%s 2>>%s", command, mylog, mylog); | |
39 | fprintf(flog, "Running command: %s\n", command); | |
40 | return system(mycommand); | |
41 | } | |
42 | ||
43 | void errorbox(char *message) | |
44 | { | |
45 | newtWinMessage(ctr[TR_ERROR], ctr[TR_OK], message); | |
46 | } | |
47 | ||
48 | void statuswindow(int width, int height, char *title, char *text, ...) | |
49 | { | |
50 | newtComponent t, f; | |
51 | char *buf = NULL; | |
52 | int size = 0; | |
53 | int i = 0; | |
54 | va_list args; | |
55 | ||
56 | va_start(args, text); | |
57 | ||
58 | do { | |
59 | size += 1000; | |
60 | if (buf) free(buf); | |
61 | buf = malloc(size); | |
62 | i = vsnprintf(buf, size, text, args); | |
63 | } while (i == size); | |
64 | ||
65 | va_end(args); | |
66 | ||
67 | newtCenteredWindow(width, height, title); | |
68 | ||
69 | t = newtTextbox(1, 1, width - 2, height - 2, NEWT_TEXTBOX_WRAP); | |
70 | newtTextboxSetText(t, buf); | |
71 | f = newtForm(NULL, NULL, 0); | |
72 | ||
73 | free(buf); | |
74 | ||
75 | newtFormAddComponent(f, t); | |
76 | ||
77 | newtDrawForm(f); | |
78 | newtRefresh(); | |
79 | newtFormDestroy(f); | |
80 | } | |
81 | ||
82 | int runcommandwithstatus(char *command, char *message) | |
83 | { | |
84 | int rc; | |
85 | char title[STRING_SIZE]; | |
86 | ||
87 | sprintf (title, "%s v%s - %s", NAME, VERSION, SLOGAN); | |
88 | statuswindow(60, 4, title, message); | |
89 | rc = mysystem(command); | |
90 | newtPopWindow(); | |
91 | ||
92 | return rc; | |
93 | } | |
94 | ||
95 | int runhiddencommandwithstatus(char *command, char *message) | |
96 | { | |
97 | int rc; | |
98 | char title[STRING_SIZE]; | |
99 | char mycommand[STRING_SIZE]; | |
100 | ||
101 | sprintf (title, "%s v%s - %s", NAME, VERSION, SLOGAN); | |
102 | statuswindow(60, 4, title, message); | |
103 | snprintf(mycommand, STRING_SIZE, "%s >>%s 2>>%s", command, mylog, mylog); | |
104 | fprintf(flog, "Running command: ***** HIDDEN *****\n"); | |
105 | rc = system(mycommand); | |
106 | newtPopWindow(); | |
107 | ||
108 | return rc; | |
109 | } | |
110 | ||
111 | /* This one borrowed from redhat installer. */ | |
112 | int runcommandwithprogress(int width, int height, char *title, char *command, | |
113 | int lines, char *text, ...) | |
114 | { | |
115 | newtComponent t, f, s; | |
116 | char *buf = NULL; | |
117 | int size = 0; | |
118 | int i = 0; | |
119 | va_list args; | |
120 | int rc = 0; | |
121 | FILE *p; | |
122 | char buffer[STRING_SIZE]; | |
123 | int progress = 0; | |
124 | char mycommand[STRING_SIZE]; | |
125 | ||
126 | va_start(args, text); | |
127 | ||
128 | do { | |
129 | size += 1000; | |
130 | if (buf) free(buf); | |
131 | buf = malloc(size); | |
132 | i = vsnprintf(buf, size, text, args); | |
133 | } while (i == size); | |
134 | ||
135 | va_end(args); | |
136 | ||
137 | newtCenteredWindow(width, height, title); | |
138 | ||
139 | t = newtTextbox(1, 1, width - 2, height - 2, NEWT_TEXTBOX_WRAP); | |
140 | newtTextboxSetText(t, buf); | |
141 | f = newtForm(NULL, NULL, 0); | |
142 | ||
143 | free(buf); | |
144 | ||
145 | newtFormAddComponent(f, t); | |
146 | ||
147 | s = newtScale(1, 3, width - 2, lines); | |
148 | newtScaleSet(s, progress); | |
149 | ||
150 | newtFormAddComponent(f, s); | |
151 | ||
152 | newtDrawForm(f); | |
153 | newtRefresh(); | |
154 | ||
155 | snprintf(mycommand, STRING_SIZE, "%s 2>>%s", command, mylog); | |
156 | fprintf(flog, "Running command: %s\n", command); | |
157 | ||
158 | if (!(p = popen(command, "r"))) | |
159 | { | |
160 | rc = 1; | |
161 | goto EXIT; | |
162 | } | |
163 | setvbuf(p, NULL, _IOLBF, 255); | |
164 | ||
165 | while (fgets(buffer, STRING_SIZE, p)) | |
166 | { | |
167 | newtScaleSet(s, ++progress); | |
168 | newtRefresh(); | |
169 | fprintf(flog, "%s", buffer); | |
170 | } | |
171 | ||
172 | rc = pclose(p); | |
173 | ||
174 | EXIT: | |
175 | newtFormDestroy(f); | |
176 | newtPopWindow(); | |
177 | ||
178 | return rc; | |
179 | } | |
180 | ||
181 | int checkformodule(char *module) | |
182 | { | |
183 | FILE *file; | |
184 | char buffer[STRING_SIZE]; | |
185 | int result = 0; | |
186 | ||
187 | if (!(file = fopen("/proc/modules", "r"))) | |
188 | { | |
189 | fprintf(flog, "Unable to open /proc/modules in checkformodule()\n"); | |
190 | return 0; | |
191 | } | |
192 | ||
193 | while (fgets(buffer, STRING_SIZE, file)) | |
194 | { | |
195 | if (strncmp(buffer, module, strlen(module)) == 0) | |
196 | { | |
197 | if (buffer[strlen(module)] == ' ') | |
198 | { | |
199 | result = 1; | |
200 | goto EXIT; | |
201 | } | |
202 | } | |
203 | } | |
204 | ||
205 | EXIT: | |
206 | fclose(file); | |
207 | ||
208 | return result; | |
209 | } | |
210 | ||
211 | int _replace_string(char string[], char *from, char *to) | |
212 | { | |
213 | int fromlen = strlen(from); | |
214 | int tolen = strlen(to); | |
215 | char *start, *p1, *p2; | |
216 | for(start = string; *start != '\0'; start++) | |
217 | { | |
218 | p1 = from; | |
219 | p2 = start; | |
220 | while(*p1 != '\0') | |
221 | { | |
222 | if(*p1 != *p2) | |
223 | break; | |
224 | p1++; | |
225 | p2++; | |
226 | } | |
227 | if(*p1 == '\0') | |
228 | { | |
229 | if(fromlen != tolen) | |
230 | { | |
231 | memmove(start + tolen, start + fromlen, | |
232 | strlen(start + fromlen) + 1); | |
233 | } | |
234 | for(p1 = to; *p1 != '\0'; p1++) | |
235 | *start++ = *p1; | |
236 | return 1; | |
237 | } | |
238 | } | |
239 | return 0; | |
240 | } | |
241 | ||
242 | int replace(char filename1[], char *from, char *to) | |
243 | { | |
244 | FILE *file1, *file2; | |
245 | char filename2[1000]; | |
246 | char temp[1000]; | |
247 | int ret = 0; | |
248 | ||
249 | /* Open the source and destination files */ | |
250 | strcpy (filename2, filename1); | |
251 | strcat (filename2, ".new"); | |
252 | if (!(file1 = fopen (filename1, "r"))) return 1; | |
253 | if (!(file2 = fopen (filename2, "w"))) { | |
254 | fclose(file1); | |
255 | return -1; | |
256 | } | |
257 | ||
258 | /* Start reading in lines */ | |
259 | while (fgets (temp, 1000, file1) != NULL) { | |
260 | ||
261 | if (strlen(to) > 0) { | |
262 | /* Replace string */ | |
263 | ret = _replace_string (temp, from, to); | |
264 | ||
265 | /* Write string to new file */ | |
266 | fputs(temp, file2); | |
267 | } else { | |
268 | /* Remove string when to is NULL */ | |
269 | if (!strstr(temp, from)) | |
270 | fputs(temp, file2); | |
271 | } | |
272 | } | |
273 | ||
274 | /* Close source and destination */ | |
275 | fclose (file1); | |
276 | fclose (file2); | |
277 | ||
278 | /* Move the file */ | |
279 | rename (filename2, filename1); | |
280 | ||
281 | return (ret); | |
282 | } | |
283 | ||
284 | /* Include enabled languages */ | |
285 | #ifdef LANG_EN_ONLY | |
286 | #include "lang_en.c" | |
3d6e1202 MT |
287 | #else |
288 | #include "lang_de.c" | |
289 | #include "lang_en.c" | |
3d6e1202 | 290 | #endif |
b4e381a8 | 291 | |
3d6e1202 MT |
292 | // returns a pointer to the actual running version number of IPFire. |
293 | // Successive updates increase effective version but not VERSION ! | |
294 | char g_title[STRING_SIZE] = ""; | |
295 | char* get_version(void) { | |
296 | FILE *f_title; | |
297 | if ((f_title = fopen ("/etc/issue", "r"))) { | |
298 | fgets (g_title, STRING_SIZE, f_title); | |
299 | fclose (f_title); | |
300 | if (g_title[strlen(g_title) - 1] == '\n') g_title[strlen(g_title) - 1] = '\0'; | |
301 | } else { | |
b4e381a8 | 302 | sprintf (g_title, "%s %s - %s", NAME, VERSION, SLOGAN); |
3d6e1202 MT |
303 | } |
304 | return g_title; | |
305 | } |