? nl ? sysctl.d Index: sysctl.8 =================================================================== RCS file: /cvs/src/sbin/sysctl/sysctl.8,v retrieving revision 1.213 diff -u -p -r1.213 sysctl.8 --- sysctl.8 12 Jan 2018 04:36:44 -0000 1.213 +++ sysctl.8 11 Feb 2018 00:52:26 -0000 @@ -45,6 +45,9 @@ .Nm sysctl .Op Fl nq .Ar name Ns = Ns Ar value ... +.Nm sysctl +.Op Fl nq +.Fl f Ar file .Sh DESCRIPTION The .Nm @@ -76,6 +79,15 @@ flag; for the table values, the name of List all the currently available string or integer values. This is the default, if no parameters are given to .Nm . +.It Fl f Ar file +Read +.Ar name +variables to output, or +.Ar name Ns = Ns Ar value +assignments from lines of a file. +If +.Ar file +is specified as - lines will be read from standard input. .It Fl n Suppress printing of the field name, only output the field value. Useful for setting shell variables. Index: sysctl.c =================================================================== RCS file: /cvs/src/sbin/sysctl/sysctl.c,v retrieving revision 1.229 diff -u -p -r1.229 sysctl.c --- sysctl.c 10 Feb 2018 05:53:58 -0000 1.229 +++ sysctl.c 11 Feb 2018 00:52:26 -0000 @@ -184,6 +184,7 @@ void debuginit(void); void listall(char *, struct list *); int parse_hex_char(char); ssize_t parse_hex_string(unsigned char *, size_t, const char *); +int parsefile(const char *); void parse(char *, int); void parse_baddynamic(int *, size_t, char *, void **, size_t *, int, int); void usage(void); @@ -220,8 +221,9 @@ int main(int argc, char *argv[]) { int ch, lvl1; + int fflag; - while ((ch = getopt(argc, argv, "Aanqw")) != -1) { + while ((ch = getopt(argc, argv, "Aafnqw")) != -1) { switch (ch) { case 'A': @@ -232,6 +234,10 @@ main(int argc, char *argv[]) aflag = 1; break; + case 'f': + fflag = 1; + break; + case 'n': nflag = 1; break; @@ -251,6 +257,13 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; + if (fflag) { + if (argc != 1) + usage(); + + return (parsefile(argv[0])); + } + if (argc == 0 || (Aflag || aflag)) { debuginit(); vfsinit(); @@ -335,6 +348,42 @@ parse_hex_string(unsigned char *dst, siz return (len); } +int +parsefile(const char *file) +{ + FILE *f; + char *line = NULL; + size_t linesize = 0; + ssize_t linelen; + + if (strcmp(file, "-") == 0) + f = stdin; + else { + f = fopen(file, "r"); + if (f == NULL) + err(1, "open %s", file); + } + + while ((linelen = getline(&line, &linesize, f)) != -1) { + char *c; + + c = strchr(line, '#'); + if (c != NULL) + *c = '\0'; + + if (strlen(line) == 0) + continue; + + line[linelen - 1] = '\0'; + parse(line, 1); + } + if (ferror(f)) + err(1, "read %s", file); + + /* all done */ + return (0); +} + /* * Parse a name into a MIB entry. * Lookup and print out the MIB entry if it exists. @@ -2725,6 +2774,7 @@ usage(void) (void)fprintf(stderr, "usage: sysctl [-Aan]\n" " sysctl [-n] name ...\n" - " sysctl [-nq] name=value ...\n"); + " sysctl [-nq] name=value ...\n" + " sysctl [-nq] -f file\n"); exit(1); }