diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2003-12-26 02:19:34 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2003-12-26 02:19:34 +0000 |
commit | ab8215431f43d132cbb0986dec6fbd542350f9d9 (patch) | |
tree | c2ae418ab2b1c66857ee5eb00e7b8a3e9ef2020a /procps/sysctl.c | |
parent | 3b1a74467a0909a62f26f6f27ac63ea922939c4d (diff) | |
download | busybox-w32-ab8215431f43d132cbb0986dec6fbd542350f9d9.tar.gz busybox-w32-ab8215431f43d132cbb0986dec6fbd542350f9d9.tar.bz2 busybox-w32-ab8215431f43d132cbb0986dec6fbd542350f9d9.zip |
Vodz last_patch120, more busybox integration, saves 330 Bytes. Tested.
Diffstat (limited to 'procps/sysctl.c')
-rw-r--r-- | procps/sysctl.c | 110 |
1 files changed, 45 insertions, 65 deletions
diff --git a/procps/sysctl.c b/procps/sysctl.c index f1799af00..ee883e163 100644 --- a/procps/sysctl.c +++ b/procps/sysctl.c | |||
@@ -34,7 +34,6 @@ | |||
34 | /* | 34 | /* |
35 | * Function Prototypes | 35 | * Function Prototypes |
36 | */ | 36 | */ |
37 | static int sysctl_usage(void); | ||
38 | static int sysctl_read_setting(const char *setting, int output); | 37 | static int sysctl_read_setting(const char *setting, int output); |
39 | static int sysctl_write_setting(const char *setting, int output); | 38 | static int sysctl_write_setting(const char *setting, int output); |
40 | static int sysctl_preload_file(const char *filename, int output); | 39 | static int sysctl_preload_file(const char *filename, int output); |
@@ -43,28 +42,33 @@ static int sysctl_display_all(const char *path, int output, int show_table); | |||
43 | /* | 42 | /* |
44 | * Globals... | 43 | * Globals... |
45 | */ | 44 | */ |
46 | static const char *PROC_PATH = "/proc/sys/"; | 45 | static const char PROC_PATH[] = "/proc/sys/"; |
47 | static const char *DEFAULT_PRELOAD = "/etc/sysctl.conf"; | 46 | static const char DEFAULT_PRELOAD[] = "/etc/sysctl.conf"; |
48 | 47 | ||
49 | /* error messages */ | 48 | /* error messages */ |
50 | static const char *ERR_UNKNOWN_PARAMETER = "error: Unknown parameter '%s'\n"; | 49 | static const char ERR_UNKNOWN_PARAMETER[] = "error: Unknown parameter '%s'\n"; |
51 | static const char *ERR_MALFORMED_SETTING = "error: Malformed setting '%s'\n"; | 50 | static const char ERR_MALFORMED_SETTING[] = "error: Malformed setting '%s'\n"; |
52 | static const char *ERR_NO_EQUALS = | 51 | static const char ERR_NO_EQUALS[] = |
53 | "error: '%s' must be of the form name=value\n"; | 52 | "error: '%s' must be of the form name=value\n"; |
54 | static const char *ERR_INVALID_KEY = "error: '%s' is an unknown key\n"; | 53 | static const char ERR_INVALID_KEY[] = "error: '%s' is an unknown key\n"; |
55 | static const char *ERR_UNKNOWN_WRITING = | 54 | static const char ERR_UNKNOWN_WRITING[] = |
56 | "error: unknown error %d setting key '%s'\n"; | 55 | "error: unknown error %d setting key '%s'\n"; |
57 | static const char *ERR_UNKNOWN_READING = | 56 | static const char ERR_UNKNOWN_READING[] = |
58 | "error: unknown error %d reading key '%s'\n"; | 57 | "error: unknown error %d reading key '%s'\n"; |
59 | static const char *ERR_PERMISSION_DENIED = | 58 | static const char ERR_PERMISSION_DENIED[] = |
60 | "error: permission denied on key '%s'\n"; | 59 | "error: permission denied on key '%s'\n"; |
61 | static const char *ERR_OPENING_DIR = "error: unable to open directory '%s'\n"; | 60 | static const char ERR_OPENING_DIR[] = "error: unable to open directory '%s'\n"; |
62 | static const char *ERR_PRELOAD_FILE = | 61 | static const char ERR_PRELOAD_FILE[] = |
63 | "error: unable to open preload file '%s'\n"; | 62 | "error: unable to open preload file '%s'\n"; |
64 | static const char *WARN_BAD_LINE = | 63 | static const char WARN_BAD_LINE[] = |
65 | "warning: %s(%d): invalid syntax, continuing...\n"; | 64 | "warning: %s(%d): invalid syntax, continuing...\n"; |
66 | 65 | ||
67 | 66 | ||
67 | static void dwrite_str(int fd, const char *buf) | ||
68 | { | ||
69 | write(fd, buf, strlen(buf)); | ||
70 | } | ||
71 | |||
68 | /* | 72 | /* |
69 | * sysctl_main()... | 73 | * sysctl_main()... |
70 | */ | 74 | */ |
@@ -76,7 +80,7 @@ int sysctl_main(int argc, char **argv) | |||
76 | int switches_allowed = 1; | 80 | int switches_allowed = 1; |
77 | 81 | ||
78 | if (argc < 2) | 82 | if (argc < 2) |
79 | return sysctl_usage(); | 83 | bb_show_usage(); |
80 | 84 | ||
81 | argv++; | 85 | argv++; |
82 | 86 | ||
@@ -103,10 +107,10 @@ int sysctl_main(int argc, char **argv) | |||
103 | ((*argv)[1] == 'a') ? 0 : 1); | 107 | ((*argv)[1] == 'a') ? 0 : 1); |
104 | case 'h': | 108 | case 'h': |
105 | case '?': | 109 | case '?': |
106 | return sysctl_usage(); | 110 | bb_show_usage(); |
107 | default: | 111 | default: |
108 | bb_error_msg(ERR_UNKNOWN_PARAMETER, *argv); | 112 | bb_error_msg(ERR_UNKNOWN_PARAMETER, *argv); |
109 | return sysctl_usage(); | 113 | bb_show_usage(); |
110 | } | 114 | } |
111 | } else { | 115 | } else { |
112 | switches_allowed = 0; | 116 | switches_allowed = 0; |
@@ -119,14 +123,6 @@ int sysctl_main(int argc, char **argv) | |||
119 | return retval; | 123 | return retval; |
120 | } /* end sysctl_main() */ | 124 | } /* end sysctl_main() */ |
121 | 125 | ||
122 | /* | ||
123 | * Display the sysctl_usage format | ||
124 | */ | ||
125 | int sysctl_usage() | ||
126 | { | ||
127 | bb_show_usage(); | ||
128 | return -1; | ||
129 | } /* end sysctl_usage() */ | ||
130 | 126 | ||
131 | 127 | ||
132 | /* | 128 | /* |
@@ -134,17 +130,18 @@ int sysctl_usage() | |||
134 | * preload the sysctl's from a conf file | 130 | * preload the sysctl's from a conf file |
135 | * - we parse the file and then reform it (strip out whitespace) | 131 | * - we parse the file and then reform it (strip out whitespace) |
136 | */ | 132 | */ |
133 | #define PRELOAD_BUF 256 | ||
134 | |||
137 | int sysctl_preload_file(const char *filename, int output) | 135 | int sysctl_preload_file(const char *filename, int output) |
138 | { | 136 | { |
139 | int lineno = 0; | 137 | int lineno = 0; |
140 | char oneline[256]; | 138 | char oneline[PRELOAD_BUF]; |
141 | char buffer[256]; | 139 | char buffer[PRELOAD_BUF]; |
142 | char *name, *value, *ptr; | 140 | char *name, *value, *ptr; |
143 | FILE *fp = NULL; | 141 | FILE *fp = NULL; |
144 | 142 | ||
145 | if (!filename || ((fp = fopen(filename, "r")) == NULL)) { | 143 | if (!filename || ((fp = fopen(filename, "r")) == NULL)) { |
146 | bb_error_msg(ERR_PRELOAD_FILE, filename); | 144 | bb_error_msg_and_die(ERR_PRELOAD_FILE, filename); |
147 | return 1; | ||
148 | } | 145 | } |
149 | 146 | ||
150 | while (fgets(oneline, sizeof(oneline) - 1, fp)) { | 147 | while (fgets(oneline, sizeof(oneline) - 1, fp)) { |
@@ -175,9 +172,9 @@ int sysctl_preload_file(const char *filename, int output) | |||
175 | 172 | ||
176 | while ((*value == ' ' || *value == '\t') && *value != 0) | 173 | while ((*value == ' ' || *value == '\t') && *value != 0) |
177 | value++; | 174 | value++; |
178 | strncpy(buffer, name, sizeof(buffer)); | 175 | strcpy(buffer, name); |
179 | strncat(buffer, "=", sizeof(buffer)); | 176 | strcat(buffer, "="); |
180 | strncat(buffer, value, sizeof(buffer)); | 177 | strcat(buffer, value); |
181 | sysctl_write_setting(buffer, output); | 178 | sysctl_write_setting(buffer, output); |
182 | } | 179 | } |
183 | fclose(fp); | 180 | fclose(fp); |
@@ -200,7 +197,7 @@ int sysctl_write_setting(const char *setting, int output) | |||
200 | if (!name) /* probably dont' want to display this err */ | 197 | if (!name) /* probably dont' want to display this err */ |
201 | return 0; | 198 | return 0; |
202 | 199 | ||
203 | if (!(equals = index(setting, '='))) { | 200 | if (!(equals = strchr(setting, '='))) { |
204 | bb_error_msg(ERR_NO_EQUALS, setting); | 201 | bb_error_msg(ERR_NO_EQUALS, setting); |
205 | return -1; | 202 | return -1; |
206 | } | 203 | } |
@@ -212,16 +209,8 @@ int sysctl_write_setting(const char *setting, int output) | |||
212 | return -2; | 209 | return -2; |
213 | } | 210 | } |
214 | 211 | ||
215 | tmpname = | 212 | bb_xasprintf(&tmpname, "%s%.*s", PROC_PATH, (equals - name), name); |
216 | (char *) xmalloc((equals - name + 1 + bb_strlen(PROC_PATH)) * | 213 | outname = bb_xstrdup(tmpname + strlen(PROC_PATH)); |
217 | sizeof(char)); | ||
218 | outname = (char *) xmalloc((equals - name + 1) * sizeof(char)); | ||
219 | |||
220 | strcpy(tmpname, PROC_PATH); | ||
221 | strncat(tmpname, name, (int) (equals - name)); | ||
222 | tmpname[equals - name + bb_strlen(PROC_PATH)] = 0; | ||
223 | strncpy(outname, name, (int) (equals - name)); | ||
224 | outname[equals - name] = 0; | ||
225 | 214 | ||
226 | while ((cptr = strchr(tmpname, '.')) != NULL) | 215 | while ((cptr = strchr(tmpname, '.')) != NULL) |
227 | *cptr = '/'; | 216 | *cptr = '/'; |
@@ -243,14 +232,14 @@ int sysctl_write_setting(const char *setting, int output) | |||
243 | } | 232 | } |
244 | retval = -1; | 233 | retval = -1; |
245 | } else { | 234 | } else { |
246 | write(fd, value, bb_strlen(value)); | 235 | dwrite_str(fd, value); |
247 | close(fd); | 236 | close(fd); |
248 | if (output) { | 237 | if (output) { |
249 | write(STDOUT_FILENO, outname, bb_strlen(outname)); | 238 | dwrite_str(STDOUT_FILENO, outname); |
250 | write(STDOUT_FILENO, " = ", 3); | 239 | dwrite_str(STDOUT_FILENO, " = "); |
251 | } | 240 | } |
252 | write(STDOUT_FILENO, value, bb_strlen(value)); | 241 | dwrite_str(STDOUT_FILENO, value); |
253 | write(STDOUT_FILENO, "\n", 1); | 242 | dwrite_str(STDOUT_FILENO, "\n"); |
254 | } | 243 | } |
255 | 244 | ||
256 | /* cleanup */ | 245 | /* cleanup */ |
@@ -275,14 +264,8 @@ int sysctl_read_setting(const char *setting, int output) | |||
275 | if (!setting || !*setting) | 264 | if (!setting || !*setting) |
276 | bb_error_msg(ERR_INVALID_KEY, setting); | 265 | bb_error_msg(ERR_INVALID_KEY, setting); |
277 | 266 | ||
278 | tmpname = | 267 | tmpname = concat_path_file(PROC_PATH, name); |
279 | (char *) xmalloc((bb_strlen(name) + bb_strlen(PROC_PATH) + 1) * | 268 | outname = bb_xstrdup(tmpname + strlen(PROC_PATH)); |
280 | sizeof(char)); | ||
281 | outname = (char *) xmalloc((bb_strlen(name) + 1) * sizeof(char)); | ||
282 | |||
283 | strcpy(tmpname, PROC_PATH); | ||
284 | strcat(tmpname, name); | ||
285 | strcpy(outname, name); | ||
286 | 269 | ||
287 | while ((cptr = strchr(tmpname, '.')) != NULL) | 270 | while ((cptr = strchr(tmpname, '.')) != NULL) |
288 | *cptr = '/'; | 271 | *cptr = '/'; |
@@ -305,10 +288,10 @@ int sysctl_read_setting(const char *setting, int output) | |||
305 | } else { | 288 | } else { |
306 | while (fgets(inbuf, sizeof(inbuf) - 1, fp)) { | 289 | while (fgets(inbuf, sizeof(inbuf) - 1, fp)) { |
307 | if (output) { | 290 | if (output) { |
308 | write(STDOUT_FILENO, outname, bb_strlen(outname)); | 291 | dwrite_str(STDOUT_FILENO, outname); |
309 | write(STDOUT_FILENO, " = ", 3); | 292 | dwrite_str(STDOUT_FILENO, " = "); |
310 | } | 293 | } |
311 | write(STDOUT_FILENO, inbuf, bb_strlen(inbuf)); | 294 | dwrite_str(STDOUT_FILENO, inbuf); |
312 | } | 295 | } |
313 | fclose(fp); | 296 | fclose(fp); |
314 | } | 297 | } |
@@ -337,18 +320,14 @@ int sysctl_display_all(const char *path, int output, int show_table) | |||
337 | bb_perror_msg(ERR_OPENING_DIR, path); | 320 | bb_perror_msg(ERR_OPENING_DIR, path); |
338 | retval = -1; | 321 | retval = -1; |
339 | } else { | 322 | } else { |
340 | readdir(dp); | ||
341 | readdir(dp); /* skip . and .. */ | ||
342 | while ((de = readdir(dp)) != NULL) { | 323 | while ((de = readdir(dp)) != NULL) { |
343 | tmpdir = | 324 | tmpdir = concat_subpath_file(path, de->d_name); |
344 | (char *) xmalloc(bb_strlen(path) + bb_strlen(de->d_name) + 2); | 325 | if(tmpdir == NULL) |
345 | strcpy(tmpdir, path); | 326 | continue; |
346 | strcat(tmpdir, de->d_name); | ||
347 | if ((retval2 = stat(tmpdir, &ts)) != 0) | 327 | if ((retval2 = stat(tmpdir, &ts)) != 0) |
348 | bb_perror_msg(tmpdir); | 328 | bb_perror_msg(tmpdir); |
349 | else { | 329 | else { |
350 | if (S_ISDIR(ts.st_mode)) { | 330 | if (S_ISDIR(ts.st_mode)) { |
351 | strcat(tmpdir, "/"); | ||
352 | sysctl_display_all(tmpdir, output, show_table); | 331 | sysctl_display_all(tmpdir, output, show_table); |
353 | } else | 332 | } else |
354 | retval |= | 333 | retval |= |
@@ -369,4 +348,5 @@ int main(int argc, char **argv) | |||
369 | { | 348 | { |
370 | return sysctl_main(argc, argv); | 349 | return sysctl_main(argc, argv); |
371 | } | 350 | } |
351 | const char *bb_applet_name = "sysctl"; | ||
372 | #endif | 352 | #endif |