Add a command-line option to format output for human readability.

Currently, the only effect it has is to print some (but not all) numbers
using thousands separators.
This commit is contained in:
Dag-Erling Smørgrav 2003-11-07 16:33:45 +00:00
parent 0373534163
commit 45817aaa9c
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=122233
2 changed files with 25 additions and 13 deletions

View File

@ -40,11 +40,11 @@
.Nd get or set kernel state .Nd get or set kernel state
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl beNnox .Op Fl bedhNnox
.Ar name Ns Op = Ns Ar value .Ar name Ns Op = Ns Ar value
.Ar ... .Ar ...
.Nm .Nm
.Op Fl bdeNnox .Op Fl bdehNnox
.Fl a .Fl a
.Sh DESCRIPTION .Sh DESCRIPTION
The The
@ -84,6 +84,8 @@ This option is ignored if either
or or
.Fl n .Fl n
is specified, or a variable is being set. is specified, or a variable is being set.
.It Fl h
Format output for human, rather than machine, readability.
.It Fl N .It Fl N
Show only variable names, not their values. Show only variable names, not their values.
This is particularly useful with shells that offer programmable This is particularly useful with shells that offer programmable

View File

@ -58,12 +58,13 @@ static const char rcsid[] =
#include <ctype.h> #include <ctype.h>
#include <err.h> #include <err.h>
#include <errno.h> #include <errno.h>
#include <locale.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
static int aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag; static int aflag, bflag, dflag, eflag, hflag, Nflag, nflag, oflag, 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 *);
@ -78,8 +79,8 @@ usage(void)
{ {
(void)fprintf(stderr, "%s\n%s\n", (void)fprintf(stderr, "%s\n%s\n",
"usage: sysctl [-bdeNnox] variable[=value] ...", "usage: sysctl [-bdehNnox] variable[=value] ...",
" sysctl [-bdeNnox] -a"); " sysctl [-bdehNnox] -a");
exit(1); exit(1);
} }
@ -87,10 +88,12 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
int ch; int ch;
setlocale(LC_NUMERIC, "");
setbuf(stdout,0); setbuf(stdout,0);
setbuf(stderr,0); setbuf(stderr,0);
while ((ch = getopt(argc, argv, "AabdeNnowxX")) != -1) { while ((ch = getopt(argc, argv, "AabdehNnowxX")) != -1) {
switch (ch) { switch (ch) {
case 'A': case 'A':
/* compatibility */ /* compatibility */
@ -108,6 +111,9 @@ main(int argc, char **argv)
case 'e': case 'e':
eflag = 1; eflag = 1;
break; break;
case 'h':
hflag = 1;
break;
case 'N': case 'N':
Nflag = 1; Nflag = 1;
break; break;
@ -305,7 +311,8 @@ S_clockinfo(int l2, void *p)
warnx("S_clockinfo %d != %d", l2, sizeof(*ci)); warnx("S_clockinfo %d != %d", l2, sizeof(*ci));
return (0); return (0);
} }
printf("{ hz = %d, tick = %d, profhz = %d, stathz = %d }", printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
"{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
ci->hz, ci->tick, ci->profhz, ci->stathz); ci->hz, ci->tick, ci->profhz, ci->stathz);
return (0); return (0);
} }
@ -319,7 +326,7 @@ S_loadavg(int l2, void *p)
warnx("S_loadavg %d != %d", l2, sizeof(*tv)); warnx("S_loadavg %d != %d", l2, sizeof(*tv));
return (0); return (0);
} }
printf("{ %.2f %.2f %.2f }", printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
(double)tv->ldavg[0]/(double)tv->fscale, (double)tv->ldavg[0]/(double)tv->fscale,
(double)tv->ldavg[1]/(double)tv->fscale, (double)tv->ldavg[1]/(double)tv->fscale,
(double)tv->ldavg[2]/(double)tv->fscale); (double)tv->ldavg[2]/(double)tv->fscale);
@ -337,7 +344,8 @@ S_timeval(int l2, void *p)
warnx("S_timeval %d != %d", l2, sizeof(*tv)); warnx("S_timeval %d != %d", l2, sizeof(*tv));
return (0); return (0);
} }
printf("{ sec = %ld, usec = %ld } ", printf(hflag ? "{ sec = %'ld, usec = %'ld } " :
"{ sec = %ld, usec = %ld } ",
tv->tv_sec, tv->tv_usec); tv->tv_sec, tv->tv_usec);
tv_sec = tv->tv_sec; tv_sec = tv->tv_sec;
p1 = strdup(ctime(&tv_sec)); p1 = strdup(ctime(&tv_sec));
@ -555,10 +563,11 @@ show_var(int *oid, int nlen)
fmt++; fmt++;
val = ""; val = "";
while (len >= sizeof(int)) { while (len >= sizeof(int)) {
fputs(val, stdout);
if(*fmt == 'U') if(*fmt == 'U')
printf("%s%u", val, *(unsigned int *)p); printf(hflag ? "%'u" : "%u", *(unsigned int *)p);
else else
printf("%s%d", val, *(int *)p); printf(hflag ? "%'d" : "%d", *(int *)p);
val = " "; val = " ";
len -= sizeof(int); len -= sizeof(int);
p += sizeof(int); p += sizeof(int);
@ -571,10 +580,11 @@ show_var(int *oid, int nlen)
fmt++; fmt++;
val = ""; val = "";
while (len >= sizeof(long)) { while (len >= sizeof(long)) {
fputs(val, stdout);
if(*fmt == 'U') if(*fmt == 'U')
printf("%s%lu", val, *(unsigned long *)p); printf(hflag ? "%'lu" : "%lu", *(unsigned long *)p);
else else
printf("%s%ld", val, *(long *)p); printf(hflag ? "%'ld" : "%ld", *(long *)p);
val = " "; val = " ";
len -= sizeof(long); len -= sizeof(long);
p += sizeof(long); p += sizeof(long);