]> git.ipfire.org Git - ipfire-2.x.git/blob - src/libsmooth/main.c
Merge branch 'next' of ssh://git.ipfire.org/pub/git/ipfire-2.x into seventeen
[ipfire-2.x.git] / src / libsmooth / main.c
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
10 #include "libsmooth.h"
11
12 #include <libintl.h>
13 #define _(x) dgettext("libsmooth", x)
14
15 /* stripnl(). Replaces \n with \0 */
16 void stripnl(char *s) {
17 char *t = strchr(s, '\n');
18 if (t)
19 *t = '\0';
20 }
21
22 /* Little wrapper. */
23 int mysystem(const char* output, const char *command) {
24 char mycommand[STRING_SIZE];
25
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);
34
35 return system(mycommand);
36 }
37
38 void errorbox(char *message) {
39 newtWinMessage(_("Error"), _("OK"), message);
40 }
41
42 void statuswindow(int width, int height, const char *title, const char *text, ...) {
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
75 int runcommandwithstatus(const char *command, const char* title, const char *message, const char* output) {
76 statuswindow(60, 4, title, message);
77
78 int rc = mysystem(output, command);
79 newtPopWindow();
80
81 return rc;
82 }
83
84 int runhiddencommandwithstatus(const char *command, const char* title, const char *message, const char* output) {
85 statuswindow(60, 4, title, message);
86
87 int rc = mysystem(output, command);
88 newtPopWindow();
89
90 return rc;
91 }
92
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
103 /* This one borrowed from redhat installer. */
104 int runcommandwithprogress(int width, int height, const char *title, const char *command,
105 int lines, char *text, ...) {
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();
145
146 if (!(p = popen(command, "r")))
147 {
148 rc = 1;
149 goto EXIT;
150 }
151 setvbuf(p, NULL, _IOLBF, 255);
152
153 while (fgets(buffer, STRING_SIZE, p)) {
154 newtScaleSet(s, ++progress);
155 newtRefresh();
156 }
157
158 rc = pclose(p);
159
160 EXIT:
161 newtFormDestroy(f);
162 newtPopWindow();
163
164 return rc;
165 }
166
167 int checkformodule(const char *module) {
168 FILE *file;
169 char buffer[STRING_SIZE];
170 int result = 0;
171
172 if (!(file = fopen("/proc/modules", "r")))
173 {
174 fprintf(stderr, "Unable to open /proc/modules in checkformodule()\n");
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
227 int replace(char filename1[], char *from, char *to) {
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) {
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
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';
276 }
277 return g_title;
278 }