Clean up option handling a little.

Add an option for showing sysctl descriptions instead of their values.
This commit is contained in:
des 1999-01-10 02:10:08 +00:00
parent 9efaefb948
commit cd585d50b4
4 changed files with 80 additions and 42 deletions

View File

@ -30,7 +30,7 @@
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" From: @(#)sysctl.8 8.1 (Berkeley) 6/6/93 .\" From: @(#)sysctl.8 8.1 (Berkeley) 6/6/93
.\" $Id: sysctl.8,v 1.16 1998/07/29 08:30:37 joerg Exp $ .\" $Id: sysctl.8,v 1.17 1998/09/29 02:01:06 jkoshy Exp $
.\" .\"
.Dd September 23, 1994 .Dd September 23, 1994
.Dt SYSCTL 8 .Dt SYSCTL 8
@ -40,14 +40,14 @@
.Nd get or set kernel state .Nd get or set kernel state
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm sysctl .Nm sysctl
.Op Fl bn .Op Fl bdn
.Ar name ... .Ar name ...
.Nm sysctl .Nm sysctl
.Op Fl bn .Op Fl bn
.Fl w .Fl w
.Ar name=value ... .Ar name=value ...
.Nm sysctl .Nm sysctl
.Op Fl bn .Op Fl bdn
.Fl aAX .Fl aAX
.Sh DESCRIPTION .Sh DESCRIPTION
The The
@ -85,6 +85,9 @@ set psize=`sysctl -n hw.pagesize`
Force the value of the variable(s) to be output in raw, binary Force the value of the variable(s) to be output in raw, binary
format. No names are printed and no terminating newlines are output. format. No names are printed and no terminating newlines are output.
This is mostly useful with a single variable. This is mostly useful with a single variable.
.It Fl d
Display the description rather than the value of the requested
variable(s).
.It Fl w Ar name=value ... .It Fl w Ar name=value ...
Set the MIB Set the MIB
.Ar name .Ar name

View File

@ -42,7 +42,7 @@ static const char copyright[] =
static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93"; static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
#endif #endif
static const char rcsid[] = static const char rcsid[] =
"$Id: sysctl.c,v 1.18 1998/08/25 07:38:19 dfr Exp $"; "$Id: sysctl.c,v 1.19 1998/11/08 19:27:43 phk Exp $";
#endif /* not lint */ #endif /* not lint */
#include <sys/types.h> #include <sys/types.h>
@ -58,7 +58,7 @@ static const char rcsid[] =
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
static int Aflag, aflag, nflag, wflag, Xflag, bflag; static int Aflag, aflag, bflag, dflag, nflag, wflag, Xflag;
static int oidfmt(int *, int, char *, u_int *); static int oidfmt(int *, int, char *, u_int *);
static void parse(char *); static void parse(char *);
@ -70,11 +70,12 @@ static void
usage(void) usage(void)
{ {
(void)fprintf(stderr, "%s\n%s\n%s\n%s\n", (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
"usage: sysctl [-bnX] variable ...", "usage: sysctl [-bdn] variable ...",
" sysctl [-bnX] -w variable=value ...", " sysctl [-bn] -w variable=value ...",
" sysctl [-bnX] -a", " sysctl [-bdn] -a",
" sysctl [-bnX] -A"); " sysctl [-bdn] -A",
" sysctl [-bdn] -X");
exit(1); exit(1);
} }
@ -85,11 +86,12 @@ main(int argc, char **argv)
setbuf(stdout,0); setbuf(stdout,0);
setbuf(stderr,0); setbuf(stderr,0);
while ((ch = getopt(argc, argv, "AabnwX")) != -1) { while ((ch = getopt(argc, argv, "AabdnwX")) != -1) {
switch (ch) { switch (ch) {
case 'A': Aflag = 1; break; case 'A': Aflag = 1; break;
case 'a': aflag = 1; break; case 'a': aflag = 1; break;
case 'b': bflag = 1; break; case 'b': bflag = 1; break;
case 'd': dflag = 1; break;
case 'n': nflag = 1; break; case 'n': nflag = 1; break;
case 'w': wflag = 1; break; case 'w': wflag = 1; break;
case 'X': Xflag = Aflag = 1; break; case 'X': Xflag = Aflag = 1; break;
@ -99,6 +101,8 @@ main(int argc, char **argv)
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (wflag && (Aflag || aflag || dflag))
usage();
if (Aflag || aflag) if (Aflag || aflag)
exit (sysctl_all(0, 0)); exit (sysctl_all(0, 0));
if (argc == 0) if (argc == 0)
@ -336,13 +340,34 @@ static int
show_var(int *oid, int nlen) show_var(int *oid, int nlen)
{ {
u_char buf[BUFSIZ], *val, *p; u_char buf[BUFSIZ], *val, *p;
char name[BUFSIZ], *fmt; char name[BUFSIZ], descr[BUFSIZ], *fmt;
int qoid[CTL_MAXNAME+2]; int qoid[CTL_MAXNAME+2];
int i; int i;
size_t j, len; size_t j, len;
u_int kind; u_int kind;
int (*func)(int, void *) = 0; int (*func)(int, void *) = 0;
qoid[0] = 0;
memcpy(qoid + 2, oid, nlen * sizeof(int));
qoid[1] = 1;
j = sizeof name;
i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
if (i || !j)
err(1, "sysctl name %d %d %d", i, j, errno);
if (dflag) {
qoid[1] = 5;
j = sizeof descr;
i = sysctl(qoid, nlen + 2, descr, &j, 0, 0);
if (i || !j)
err(1, "sysctl name %d %d %d", i, j, errno);
if (!nflag)
printf("%s: ", name);
printf("%s", descr[0] ? descr : "[no description]");
return (0);
}
/* find an estimate of how much we need for this var */ /* find an estimate of how much we need for this var */
j = 0; j = 0;
i = sysctl(oid, nlen, 0, &j, 0, 0); i = sysctl(oid, nlen, 0, &j, 0, 0);
@ -359,10 +384,7 @@ show_var(int *oid, int nlen)
return (0); return (0);
} }
qoid[0] = 0;
qoid[1] = 4; qoid[1] = 4;
memcpy(qoid + 2, oid, nlen * sizeof(int));
j = sizeof buf; j = sizeof buf;
i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
if (i || !j) if (i || !j)
@ -372,12 +394,6 @@ show_var(int *oid, int nlen)
fmt = (char *)(buf + sizeof(u_int)); fmt = (char *)(buf + sizeof(u_int));
qoid[1] = 1;
j = sizeof name;
i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
if (i || !j)
err(1, "sysctl name %d %d %d", i, j, errno);
p = val; p = val;
switch (*fmt) { switch (*fmt) {
case 'A': case 'A':

View File

@ -30,7 +30,7 @@
.\" SUCH DAMAGE. .\" SUCH DAMAGE.
.\" .\"
.\" From: @(#)sysctl.8 8.1 (Berkeley) 6/6/93 .\" From: @(#)sysctl.8 8.1 (Berkeley) 6/6/93
.\" $Id: sysctl.8,v 1.16 1998/07/29 08:30:37 joerg Exp $ .\" $Id: sysctl.8,v 1.17 1998/09/29 02:01:06 jkoshy Exp $
.\" .\"
.Dd September 23, 1994 .Dd September 23, 1994
.Dt SYSCTL 8 .Dt SYSCTL 8
@ -40,14 +40,14 @@
.Nd get or set kernel state .Nd get or set kernel state
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm sysctl .Nm sysctl
.Op Fl bn .Op Fl bdn
.Ar name ... .Ar name ...
.Nm sysctl .Nm sysctl
.Op Fl bn .Op Fl bn
.Fl w .Fl w
.Ar name=value ... .Ar name=value ...
.Nm sysctl .Nm sysctl
.Op Fl bn .Op Fl bdn
.Fl aAX .Fl aAX
.Sh DESCRIPTION .Sh DESCRIPTION
The The
@ -85,6 +85,9 @@ set psize=`sysctl -n hw.pagesize`
Force the value of the variable(s) to be output in raw, binary Force the value of the variable(s) to be output in raw, binary
format. No names are printed and no terminating newlines are output. format. No names are printed and no terminating newlines are output.
This is mostly useful with a single variable. This is mostly useful with a single variable.
.It Fl d
Display the description rather than the value of the requested
variable(s).
.It Fl w Ar name=value ... .It Fl w Ar name=value ...
Set the MIB Set the MIB
.Ar name .Ar name

View File

@ -42,7 +42,7 @@ static const char copyright[] =
static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93"; static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
#endif #endif
static const char rcsid[] = static const char rcsid[] =
"$Id: sysctl.c,v 1.18 1998/08/25 07:38:19 dfr Exp $"; "$Id: sysctl.c,v 1.19 1998/11/08 19:27:43 phk Exp $";
#endif /* not lint */ #endif /* not lint */
#include <sys/types.h> #include <sys/types.h>
@ -58,7 +58,7 @@ static const char rcsid[] =
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
static int Aflag, aflag, nflag, wflag, Xflag, bflag; static int Aflag, aflag, bflag, dflag, nflag, wflag, Xflag;
static int oidfmt(int *, int, char *, u_int *); static int oidfmt(int *, int, char *, u_int *);
static void parse(char *); static void parse(char *);
@ -70,11 +70,12 @@ static void
usage(void) usage(void)
{ {
(void)fprintf(stderr, "%s\n%s\n%s\n%s\n", (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
"usage: sysctl [-bnX] variable ...", "usage: sysctl [-bdn] variable ...",
" sysctl [-bnX] -w variable=value ...", " sysctl [-bn] -w variable=value ...",
" sysctl [-bnX] -a", " sysctl [-bdn] -a",
" sysctl [-bnX] -A"); " sysctl [-bdn] -A",
" sysctl [-bdn] -X");
exit(1); exit(1);
} }
@ -85,11 +86,12 @@ main(int argc, char **argv)
setbuf(stdout,0); setbuf(stdout,0);
setbuf(stderr,0); setbuf(stderr,0);
while ((ch = getopt(argc, argv, "AabnwX")) != -1) { while ((ch = getopt(argc, argv, "AabdnwX")) != -1) {
switch (ch) { switch (ch) {
case 'A': Aflag = 1; break; case 'A': Aflag = 1; break;
case 'a': aflag = 1; break; case 'a': aflag = 1; break;
case 'b': bflag = 1; break; case 'b': bflag = 1; break;
case 'd': dflag = 1; break;
case 'n': nflag = 1; break; case 'n': nflag = 1; break;
case 'w': wflag = 1; break; case 'w': wflag = 1; break;
case 'X': Xflag = Aflag = 1; break; case 'X': Xflag = Aflag = 1; break;
@ -99,6 +101,8 @@ main(int argc, char **argv)
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (wflag && (Aflag || aflag || dflag))
usage();
if (Aflag || aflag) if (Aflag || aflag)
exit (sysctl_all(0, 0)); exit (sysctl_all(0, 0));
if (argc == 0) if (argc == 0)
@ -336,13 +340,34 @@ static int
show_var(int *oid, int nlen) show_var(int *oid, int nlen)
{ {
u_char buf[BUFSIZ], *val, *p; u_char buf[BUFSIZ], *val, *p;
char name[BUFSIZ], *fmt; char name[BUFSIZ], descr[BUFSIZ], *fmt;
int qoid[CTL_MAXNAME+2]; int qoid[CTL_MAXNAME+2];
int i; int i;
size_t j, len; size_t j, len;
u_int kind; u_int kind;
int (*func)(int, void *) = 0; int (*func)(int, void *) = 0;
qoid[0] = 0;
memcpy(qoid + 2, oid, nlen * sizeof(int));
qoid[1] = 1;
j = sizeof name;
i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
if (i || !j)
err(1, "sysctl name %d %d %d", i, j, errno);
if (dflag) {
qoid[1] = 5;
j = sizeof descr;
i = sysctl(qoid, nlen + 2, descr, &j, 0, 0);
if (i || !j)
err(1, "sysctl name %d %d %d", i, j, errno);
if (!nflag)
printf("%s: ", name);
printf("%s", descr[0] ? descr : "[no description]");
return (0);
}
/* find an estimate of how much we need for this var */ /* find an estimate of how much we need for this var */
j = 0; j = 0;
i = sysctl(oid, nlen, 0, &j, 0, 0); i = sysctl(oid, nlen, 0, &j, 0, 0);
@ -359,10 +384,7 @@ show_var(int *oid, int nlen)
return (0); return (0);
} }
qoid[0] = 0;
qoid[1] = 4; qoid[1] = 4;
memcpy(qoid + 2, oid, nlen * sizeof(int));
j = sizeof buf; j = sizeof buf;
i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
if (i || !j) if (i || !j)
@ -372,12 +394,6 @@ show_var(int *oid, int nlen)
fmt = (char *)(buf + sizeof(u_int)); fmt = (char *)(buf + sizeof(u_int));
qoid[1] = 1;
j = sizeof name;
i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
if (i || !j)
err(1, "sysctl name %d %d %d", i, j, errno);
p = val; p = val;
switch (*fmt) { switch (*fmt) {
case 'A': case 'A':