From 5a28a25b9dd81e0975532458723c4244ff532e58 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Mon, 29 Oct 2007 19:22:13 +0000 Subject: syscall: code shrink text data bss dec hex filename 777209 974 9676 787859 c0593 busybox_old 777042 974 9676 787692 c04ec busybox_unstripped --- libbb/trim.c | 8 ++- 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) size_t lws; /* trim trailing whitespace */ - while (len && isspace(s[len-1])) --len; + while (len && isspace(s[len-1])) + --len; /* trim leading whitespace */ if (len) { lws = strspn(s, " \n\r\t\v"); - memmove(s, s + lws, len -= lws); + if (lws) { + len -= lws; + memmove(s, s + lws, len); + } } s[len] = '\0'; } 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); /* * Globals... */ -static const char PROC_PATH[] ALIGN1 = "/proc/sys/"; -static const char DEFAULT_PRELOAD[] ALIGN1 = "/etc/sysctl.conf"; +static const char ETC_SYSCTL_CONF[] ALIGN1 = "/etc/sysctl.conf"; +static const char PROC_SYS[] ALIGN1 = "/proc/sys/"; +enum { strlen_PROC_SYS = sizeof(PROC_SYS) - 1 }; /* error messages */ static const char ERR_UNKNOWN_PARAMETER[] ALIGN1 = - "error: Unknown parameter '%s'\n"; + "error: unknown parameter '%s'"; static const char ERR_MALFORMED_SETTING[] ALIGN1 = - "error: Malformed setting '%s'\n"; + "error: malformed setting '%s'"; static const char ERR_NO_EQUALS[] ALIGN1 = - "error: '%s' must be of the form name=value\n"; + "error: '%s' must be of the form name=value"; static const char ERR_INVALID_KEY[] ALIGN1 = - "error: '%s' is an unknown key\n"; + "error: '%s' is an unknown key"; static const char ERR_UNKNOWN_WRITING[] ALIGN1 = - "error: unknown error %d setting key '%s'\n"; + "error setting key '%s'"; static const char ERR_UNKNOWN_READING[] ALIGN1 = - "error: unknown error %d reading key '%s'\n"; + "error reading key '%s'"; static const char ERR_PERMISSION_DENIED[] ALIGN1 = - "error: permission denied on key '%s'\n"; + "error: permission denied on key '%s'"; static const char ERR_PRELOAD_FILE[] ALIGN1 = - "error: cannot open preload file '%s'\n"; + "error: cannot open preload file '%s'"; static const char WARN_BAD_LINE[] ALIGN1 = - "warning: %s(%d): invalid syntax, continuing...\n"; + "warning: %s(%d): invalid syntax, continuing"; static void dwrite_str(int fd, const char *buf) @@ -72,7 +73,7 @@ int sysctl_main(int argc, char **argv) argv++; - for (; argv && *argv && **argv; argv++) { + for (; *argv /*&& **argv*/; argv++) { if (switches_allowed && **argv == '-') { /* we have a switch */ switch ((*argv)[1]) { case 'n': @@ -84,120 +85,117 @@ int sysctl_main(int argc, char **argv) break; case 'p': argv++; - return - sysctl_preload_file(((argv && *argv - && **argv) ? *argv : - DEFAULT_PRELOAD), output); + return sysctl_preload_file(((*argv /*&& **argv*/) ? *argv : ETC_SYSCTL_CONF), + output); case 'a': case 'A': - switches_allowed = 0; - return sysctl_display_all(PROC_PATH, output, - ((*argv)[1] == 'a') ? 0 : 1); - case 'h': - case '?': - bb_show_usage(); + return sysctl_display_all(PROC_SYS, output, + ((*argv)[1] == 'A')); default: bb_error_msg(ERR_UNKNOWN_PARAMETER, *argv); + /* fall through */ + //case 'h': + //case '?': bb_show_usage(); } } else { switches_allowed = 0; if (write_mode) - retval = sysctl_write_setting(*argv, output); + retval |= sysctl_write_setting(*argv, output); else sysctl_read_setting(*argv, output); } } return retval; -} /* end sysctl_main() */ - - +} /* end sysctl_main() */ /* - * sysctl_preload_file - * preload the sysctl's from a conf file - * - we parse the file and then reform it (strip out whitespace) + * sysctl_preload_file + * preload the sysctl's from a conf file + * - we parse the file and then reform it (strip out whitespace) */ #define PRELOAD_BUF 256 -int sysctl_preload_file(const char *filename, int output) +static int sysctl_preload_file(const char *filename, int output) { - int lineno = 0; + int lineno; char oneline[PRELOAD_BUF]; char buffer[PRELOAD_BUF]; - char *name, *value, *ptr; - FILE *fp = NULL; + char *name, *value; + FILE *fp; - if (!filename || ((fp = fopen(filename, "r")) == NULL)) { + fp = fopen(filename, "r"); + if (fp == NULL) { bb_error_msg_and_die(ERR_PRELOAD_FILE, filename); } + lineno = 0; while (fgets(oneline, sizeof(oneline) - 1, fp)) { - oneline[sizeof(oneline) - 1] = '\0'; lineno++; trim(oneline); - ptr = (char *) oneline; - - if (*ptr == '#' || *ptr == ';') + if (oneline[0] == '#' || oneline[0] == ';') continue; - - if (strlen(ptr) < 2) + if (!oneline[0] || !oneline[1]) continue; - name = strtok(ptr, "="); - if (!name || !*name) { + name = strtok(oneline, "="); + if (!name) { bb_error_msg(WARN_BAD_LINE, filename, lineno); continue; } - trim(name); + if (!*name) { + bb_error_msg(WARN_BAD_LINE, filename, lineno); + continue; + } value = strtok(NULL, "\n\r"); - if (!value || !*value) { + if (!value) { bb_error_msg(WARN_BAD_LINE, filename, lineno); continue; } - - while ((*value == ' ' || *value == '\t') && *value != 0) + while (*value == ' ' || *value == '\t') value++; + if (!*value) { + bb_error_msg(WARN_BAD_LINE, filename, lineno); + continue; + } + /* safe because sizeof(oneline) == sizeof(buffer) */ sprintf(buffer, "%s=%s", name, value); sysctl_write_setting(buffer, output); } fclose(fp); return 0; -} /* end sysctl_preload_file() */ - +} /* end sysctl_preload_file() */ /* * Write a single sysctl setting */ -int sysctl_write_setting(const char *setting, int output) +static int sysctl_write_setting(const char *setting, int output) { int retval = 0; - const char *name = setting; + const char *name; const char *value; const char *equals; char *tmpname, *outname, *cptr; int fd = -1; - if (!name) /* probably dont' want to display this err */ - return 0; - - if (!(equals = strchr(setting, '='))) { + name = setting; + equals = strchr(setting, '='); + if (!equals) { bb_error_msg(ERR_NO_EQUALS, setting); return -1; } - value = equals + sizeof(char); /* point to the value in name=value */ - - if (!*name || !*value || name == equals) { + value = equals + 1; /* point to the value in name=value */ + if (name == equals || !*value) { bb_error_msg(ERR_MALFORMED_SETTING, setting); return -2; } - tmpname = xasprintf("%s%.*s", PROC_PATH, (int)(equals - name), name); - outname = xstrdup(tmpname + strlen(PROC_PATH)); + tmpname = xasprintf("%s%.*s", PROC_SYS, (int)(equals - name), name); + outname = xstrdup(tmpname + strlen_PROC_SYS); while ((cptr = strchr(tmpname, '.')) != NULL) *cptr = '/'; @@ -215,7 +213,7 @@ int sysctl_write_setting(const char *setting, int output) bb_perror_msg(ERR_PERMISSION_DENIED, outname); break; default: - bb_error_msg(ERR_UNKNOWN_WRITING, errno, outname); + bb_perror_msg(ERR_UNKNOWN_WRITING, outname); break; } retval = -1; @@ -230,37 +228,36 @@ int sysctl_write_setting(const char *setting, int output) dwrite_str(STDOUT_FILENO, "\n"); } - /* cleanup */ free(tmpname); free(outname); return retval; -} /* end sysctl_write_setting() */ - +} /* end sysctl_write_setting() */ /* * Read a sysctl setting - * */ -int sysctl_read_setting(const char *setting, int output) +static int sysctl_read_setting(const char *setting, int output) { int retval = 0; char *tmpname, *outname, *cptr; char inbuf[1025]; - const char *name = setting; + const char *name; FILE *fp; - if (!setting || !*setting) + if (!*setting) bb_error_msg(ERR_INVALID_KEY, setting); - tmpname = concat_path_file(PROC_PATH, name); - outname = xstrdup(tmpname + strlen(PROC_PATH)); + name = setting; + tmpname = concat_path_file(PROC_SYS, name); + outname = xstrdup(tmpname + strlen_PROC_SYS); while ((cptr = strchr(tmpname, '.')) != NULL) *cptr = '/'; while ((cptr = strchr(outname, '/')) != NULL) *cptr = '.'; - if ((fp = fopen(tmpname, "r")) == NULL) { + fp = fopen(tmpname, "r"); + if (fp == NULL) { switch (errno) { case ENOENT: bb_error_msg(ERR_INVALID_KEY, outname); @@ -269,7 +266,7 @@ int sysctl_read_setting(const char *setting, int output) bb_error_msg(ERR_PERMISSION_DENIED, outname); break; default: - bb_error_msg(ERR_UNKNOWN_READING, errno, outname); + bb_perror_msg(ERR_UNKNOWN_READING, outname); break; } retval = -1; @@ -287,18 +284,14 @@ int sysctl_read_setting(const char *setting, int output) free(tmpname); free(outname); return retval; -} /* end sysctl_read_setting() */ - - +} /* end sysctl_read_setting() */ /* * Display all the sysctl settings - * */ -int sysctl_display_all(const char *path, int output, int show_table) +static int sysctl_display_all(const char *path, int output, int show_table) { int retval = 0; - int retval2; DIR *dp; struct dirent *de; char *tmpdir; @@ -306,28 +299,22 @@ int sysctl_display_all(const char *path, int output, int show_table) dp = opendir(path); if (!dp) { - retval = -1; - } else { - while ((de = readdir(dp)) != NULL) { - tmpdir = concat_subpath_file(path, de->d_name); - if (tmpdir == NULL) - continue; - retval2 = stat(tmpdir, &ts); - if (retval2 != 0) - bb_perror_msg(tmpdir); - else { - if (S_ISDIR(ts.st_mode)) { - sysctl_display_all(tmpdir, output, show_table); - } else - retval |= - sysctl_read_setting(tmpdir + strlen(PROC_PATH), - output); - - } - free(tmpdir); - } /* end while */ - closedir(dp); + return -1; } + while ((de = readdir(dp)) != NULL) { + tmpdir = concat_subpath_file(path, de->d_name); + if (tmpdir == NULL) + continue; + if (stat(tmpdir, &ts) != 0) { + bb_perror_msg(tmpdir); + } else if (S_ISDIR(ts.st_mode)) { + sysctl_display_all(tmpdir, output, show_table); + } else { + retval |= sysctl_read_setting(tmpdir + strlen_PROC_SYS, output); + } + free(tmpdir); + } /* end while */ + closedir(dp); return retval; -} /* end sysctl_display_all() */ +} /* end sysctl_display_all() */ -- cgit v1.2.3