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
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)
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
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;
}
}
-// 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;
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;
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) {