diff options
author | Rob Landley <rob@landley.net> | 2014-05-12 07:52:12 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2014-05-12 07:52:12 -0500 |
commit | f272df9e821e00afb962a62556318cecfcd694ae (patch) | |
tree | 096f013eabe0cd475c0b02b8ca47fe4f9d10671c | |
parent | 97c2c1eba507d10005e305098c3fc21b1472c97e (diff) | |
download | toybox-f272df9e821e00afb962a62556318cecfcd694ae.tar.gz |
First cleanup pass on sysctl.
Remove unnecessary typecasting of things that are already char * to char *,
and multiplying by sizeof(char *) which is 1. Rename do_process() do_flag_a()
since that's the only caller. Move read_key_values() down past trim_spaces()
and read_config_file() so it's next to its only two users. Replace some
euphemisms for 0 with 0.
replace_char() really sounds like something libc should already have one of,
but I'm blanking on it if so. (It doesn't need a temporary variable when the
argument variable is already a copy so changing it won't affect the caller.)
-rw-r--r-- | toys/pending/sysctl.c | 80 |
1 files changed, 40 insertions, 40 deletions
diff --git a/toys/pending/sysctl.c b/toys/pending/sysctl.c index 74e7dee1..7e8736d7 100644 --- a/toys/pending/sysctl.c +++ b/toys/pending/sysctl.c @@ -13,6 +13,8 @@ config SYSCTL help usage: sysctl [OPTIONS] [KEY[=VALUE]]... + Configure kernel parameters at runtime. + -a, A Show all values -e Don't warn about unknown keys -N Show only key names @@ -45,10 +47,7 @@ show_error_msg: static void replace_char(char *str, char old, char new) { - char *tmp = str; - - for (; *tmp; tmp++) - if (*tmp == old) *tmp = new; + for (; *str; str++) if (*str == old) *str = new; } static void handle_file_error(char *key_name) @@ -84,11 +83,11 @@ static void write_to_file(char *fpath, char *key_name, char *key_value) static char *get_key_value(char *buff, int *offset) { - char *line, *tmp = (char *) (buff + *offset); + char *line, *tmp = buff + *offset; int index = 0, multiplier = 1; - if (!(*tmp)) return NULL; - line = (char *) xmalloc(sizeof(char) * MAX_BYTES_LINE); + if (!*tmp) return 0; + line = xmalloc(MAX_BYTES_LINE); for (; *tmp != '\n'; tmp++) { line[index++] = *tmp; if (MAX_BYTES_LINE == index) { // buffer overflow @@ -101,35 +100,6 @@ static char *get_key_value(char *buff, int *offset) return line; } -// Open file for each and every key name and read file contents -void read_key_values(char *fpath) -{ - char *key_value, *fdata, *key_name, *tmp = xstrdup(fpath); - int offset = 0; - - key_name = (tmp + strlen(PROC_SYS_DIR) + 1); - replace_char(key_name, '/', '.'); - if (!(fdata = readfile(fpath, NULL, 0))) { - handle_file_error(key_name); - free(tmp); - return; - } - if (toys.optflags & FLAG_N) { - xprintf("%s\n", key_name); - free(tmp); - free(fdata); - return; - } - for (; (key_value = get_key_value(fdata, &offset)); free(key_value)) { - if (!(toys.optflags & FLAG_q)) { - if (!(toys.optflags & FLAG_n)) xprintf("%s = ", key_name); - xprintf("%s\n", key_value); - } - } - free(tmp); - free(fdata); -} - static void trim_spaces(char **param) { int len = 0; @@ -147,7 +117,7 @@ static void trim_spaces(char **param) } } -// Read config file and write values to there corresponding key name files +// Read config file and write values to corresponding key name files static void read_config_file(char *fname) { char *line, *name = NULL, *value = NULL; @@ -167,7 +137,36 @@ static void read_config_file(char *fname) xclose(fd); } -static int do_process(struct dirtree *dt) +// Open file for each and every key name and read file contents +void read_key_values(char *fpath) +{ + char *key_value, *fdata, *key_name, *tmp = xstrdup(fpath); + int offset = 0; + + key_name = (tmp + strlen(PROC_SYS_DIR) + 1); + replace_char(key_name, '/', '.'); + if (!(fdata = readfile(fpath, NULL, 0))) { + handle_file_error(key_name); + free(tmp); + return; + } + if (toys.optflags & FLAG_N) { + xprintf("%s\n", key_name); + free(tmp); + free(fdata); + return; + } + for (; (key_value = get_key_value(fdata, &offset)); free(key_value)) { + if (!(toys.optflags & FLAG_q)) { + if (!(toys.optflags & FLAG_n)) xprintf("%s = ", key_name); + xprintf("%s\n", key_value); + } + } + free(tmp); + free(fdata); +} + +static int do_flag_a(struct dirtree *dt) { char *fpath; @@ -177,15 +176,16 @@ static int do_process(struct dirtree *dt) read_key_values(fpath); free(fpath); } + return 0; } void sysctl_main() { - char *name = NULL, *value = NULL, **args = NULL; + char *name = 0, *value = 0, **args = 0; if (toys.optflags & FLAG_a) { - dirtree_read(PROC_SYS_DIR, do_process); + dirtree_read(PROC_SYS_DIR, do_flag_a); return; } if (toys.optflags & FLAG_p) { |