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