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
2 changed files with 25 additions and 13 deletions

View File

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

View File

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