]> git.ipfire.org Git - ipfire-2.x.git/blob - src/libsmooth/main.c
Merge branch 'master' of ssh://git.ipfire.org/pub/git/ipfire-2.x into install-raid
[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 /* This one borrowed from redhat installer. */
94 int runcommandwithprogress(int width, int height, const char *title, const char *command,
95 int lines, char *text, ...) {
96 newtComponent t, f, s;
97 char *buf = NULL;
98 int size = 0;
99 int i = 0;
100 va_list args;
101 int rc = 0;
102 FILE *p;
103 char buffer[STRING_SIZE];
104 int progress = 0;
105 char mycommand[STRING_SIZE];
106
107 va_start(args, text);
108
109 do {
110 size += 1000;
111 if (buf) free(buf);
112 buf = malloc(size);
113 i = vsnprintf(buf, size, text, args);
114 } while (i == size);
115
116 va_end(args);
117
118 newtCenteredWindow(width, height, title);
119
120 t = newtTextbox(1, 1, width - 2, height - 2, NEWT_TEXTBOX_WRAP);
121 newtTextboxSetText(t, buf);
122 f = newtForm(NULL, NULL, 0);
123
124 free(buf);
125
126 newtFormAddComponent(f, t);
127
128 s = newtScale(1, 3, width - 2, lines);
129 newtScaleSet(s, progress);
130
131 newtFormAddComponent(f, s);
132
133 newtDrawForm(f);
134 newtRefresh();
135
136 if (!(p = popen(command, "r")))
137 {
138 rc = 1;
139 goto EXIT;
140 }
141 setvbuf(p, NULL, _IOLBF, 255);
142
143 while (fgets(buffer, STRING_SIZE, p)) {
144 newtScaleSet(s, ++progress);
145 newtRefresh();
146 }
147
148 rc = pclose(p);
149
150 EXIT:
151 newtFormDestroy(f);
152 newtPopWindow();
153
154 return rc;
155 }
156
157 int checkformodule(const char *module) {
158 FILE *file;
159 char buffer[STRING_SIZE];
160 int result = 0;
161
162 if (!(file = fopen("/proc/modules", "r")))
163 {
164 fprintf(stderr, "Unable to open /proc/modules in checkformodule()\n");
165 return 0;
166 }
167
168 while (fgets(buffer, STRING_SIZE, file))
169 {
170 if (strncmp(buffer, module, strlen(module)) == 0)
171 {
172 if (buffer[strlen(module)] == ' ')
173 {
174 result = 1;
175 goto EXIT;
176 }
177 }
178 }
179
180 EXIT:
181 fclose(file);
182
183 return result;
184 }
185
186 int _replace_string(char string[], char *from, char *to)
187 {
188 int fromlen = strlen(from);
189 int tolen = strlen(to);
190 char *start, *p1, *p2;
191 for(start = string; *start != '\0'; start++)
192 {
193 p1 = from;
194 p2 = start;
195 while(*p1 != '\0')
196 {
197 if(*p1 != *p2)
198 break;
199 p1++;
200 p2++;
201 }
202 if(*p1 == '\0')
203 {
204 if(fromlen != tolen)
205 {
206 memmove(start + tolen, start + fromlen,
207 strlen(start + fromlen) + 1);
208 }
209 for(p1 = to; *p1 != '\0'; p1++)
210 *start++ = *p1;
211 return 1;
212 }
213 }
214 return 0;
215 }
216
217 int replace(char filename1[], char *from, char *to) {
218 FILE *file1, *file2;
219 char filename2[1000];
220 char temp[1000];
221 int ret = 0;
222
223 /* Open the source and destination files */
224 strcpy (filename2, filename1);
225 strcat (filename2, ".new");
226 if (!(file1 = fopen (filename1, "r"))) return 1;
227 if (!(file2 = fopen (filename2, "w"))) {
228 fclose(file1);
229 return -1;
230 }
231
232 /* Start reading in lines */
233 while (fgets (temp, 1000, file1) != NULL) {
234 if (strlen(to) > 0) {
235 /* Replace string */
236 ret = _replace_string (temp, from, to);
237
238 /* Write string to new file */
239 fputs(temp, file2);
240 } else {
241 /* Remove string when to is NULL */
242 if (!strstr(temp, from))
243 fputs(temp, file2);
244 }
245 }
246
247 /* Close source and destination */
248 fclose (file1);
249 fclose (file2);
250
251 /* Move the file */
252 rename (filename2, filename1);
253
254 return (ret);
255 }
256
257 // returns a pointer to the actual running version number of IPFire.
258 // Successive updates increase effective version but not VERSION !
259 char g_title[STRING_SIZE] = "";
260 char* get_version(void) {
261 FILE *f_title;
262 if ((f_title = fopen ("/etc/issue", "r"))) {
263 fgets (g_title, STRING_SIZE, f_title);
264 fclose (f_title);
265 if (g_title[strlen(g_title) - 1] == '\n') g_title[strlen(g_title) - 1] = '\0';
266 }
267 return g_title;
268 }