]>
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 /* 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
;
103 char buffer
[STRING_SIZE
];
105 char mycommand
[STRING_SIZE
];
107 va_start(args
, text
);
113 i
= vsnprintf(buf
, size
, text
, args
);
118 newtCenteredWindow(width
, height
, title
);
120 t
= newtTextbox(1, 1, width
- 2, height
- 2, NEWT_TEXTBOX_WRAP
);
121 newtTextboxSetText(t
, buf
);
122 f
= newtForm(NULL
, NULL
, 0);
126 newtFormAddComponent(f
, t
);
128 s
= newtScale(1, 3, width
- 2, lines
);
129 newtScaleSet(s
, progress
);
131 newtFormAddComponent(f
, s
);
136 if (!(p
= popen(command
, "r")))
141 setvbuf(p
, NULL
, _IOLBF
, 255);
143 while (fgets(buffer
, STRING_SIZE
, p
)) {
144 newtScaleSet(s
, ++progress
);
157 int checkformodule(const char *module
) {
159 char buffer
[STRING_SIZE
];
162 if (!(file
= fopen("/proc/modules", "r")))
164 fprintf(stderr
, "Unable to open /proc/modules in checkformodule()\n");
168 while (fgets(buffer
, STRING_SIZE
, file
))
170 if (strncmp(buffer
, module
, strlen(module
)) == 0)
172 if (buffer
[strlen(module
)] == ' ')
186 int _replace_string(char string
[], char *from
, char *to
)
188 int fromlen
= strlen(from
);
189 int tolen
= strlen(to
);
190 char *start
, *p1
, *p2
;
191 for(start
= string
; *start
!= '\0'; start
++)
206 memmove(start
+ tolen
, start
+ fromlen
,
207 strlen(start
+ fromlen
) + 1);
209 for(p1
= to
; *p1
!= '\0'; p1
++)
217 int replace(char filename1
[], char *from
, char *to
) {
219 char filename2
[1000];
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"))) {
232 /* Start reading in lines */
233 while (fgets (temp
, 1000, file1
) != NULL
) {
234 if (strlen(to
) > 0) {
236 ret
= _replace_string (temp
, from
, to
);
238 /* Write string to new file */
241 /* Remove string when to is NULL */
242 if (!strstr(temp
, from
))
247 /* Close source and destination */
252 rename (filename2
, filename1
);
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) {
262 if ((f_title
= fopen ("/etc/issue", "r"))) {
263 fgets (g_title
, STRING_SIZE
, f_title
);
265 if (g_title
[strlen(g_title
) - 1] == '\n') g_title
[strlen(g_title
) - 1] = '\0';