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