]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/libsmooth/main.c
1 /* SmoothWall libsmooth.
3 * This program is distributed under the terms of the GNU General Public
4 * Licence. See the file COPYING for details.
6 * (c) Lawrence Manning, 2001
7 * Contains library functions.
10 #include "libsmooth.h"
13 #define _(x) dgettext("libsmooth", x)
20 /* stripnl(). Replaces \n with \0 */
21 void stripnl(char *s
) {
22 char *t
= strchr(s
, '\n');
28 int mysystem(const char *command
) {
29 char mycommand
[STRING_SIZE
];
31 snprintf(mycommand
, STRING_SIZE
, "%s >>%s 2>>%s", command
, mylog
, mylog
);
32 fprintf(flog
, "Running command: %s\n", command
);
34 return system(mycommand
);
37 void errorbox(char *message
) {
38 newtWinMessage(_("Error"), _("OK"), message
);
41 void statuswindow(int width
, int height
, const char *title
, const char *text
, ...) {
54 i
= vsnprintf(buf
, size
, text
, args
);
59 newtCenteredWindow(width
, height
, title
);
61 t
= newtTextbox(1, 1, width
- 2, height
- 2, NEWT_TEXTBOX_WRAP
);
62 newtTextboxSetText(t
, buf
);
63 f
= newtForm(NULL
, NULL
, 0);
67 newtFormAddComponent(f
, t
);
74 int runcommandwithstatus(const char *command
, const char* title
, const char *message
) {
75 statuswindow(60, 4, title
, message
);
77 int rc
= mysystem(command
);
83 int runhiddencommandwithstatus(const char *command
, const char* title
, const char *message
) {
84 statuswindow(60, 4, title
, message
);
86 char mycommand
[STRING_SIZE
];
87 snprintf(mycommand
, STRING_SIZE
, "%s >>%s 2>>%s", command
, mylog
, mylog
);
88 fprintf(flog
, "Running command: ***** HIDDEN *****\n");
90 int rc
= system(mycommand
);
96 /* This one borrowed from redhat installer. */
97 int runcommandwithprogress(int width
, int height
, const char *title
, const char *command
,
98 int lines
, char *text
, ...) {
99 newtComponent t
, f
, s
;
106 char buffer
[STRING_SIZE
];
108 char mycommand
[STRING_SIZE
];
110 va_start(args
, text
);
116 i
= vsnprintf(buf
, size
, text
, args
);
121 newtCenteredWindow(width
, height
, title
);
123 t
= newtTextbox(1, 1, width
- 2, height
- 2, NEWT_TEXTBOX_WRAP
);
124 newtTextboxSetText(t
, buf
);
125 f
= newtForm(NULL
, NULL
, 0);
129 newtFormAddComponent(f
, t
);
131 s
= newtScale(1, 3, width
- 2, lines
);
132 newtScaleSet(s
, progress
);
134 newtFormAddComponent(f
, s
);
139 snprintf(mycommand
, STRING_SIZE
, "%s 2>>%s", command
, mylog
);
140 fprintf(flog
, "Running command: %s\n", command
);
142 if (!(p
= popen(command
, "r")))
147 setvbuf(p
, NULL
, _IOLBF
, 255);
149 while (fgets(buffer
, STRING_SIZE
, p
))
151 newtScaleSet(s
, ++progress
);
153 fprintf(flog
, "%s", buffer
);
165 int checkformodule(const char *module
) {
167 char buffer
[STRING_SIZE
];
170 if (!(file
= fopen("/proc/modules", "r")))
172 fprintf(flog
, "Unable to open /proc/modules in checkformodule()\n");
176 while (fgets(buffer
, STRING_SIZE
, file
))
178 if (strncmp(buffer
, module
, strlen(module
)) == 0)
180 if (buffer
[strlen(module
)] == ' ')
194 int _replace_string(char string
[], char *from
, char *to
)
196 int fromlen
= strlen(from
);
197 int tolen
= strlen(to
);
198 char *start
, *p1
, *p2
;
199 for(start
= string
; *start
!= '\0'; start
++)
214 memmove(start
+ tolen
, start
+ fromlen
,
215 strlen(start
+ fromlen
) + 1);
217 for(p1
= to
; *p1
!= '\0'; p1
++)
225 int replace(char filename1
[], char *from
, char *to
) {
227 char filename2
[1000];
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"))) {
240 /* Start reading in lines */
241 while (fgets (temp
, 1000, file1
) != NULL
) {
242 if (strlen(to
) > 0) {
244 ret
= _replace_string (temp
, from
, to
);
246 /* Write string to new file */
249 /* Remove string when to is NULL */
250 if (!strstr(temp
, from
))
255 /* Close source and destination */
260 rename (filename2
, filename1
);
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) {
270 if ((f_title
= fopen ("/etc/issue", "r"))) {
271 fgets (g_title
, STRING_SIZE
, f_title
);
273 if (g_title
[strlen(g_title
) - 1] == '\n') g_title
[strlen(g_title
) - 1] = '\0';