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
This commit is contained in:
Anton Berezin 2001-10-30 20:15:32 +00:00
parent 646d372751
commit d0b8aabb19
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=85747
2 changed files with 31 additions and 13 deletions

View File

@ -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

View File

@ -58,7 +58,7 @@ static const char rcsid[] =
#include <string.h>
#include <unistd.h>
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++);