]>
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)
15 /* stripnl(). Replaces \n with \0 */
16 void stripnl(char *s
) {
17 char *t
= strchr(s
, '\n');
23 int mysystem(const char* output
, const char *command
) {
24 char mycommand
[STRING_SIZE
];
29 snprintf(mycommand
, sizeof(mycommand
), "%s >>%s 2>&1", command
, output
);
31 FILE* f
= fopen(output
, "w+");
32 fprintf(f
, "Running command: %s\n", command
);
35 return system(mycommand
);
38 void errorbox(char *message
) {
39 newtWinMessage(_("Error"), _("OK"), message
);
42 void statuswindow(int width
, int height
, const char *title
, const char *text
, ...) {
55 i
= vsnprintf(buf
, size
, text
, args
);
60 newtCenteredWindow(width
, height
, title
);
62 t
= newtTextbox(1, 1, width
- 2, height
- 2, NEWT_TEXTBOX_WRAP
);
63 newtTextboxSetText(t
, buf
);
64 f
= newtForm(NULL
, NULL
, 0);
68 newtFormAddComponent(f
, t
);
75 int runcommandwithstatus(const char *command
, const char* title
, const char *message
, const char* output
) {
76 statuswindow(60, 4, title
, message
);
78 int rc
= mysystem(output
, command
);
84 int runhiddencommandwithstatus(const char *command
, const char* title
, const char *message
, const char* output
) {
85 statuswindow(60, 4, title
, message
);
87 int rc
= mysystem(output
, command
);
93 int splashWindow(const char* title
, const char* message
, unsigned int timeout
) {
94 statuswindow(60, 4, title
, message
);
96 // Wait so the user can read this message
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
;
113 char buffer
[STRING_SIZE
];
115 char mycommand
[STRING_SIZE
];
117 va_start(args
, text
);
123 i
= vsnprintf(buf
, size
, text
, args
);
128 newtCenteredWindow(width
, height
, title
);
130 t
= newtTextbox(1, 1, width
- 2, height
- 2, NEWT_TEXTBOX_WRAP
);
131 newtTextboxSetText(t
, buf
);
132 f
= newtForm(NULL
, NULL
, 0);
136 newtFormAddComponent(f
, t
);
138 s
= newtScale(1, 3, width
- 2, lines
);
139 newtScaleSet(s
, progress
);
141 newtFormAddComponent(f
, s
);
146 if (!(p
= popen(command
, "r")))
151 setvbuf(p
, NULL
, _IOLBF
, 255);
153 while (fgets(buffer
, STRING_SIZE
, p
)) {
154 newtScaleSet(s
, ++progress
);
167 int checkformodule(const char *module
) {
169 char buffer
[STRING_SIZE
];
172 if (!(file
= fopen("/proc/modules", "r")))
174 fprintf(stderr
, "Unable to open /proc/modules in checkformodule()\n");
178 while (fgets(buffer
, STRING_SIZE
, file
))
180 if (strncmp(buffer
, module
, strlen(module
)) == 0)
182 if (buffer
[strlen(module
)] == ' ')
196 int _replace_string(char string
[], char *from
, char *to
)
198 int fromlen
= strlen(from
);
199 int tolen
= strlen(to
);
200 char *start
, *p1
, *p2
;
201 for(start
= string
; *start
!= '\0'; start
++)
216 memmove(start
+ tolen
, start
+ fromlen
,
217 strlen(start
+ fromlen
) + 1);
219 for(p1
= to
; *p1
!= '\0'; p1
++)
227 int replace(char filename1
[], char *from
, char *to
) {
229 char filename2
[1000];
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"))) {
242 /* Start reading in lines */
243 while (fgets (temp
, 1000, file1
) != NULL
) {
244 if (strlen(to
) > 0) {
246 ret
= _replace_string (temp
, from
, to
);
248 /* Write string to new file */
251 /* Remove string when to is NULL */
252 if (!strstr(temp
, from
))
257 /* Close source and destination */
262 rename (filename2
, filename1
);
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) {
272 if ((f_title
= fopen ("/etc/issue", "r"))) {
273 fgets (g_title
, STRING_SIZE
, f_title
);
275 if (g_title
[strlen(g_title
) - 1] == '\n') g_title
[strlen(g_title
) - 1] = '\0';