diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-10-29 19:22:13 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-10-29 19:22:13 +0000 |
commit | 5a28a25b9dd81e0975532458723c4244ff532e58 (patch) | |
tree | 53d30dbcfb05156500451389d47827487564b257 | |
parent | 63430ae35ad98d5946851e8a366379dd1574f2a2 (diff) | |
download | busybox-w32-5a28a25b9dd81e0975532458723c4244ff532e58.tar.gz busybox-w32-5a28a25b9dd81e0975532458723c4244ff532e58.tar.bz2 busybox-w32-5a28a25b9dd81e0975532458723c4244ff532e58.zip |
syscall: code shrink
text data bss dec hex filename
777209 974 9676 787859 c0593 busybox_old
777042 974 9676 787692 c04ec busybox_unstripped
-rw-r--r-- | libbb/trim.c | 8 | ||||
-rw-r--r-- | procps/sysctl.c | 185 |
2 files changed, 92 insertions, 101 deletions
diff --git a/libbb/trim.c b/libbb/trim.c index 4957d7276..94ccaf7ed 100644 --- a/libbb/trim.c +++ b/libbb/trim.c | |||
@@ -16,12 +16,16 @@ void trim(char *s) | |||
16 | size_t lws; | 16 | size_t lws; |
17 | 17 | ||
18 | /* trim trailing whitespace */ | 18 | /* trim trailing whitespace */ |
19 | while (len && isspace(s[len-1])) --len; | 19 | while (len && isspace(s[len-1])) |
20 | --len; | ||
20 | 21 | ||
21 | /* trim leading whitespace */ | 22 | /* trim leading whitespace */ |
22 | if (len) { | 23 | if (len) { |
23 | lws = strspn(s, " \n\r\t\v"); | 24 | lws = strspn(s, " \n\r\t\v"); |
24 | memmove(s, s + lws, len -= lws); | 25 | if (lws) { |
26 | len -= lws; | ||
27 | memmove(s, s + lws, len); | ||
28 | } | ||
25 | } | 29 | } |
26 | s[len] = '\0'; | 30 | s[len] = '\0'; |
27 | } | 31 | } |
diff --git a/procps/sysctl.c b/procps/sysctl.c index a84f9aef9..a12948017 100644 --- a/procps/sysctl.c +++ b/procps/sysctl.c | |||
@@ -27,28 +27,29 @@ static int sysctl_display_all(const char *path, int output, int show_table); | |||
27 | /* | 27 | /* |
28 | * Globals... | 28 | * Globals... |
29 | */ | 29 | */ |
30 | static const char PROC_PATH[] ALIGN1 = "/proc/sys/"; | 30 | static const char ETC_SYSCTL_CONF[] ALIGN1 = "/etc/sysctl.conf"; |
31 | static const char DEFAULT_PRELOAD[] ALIGN1 = "/etc/sysctl.conf"; | 31 | static const char PROC_SYS[] ALIGN1 = "/proc/sys/"; |
32 | enum { strlen_PROC_SYS = sizeof(PROC_SYS) - 1 }; | ||
32 | 33 | ||
33 | /* error messages */ | 34 | /* error messages */ |
34 | static const char ERR_UNKNOWN_PARAMETER[] ALIGN1 = | 35 | static const char ERR_UNKNOWN_PARAMETER[] ALIGN1 = |
35 | "error: Unknown parameter '%s'\n"; | 36 | "error: unknown parameter '%s'"; |
36 | static const char ERR_MALFORMED_SETTING[] ALIGN1 = | 37 | static const char ERR_MALFORMED_SETTING[] ALIGN1 = |
37 | "error: Malformed setting '%s'\n"; | 38 | "error: malformed setting '%s'"; |
38 | static const char ERR_NO_EQUALS[] ALIGN1 = | 39 | static const char ERR_NO_EQUALS[] ALIGN1 = |
39 | "error: '%s' must be of the form name=value\n"; | 40 | "error: '%s' must be of the form name=value"; |
40 | static const char ERR_INVALID_KEY[] ALIGN1 = | 41 | static const char ERR_INVALID_KEY[] ALIGN1 = |
41 | "error: '%s' is an unknown key\n"; | 42 | "error: '%s' is an unknown key"; |
42 | static const char ERR_UNKNOWN_WRITING[] ALIGN1 = | 43 | static const char ERR_UNKNOWN_WRITING[] ALIGN1 = |
43 | "error: unknown error %d setting key '%s'\n"; | 44 | "error setting key '%s'"; |
44 | static const char ERR_UNKNOWN_READING[] ALIGN1 = | 45 | static const char ERR_UNKNOWN_READING[] ALIGN1 = |
45 | "error: unknown error %d reading key '%s'\n"; | 46 | "error reading key '%s'"; |
46 | static const char ERR_PERMISSION_DENIED[] ALIGN1 = | 47 | static const char ERR_PERMISSION_DENIED[] ALIGN1 = |
47 | "error: permission denied on key '%s'\n"; | 48 | "error: permission denied on key '%s'"; |
48 | static const char ERR_PRELOAD_FILE[] ALIGN1 = | 49 | static const char ERR_PRELOAD_FILE[] ALIGN1 = |
49 | "error: cannot open preload file '%s'\n"; | 50 | "error: cannot open preload file '%s'"; |
50 | static const char WARN_BAD_LINE[] ALIGN1 = | 51 | static const char WARN_BAD_LINE[] ALIGN1 = |
51 | "warning: %s(%d): invalid syntax, continuing...\n"; | 52 | "warning: %s(%d): invalid syntax, continuing"; |
52 | 53 | ||
53 | 54 | ||
54 | static void dwrite_str(int fd, const char *buf) | 55 | static void dwrite_str(int fd, const char *buf) |
@@ -72,7 +73,7 @@ int sysctl_main(int argc, char **argv) | |||
72 | 73 | ||
73 | argv++; | 74 | argv++; |
74 | 75 | ||
75 | for (; argv && *argv && **argv; argv++) { | 76 | for (; *argv /*&& **argv*/; argv++) { |
76 | if (switches_allowed && **argv == '-') { /* we have a switch */ | 77 | if (switches_allowed && **argv == '-') { /* we have a switch */ |
77 | switch ((*argv)[1]) { | 78 | switch ((*argv)[1]) { |
78 | case 'n': | 79 | case 'n': |
@@ -84,120 +85,117 @@ int sysctl_main(int argc, char **argv) | |||
84 | break; | 85 | break; |
85 | case 'p': | 86 | case 'p': |
86 | argv++; | 87 | argv++; |
87 | return | 88 | return sysctl_preload_file(((*argv /*&& **argv*/) ? *argv : ETC_SYSCTL_CONF), |
88 | sysctl_preload_file(((argv && *argv | 89 | output); |
89 | && **argv) ? *argv : | ||
90 | DEFAULT_PRELOAD), output); | ||
91 | case 'a': | 90 | case 'a': |
92 | case 'A': | 91 | case 'A': |
93 | switches_allowed = 0; | 92 | return sysctl_display_all(PROC_SYS, output, |
94 | return sysctl_display_all(PROC_PATH, output, | 93 | ((*argv)[1] == 'A')); |
95 | ((*argv)[1] == 'a') ? 0 : 1); | ||
96 | case 'h': | ||
97 | case '?': | ||
98 | bb_show_usage(); | ||
99 | default: | 94 | default: |
100 | bb_error_msg(ERR_UNKNOWN_PARAMETER, *argv); | 95 | bb_error_msg(ERR_UNKNOWN_PARAMETER, *argv); |
96 | /* fall through */ | ||
97 | //case 'h': | ||
98 | //case '?': | ||
101 | bb_show_usage(); | 99 | bb_show_usage(); |
102 | } | 100 | } |
103 | } else { | 101 | } else { |
104 | switches_allowed = 0; | 102 | switches_allowed = 0; |
105 | if (write_mode) | 103 | if (write_mode) |
106 | retval = sysctl_write_setting(*argv, output); | 104 | retval |= sysctl_write_setting(*argv, output); |
107 | else | 105 | else |
108 | sysctl_read_setting(*argv, output); | 106 | sysctl_read_setting(*argv, output); |
109 | } | 107 | } |
110 | } | 108 | } |
111 | return retval; | 109 | return retval; |
112 | } /* end sysctl_main() */ | 110 | } /* end sysctl_main() */ |
113 | |||
114 | |||
115 | 111 | ||
116 | /* | 112 | /* |
117 | * sysctl_preload_file | 113 | * sysctl_preload_file |
118 | * preload the sysctl's from a conf file | 114 | * preload the sysctl's from a conf file |
119 | * - we parse the file and then reform it (strip out whitespace) | 115 | * - we parse the file and then reform it (strip out whitespace) |
120 | */ | 116 | */ |
121 | #define PRELOAD_BUF 256 | 117 | #define PRELOAD_BUF 256 |
122 | 118 | ||
123 | int sysctl_preload_file(const char *filename, int output) | 119 | static int sysctl_preload_file(const char *filename, int output) |
124 | { | 120 | { |
125 | int lineno = 0; | 121 | int lineno; |
126 | char oneline[PRELOAD_BUF]; | 122 | char oneline[PRELOAD_BUF]; |
127 | char buffer[PRELOAD_BUF]; | 123 | char buffer[PRELOAD_BUF]; |
128 | char *name, *value, *ptr; | 124 | char *name, *value; |
129 | FILE *fp = NULL; | 125 | FILE *fp; |
130 | 126 | ||
131 | if (!filename || ((fp = fopen(filename, "r")) == NULL)) { | 127 | fp = fopen(filename, "r"); |
128 | if (fp == NULL) { | ||
132 | bb_error_msg_and_die(ERR_PRELOAD_FILE, filename); | 129 | bb_error_msg_and_die(ERR_PRELOAD_FILE, filename); |
133 | } | 130 | } |
134 | 131 | ||
132 | lineno = 0; | ||
135 | while (fgets(oneline, sizeof(oneline) - 1, fp)) { | 133 | while (fgets(oneline, sizeof(oneline) - 1, fp)) { |
136 | oneline[sizeof(oneline) - 1] = '\0'; | ||
137 | lineno++; | 134 | lineno++; |
138 | trim(oneline); | 135 | trim(oneline); |
139 | ptr = (char *) oneline; | 136 | if (oneline[0] == '#' || oneline[0] == ';') |
140 | |||
141 | if (*ptr == '#' || *ptr == ';') | ||
142 | continue; | 137 | continue; |
143 | 138 | if (!oneline[0] || !oneline[1]) | |
144 | if (strlen(ptr) < 2) | ||
145 | continue; | 139 | continue; |
146 | 140 | ||
147 | name = strtok(ptr, "="); | 141 | name = strtok(oneline, "="); |
148 | if (!name || !*name) { | 142 | if (!name) { |
149 | bb_error_msg(WARN_BAD_LINE, filename, lineno); | 143 | bb_error_msg(WARN_BAD_LINE, filename, lineno); |
150 | continue; | 144 | continue; |
151 | } | 145 | } |
152 | |||
153 | trim(name); | 146 | trim(name); |
147 | if (!*name) { | ||
148 | bb_error_msg(WARN_BAD_LINE, filename, lineno); | ||
149 | continue; | ||
150 | } | ||
154 | 151 | ||
155 | value = strtok(NULL, "\n\r"); | 152 | value = strtok(NULL, "\n\r"); |
156 | if (!value || !*value) { | 153 | if (!value) { |
157 | bb_error_msg(WARN_BAD_LINE, filename, lineno); | 154 | bb_error_msg(WARN_BAD_LINE, filename, lineno); |
158 | continue; | 155 | continue; |
159 | } | 156 | } |
160 | 157 | while (*value == ' ' || *value == '\t') | |
161 | while ((*value == ' ' || *value == '\t') && *value != 0) | ||
162 | value++; | 158 | value++; |
159 | if (!*value) { | ||
160 | bb_error_msg(WARN_BAD_LINE, filename, lineno); | ||
161 | continue; | ||
162 | } | ||
163 | |||
163 | /* safe because sizeof(oneline) == sizeof(buffer) */ | 164 | /* safe because sizeof(oneline) == sizeof(buffer) */ |
164 | sprintf(buffer, "%s=%s", name, value); | 165 | sprintf(buffer, "%s=%s", name, value); |
165 | sysctl_write_setting(buffer, output); | 166 | sysctl_write_setting(buffer, output); |
166 | } | 167 | } |
167 | fclose(fp); | 168 | fclose(fp); |
168 | return 0; | 169 | return 0; |
169 | } /* end sysctl_preload_file() */ | 170 | } /* end sysctl_preload_file() */ |
170 | |||
171 | 171 | ||
172 | /* | 172 | /* |
173 | * Write a single sysctl setting | 173 | * Write a single sysctl setting |
174 | */ | 174 | */ |
175 | int sysctl_write_setting(const char *setting, int output) | 175 | static int sysctl_write_setting(const char *setting, int output) |
176 | { | 176 | { |
177 | int retval = 0; | 177 | int retval = 0; |
178 | const char *name = setting; | 178 | const char *name; |
179 | const char *value; | 179 | const char *value; |
180 | const char *equals; | 180 | const char *equals; |
181 | char *tmpname, *outname, *cptr; | 181 | char *tmpname, *outname, *cptr; |
182 | int fd = -1; | 182 | int fd = -1; |
183 | 183 | ||
184 | if (!name) /* probably dont' want to display this err */ | 184 | name = setting; |
185 | return 0; | 185 | equals = strchr(setting, '='); |
186 | 186 | if (!equals) { | |
187 | if (!(equals = strchr(setting, '='))) { | ||
188 | bb_error_msg(ERR_NO_EQUALS, setting); | 187 | bb_error_msg(ERR_NO_EQUALS, setting); |
189 | return -1; | 188 | return -1; |
190 | } | 189 | } |
191 | 190 | ||
192 | value = equals + sizeof(char); /* point to the value in name=value */ | 191 | value = equals + 1; /* point to the value in name=value */ |
193 | 192 | if (name == equals || !*value) { | |
194 | if (!*name || !*value || name == equals) { | ||
195 | bb_error_msg(ERR_MALFORMED_SETTING, setting); | 193 | bb_error_msg(ERR_MALFORMED_SETTING, setting); |
196 | return -2; | 194 | return -2; |
197 | } | 195 | } |
198 | 196 | ||
199 | tmpname = xasprintf("%s%.*s", PROC_PATH, (int)(equals - name), name); | 197 | tmpname = xasprintf("%s%.*s", PROC_SYS, (int)(equals - name), name); |
200 | outname = xstrdup(tmpname + strlen(PROC_PATH)); | 198 | outname = xstrdup(tmpname + strlen_PROC_SYS); |
201 | 199 | ||
202 | while ((cptr = strchr(tmpname, '.')) != NULL) | 200 | while ((cptr = strchr(tmpname, '.')) != NULL) |
203 | *cptr = '/'; | 201 | *cptr = '/'; |
@@ -215,7 +213,7 @@ int sysctl_write_setting(const char *setting, int output) | |||
215 | bb_perror_msg(ERR_PERMISSION_DENIED, outname); | 213 | bb_perror_msg(ERR_PERMISSION_DENIED, outname); |
216 | break; | 214 | break; |
217 | default: | 215 | default: |
218 | bb_error_msg(ERR_UNKNOWN_WRITING, errno, outname); | 216 | bb_perror_msg(ERR_UNKNOWN_WRITING, outname); |
219 | break; | 217 | break; |
220 | } | 218 | } |
221 | retval = -1; | 219 | retval = -1; |
@@ -230,37 +228,36 @@ int sysctl_write_setting(const char *setting, int output) | |||
230 | dwrite_str(STDOUT_FILENO, "\n"); | 228 | dwrite_str(STDOUT_FILENO, "\n"); |
231 | } | 229 | } |
232 | 230 | ||
233 | /* cleanup */ | ||
234 | free(tmpname); | 231 | free(tmpname); |
235 | free(outname); | 232 | free(outname); |
236 | return retval; | 233 | return retval; |
237 | } /* end sysctl_write_setting() */ | 234 | } /* end sysctl_write_setting() */ |
238 | |||
239 | 235 | ||
240 | /* | 236 | /* |
241 | * Read a sysctl setting | 237 | * Read a sysctl setting |
242 | * | ||
243 | */ | 238 | */ |
244 | int sysctl_read_setting(const char *setting, int output) | 239 | static int sysctl_read_setting(const char *setting, int output) |
245 | { | 240 | { |
246 | int retval = 0; | 241 | int retval = 0; |
247 | char *tmpname, *outname, *cptr; | 242 | char *tmpname, *outname, *cptr; |
248 | char inbuf[1025]; | 243 | char inbuf[1025]; |
249 | const char *name = setting; | 244 | const char *name; |
250 | FILE *fp; | 245 | FILE *fp; |
251 | 246 | ||
252 | if (!setting || !*setting) | 247 | if (!*setting) |
253 | bb_error_msg(ERR_INVALID_KEY, setting); | 248 | bb_error_msg(ERR_INVALID_KEY, setting); |
254 | 249 | ||
255 | tmpname = concat_path_file(PROC_PATH, name); | 250 | name = setting; |
256 | outname = xstrdup(tmpname + strlen(PROC_PATH)); | 251 | tmpname = concat_path_file(PROC_SYS, name); |
252 | outname = xstrdup(tmpname + strlen_PROC_SYS); | ||
257 | 253 | ||
258 | while ((cptr = strchr(tmpname, '.')) != NULL) | 254 | while ((cptr = strchr(tmpname, '.')) != NULL) |
259 | *cptr = '/'; | 255 | *cptr = '/'; |
260 | while ((cptr = strchr(outname, '/')) != NULL) | 256 | while ((cptr = strchr(outname, '/')) != NULL) |
261 | *cptr = '.'; | 257 | *cptr = '.'; |
262 | 258 | ||
263 | if ((fp = fopen(tmpname, "r")) == NULL) { | 259 | fp = fopen(tmpname, "r"); |
260 | if (fp == NULL) { | ||
264 | switch (errno) { | 261 | switch (errno) { |
265 | case ENOENT: | 262 | case ENOENT: |
266 | bb_error_msg(ERR_INVALID_KEY, outname); | 263 | bb_error_msg(ERR_INVALID_KEY, outname); |
@@ -269,7 +266,7 @@ int sysctl_read_setting(const char *setting, int output) | |||
269 | bb_error_msg(ERR_PERMISSION_DENIED, outname); | 266 | bb_error_msg(ERR_PERMISSION_DENIED, outname); |
270 | break; | 267 | break; |
271 | default: | 268 | default: |
272 | bb_error_msg(ERR_UNKNOWN_READING, errno, outname); | 269 | bb_perror_msg(ERR_UNKNOWN_READING, outname); |
273 | break; | 270 | break; |
274 | } | 271 | } |
275 | retval = -1; | 272 | retval = -1; |
@@ -287,18 +284,14 @@ int sysctl_read_setting(const char *setting, int output) | |||
287 | free(tmpname); | 284 | free(tmpname); |
288 | free(outname); | 285 | free(outname); |
289 | return retval; | 286 | return retval; |
290 | } /* end sysctl_read_setting() */ | 287 | } /* end sysctl_read_setting() */ |
291 | |||
292 | |||
293 | 288 | ||
294 | /* | 289 | /* |
295 | * Display all the sysctl settings | 290 | * Display all the sysctl settings |
296 | * | ||
297 | */ | 291 | */ |
298 | int sysctl_display_all(const char *path, int output, int show_table) | 292 | static int sysctl_display_all(const char *path, int output, int show_table) |
299 | { | 293 | { |
300 | int retval = 0; | 294 | int retval = 0; |
301 | int retval2; | ||
302 | DIR *dp; | 295 | DIR *dp; |
303 | struct dirent *de; | 296 | struct dirent *de; |
304 | char *tmpdir; | 297 | char *tmpdir; |
@@ -306,28 +299,22 @@ int sysctl_display_all(const char *path, int output, int show_table) | |||
306 | 299 | ||
307 | dp = opendir(path); | 300 | dp = opendir(path); |
308 | if (!dp) { | 301 | if (!dp) { |
309 | retval = -1; | 302 | return -1; |
310 | } else { | ||
311 | while ((de = readdir(dp)) != NULL) { | ||
312 | tmpdir = concat_subpath_file(path, de->d_name); | ||
313 | if (tmpdir == NULL) | ||
314 | continue; | ||
315 | retval2 = stat(tmpdir, &ts); | ||
316 | if (retval2 != 0) | ||
317 | bb_perror_msg(tmpdir); | ||
318 | else { | ||
319 | if (S_ISDIR(ts.st_mode)) { | ||
320 | sysctl_display_all(tmpdir, output, show_table); | ||
321 | } else | ||
322 | retval |= | ||
323 | sysctl_read_setting(tmpdir + strlen(PROC_PATH), | ||
324 | output); | ||
325 | |||
326 | } | ||
327 | free(tmpdir); | ||
328 | } /* end while */ | ||
329 | closedir(dp); | ||
330 | } | 303 | } |
304 | while ((de = readdir(dp)) != NULL) { | ||
305 | tmpdir = concat_subpath_file(path, de->d_name); | ||
306 | if (tmpdir == NULL) | ||
307 | continue; | ||
308 | if (stat(tmpdir, &ts) != 0) { | ||
309 | bb_perror_msg(tmpdir); | ||
310 | } else if (S_ISDIR(ts.st_mode)) { | ||
311 | sysctl_display_all(tmpdir, output, show_table); | ||
312 | } else { | ||
313 | retval |= sysctl_read_setting(tmpdir + strlen_PROC_SYS, output); | ||
314 | } | ||
315 | free(tmpdir); | ||
316 | } /* end while */ | ||
317 | closedir(dp); | ||
331 | 318 | ||
332 | return retval; | 319 | return retval; |
333 | } /* end sysctl_display_all() */ | 320 | } /* end sysctl_display_all() */ |