From cc86362753f762b4ac325999fe17e5d922715c2a Mon Sep 17 00:00:00 2001 From: des Date: Fri, 7 Nov 2003 16:33:45 +0000 Subject: 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. --- sbin/sysctl/sysctl.8 | 6 ++++-- sbin/sysctl/sysctl.c | 32 +++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 13 deletions(-) (limited to 'sbin/sysctl') diff --git a/sbin/sysctl/sysctl.8 b/sbin/sysctl/sysctl.8 index 5f9f7de..c938bce 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 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 diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c index e373b64..fa26222 100644 --- a/sbin/sysctl/sysctl.c +++ b/sbin/sysctl/sysctl.c @@ -58,12 +58,13 @@ static const char rcsid[] = #include #include #include +#include #include #include #include #include -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); -- cgit v1.1