]> git.ipfire.org Git - ipfire-2.x.git/blame - src/libsmooth/main.c
libsmooth+install: Fix output redirection
[ipfire-2.x.git] / src / libsmooth / main.c
CommitLineData
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 16void 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 23int 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
38void errorbox(char *message) {
39 newtWinMessage(_("Error"), _("OK"), message);
3d6e1202
MT
40}
41
765b17bc 42void 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 75int 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 84int 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
93/* This one borrowed from redhat installer. */
765b17bc
MT
94int runcommandwithprogress(int width, int height, const char *title, const char *command,
95 int lines, char *text, ...) {
3d6e1202
MT
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();
46b56e20 135
3d6e1202
MT
136 if (!(p = popen(command, "r")))
137 {
138 rc = 1;
139 goto EXIT;
140 }
141 setvbuf(p, NULL, _IOLBF, 255);
142
46b56e20 143 while (fgets(buffer, STRING_SIZE, p)) {
3d6e1202
MT
144 newtScaleSet(s, ++progress);
145 newtRefresh();
3d6e1202 146 }
46b56e20 147
3d6e1202
MT
148 rc = pclose(p);
149
150EXIT:
151 newtFormDestroy(f);
152 newtPopWindow();
153
154 return rc;
155}
156
765b17bc 157int checkformodule(const char *module) {
3d6e1202
MT
158 FILE *file;
159 char buffer[STRING_SIZE];
160 int result = 0;
765b17bc 161
3d6e1202
MT
162 if (!(file = fopen("/proc/modules", "r")))
163 {
46b56e20 164 fprintf(stderr, "Unable to open /proc/modules in checkformodule()\n");
3d6e1202
MT
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
180EXIT:
181 fclose(file);
182
183 return result;
184}
185
186int _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
765b17bc 217int replace(char filename1[], char *from, char *to) {
3d6e1202
MT
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) {
3d6e1202
MT
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
3d6e1202
MT
257// returns a pointer to the actual running version number of IPFire.
258// Successive updates increase effective version but not VERSION !
259char g_title[STRING_SIZE] = "";
260char* 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';
3d6e1202
MT
266 }
267 return g_title;
268}