From d0b8aabb194d6dd21acf4b97b78d363cda33d66e Mon Sep 17 00:00:00 2001 From: Anton Berezin Date: Tue, 30 Oct 2001 20:15:32 +0000 Subject: [PATCH] Implement -e option. It modifies the output produced by sysctl(8) in such a way that the name and the value of the variable(s) are separated with `=' instead of the usual `: '. This is useful for producing output that can be fed back to the sysctl utility (pasted to sysctl.conf, for example). Reviewed by: rwatson Approved by: markm MFC after: 2 weeks --- sbin/sysctl/sysctl.8 | 14 ++++++++++++-- sbin/sysctl/sysctl.c | 30 +++++++++++++++++++----------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/sbin/sysctl/sysctl.8 b/sbin/sysctl/sysctl.8 index bd5f1f254aa0..a58cfd02025f 100644 --- a/sbin/sysctl/sysctl.8 +++ b/sbin/sysctl/sysctl.8 @@ -40,11 +40,11 @@ .Nd get or set kernel state .Sh SYNOPSIS .Nm -.Op Fl bNnox +.Op Fl beNnox .Ar name Ns Op = Ns Ar value .Ar ... .Nm -.Op Fl bNnox +.Op Fl beNnox .Fl a .Sh DESCRIPTION The @@ -71,6 +71,16 @@ the command line. Force the value of the variable(s) to be output in raw, binary format. No names are printed and no terminating newlines are output. This is mostly useful with a single variable. +.It Fl e +Separate the name and the value of the variable(s) with `='. +This is useful for producing output which can be fed back to the +.Nm +utility. +This option is ignored if either +.Fl N +or +.Fl n +is specified, or a variable is being set. .It Fl N Show only variable names, not their values. This is particularly useful with shells that offer programmable diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c index 1d225500a4cd..5ffb810a4b5b 100644 --- a/sbin/sysctl/sysctl.c +++ b/sbin/sysctl/sysctl.c @@ -58,7 +58,7 @@ static const char rcsid[] = #include #include -static int aflag, bflag, Nflag, nflag, oflag, xflag; +static int aflag, bflag, eflag, Nflag, nflag, oflag, xflag; static int oidfmt(int *, int, char *, u_int *); static void parse(char *); @@ -71,8 +71,8 @@ usage(void) { (void)fprintf(stderr, "%s\n%s\n", - "usage: sysctl [-bNnox] variable[=value] ...", - " sysctl [-bNnox] -a"); + "usage: sysctl [-beNnox] variable[=value] ...", + " sysctl [-beNnox] -a"); exit(1); } @@ -83,7 +83,7 @@ main(int argc, char **argv) setbuf(stdout,0); setbuf(stderr,0); - while ((ch = getopt(argc, argv, "AabNnowxX")) != -1) { + while ((ch = getopt(argc, argv, "AabeNnowxX")) != -1) { switch (ch) { case 'A': /* compatibility */ @@ -95,6 +95,9 @@ main(int argc, char **argv) case 'b': bflag = 1; break; + case 'e': + eflag = 1; + break; case 'N': Nflag = 1; break; @@ -380,7 +383,7 @@ static int show_var(int *oid, int nlen) { u_char buf[BUFSIZ], *val, *p; - char name[BUFSIZ], *fmt; + char name[BUFSIZ], *fmt, *sep; int qoid[CTL_MAXNAME+2]; int i; size_t j, len; @@ -401,6 +404,11 @@ show_var(int *oid, int nlen) return (0); } + if (eflag) + sep = "="; + else + sep = ": "; + /* find an estimate of how much we need for this var */ j = 0; i = sysctl(oid, nlen, 0, &j, 0, 0); @@ -431,13 +439,13 @@ show_var(int *oid, int nlen) switch (*fmt) { case 'A': if (!nflag) - printf("%s: ", name); + printf("%s%s", name, sep); printf("%s", p); return (0); case 'I': if (!nflag) - printf("%s: ", name); + printf("%s%s", name, sep); fmt++; val = ""; while (len >= sizeof(int)) { @@ -453,7 +461,7 @@ show_var(int *oid, int nlen) case 'L': if (!nflag) - printf("%s: ", name); + printf("%s%s", name, sep); fmt++; val = ""; while (len >= sizeof(long)) { @@ -469,7 +477,7 @@ show_var(int *oid, int nlen) case 'P': if (!nflag) - printf("%s: ", name); + printf("%s%s", name, sep); printf("%p", *(void **)p); return (0); @@ -488,7 +496,7 @@ show_var(int *oid, int nlen) func = NULL; if (func) { if (!nflag) - printf("%s: ", name); + printf("%s%s", name, sep); return ((*func)(len, p)); } /* FALL THROUGH */ @@ -496,7 +504,7 @@ show_var(int *oid, int nlen) if (!oflag && !xflag) return (1); if (!nflag) - printf("%s: ", name); + printf("%s%s", name, sep); printf("Format:%s Length:%d Dump:0x", fmt, len); while (len-- && (xflag || p < val + 16)) printf("%02x", *p++);