diff options
author | Martijn van Duren <martijn@cvs.openbsd.org> | 2020-01-12 20:51:09 +0000 |
---|---|---|
committer | Martijn van Duren <martijn@cvs.openbsd.org> | 2020-01-12 20:51:09 +0000 |
commit | 0e2492cff7104d3ccbac5039182fedfbe0c0fa17 (patch) | |
tree | 9aeafb82a66821607fd64e404f77ea1cbbff6ee1 /usr.bin | |
parent | 8eb3d8fcef93ac71e0f33b43ff39e21869bdc10c (diff) |
Add support for human readable numbers where applicable.
OK stsp@ and bluhm@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/systat/engine.c | 51 | ||||
-rw-r--r-- | usr.bin/systat/engine.h | 3 | ||||
-rw-r--r-- | usr.bin/systat/main.c | 13 | ||||
-rw-r--r-- | usr.bin/systat/systat.1 | 10 |
4 files changed, 40 insertions, 37 deletions
diff --git a/usr.bin/systat/engine.c b/usr.bin/systat/engine.c index 64ac38b470f..5adcdb1922b 100644 --- a/usr.bin/systat/engine.c +++ b/usr.bin/systat/engine.c @@ -1,4 +1,4 @@ -/* $OpenBSD: engine.c,v 1.24 2019/07/19 15:40:11 solene Exp $ */ +/* $OpenBSD: engine.c,v 1.25 2020/01/12 20:51:08 martijn Exp $ */ /* * Copyright (c) 2001, 2007 Can Erkin Acar <canacar@openbsd.org> * @@ -52,6 +52,7 @@ struct view_ent { useconds_t udelay = 5000000; int dispstart = 0; +int humanreadable = 0; int interactive = 1; int averageonly = 0; int maxprint = 0; @@ -718,6 +719,8 @@ void print_fld_sdiv(field_def *fld, u_int64_t size, int d) { int len; + char *mult = "KMGTPE"; + int i = -1; if (fld == NULL) return; @@ -726,37 +729,25 @@ print_fld_sdiv(field_def *fld, u_int64_t size, int d) if (len < 1) return; - tb_start(); - if (tbprintft("%llu", size) <= len) - goto ok; - - tb_start(); - size /= d; - if (tbprintft("%lluK", size) <= len) - goto ok; - if (size == 0) - goto err; - - tb_start(); - size /= d; - if (tbprintft("%lluM", size) <= len) - goto ok; - if (size == 0) - goto err; - - tb_start(); - size /= d; - if (tbprintft("%lluG", size) <= len) - goto ok; - if (size == 0) + if (humanreadable) { + while (size >= 10000 && sizeof(mult) >= i + 1) { + i++; + size /= d; + } + tb_start(); + if (tbprintft("%llu%.1s", size, i == -1 ? "" : mult + i) <= len) + goto ok; goto err; - - tb_start(); - size /= d; - if (tbprintft("%lluT", size) <= len) - goto ok; - + } + do { + tb_start(); + if (tbprintft("%llu%.1s", size, i == -1 ? "" : mult + i) <= len) + goto ok; + i++; + size /= d; + } while (size != 0 && sizeof(mult) >= i); err: + tb_start(); print_fld_str(fld, "*"); tb_end(); return; diff --git a/usr.bin/systat/engine.h b/usr.bin/systat/engine.h index f4e8ede8a14..543d665783c 100644 --- a/usr.bin/systat/engine.h +++ b/usr.bin/systat/engine.h @@ -1,4 +1,4 @@ -/* $OpenBSD: engine.h,v 1.11 2019/03/04 21:23:48 dlg Exp $ */ +/* $OpenBSD: engine.h,v 1.12 2020/01/12 20:51:08 martijn Exp $ */ /* * Copyright (c) 2001, 2007 Can Erkin Acar <canacar@openbsd.org> * @@ -148,6 +148,7 @@ void foreach_view(void (*callback)(field_view *)); extern int sortdir; extern useconds_t udelay; extern int dispstart; +extern int humanreadable; extern int interactive; extern int averageonly; extern int maxprint; diff --git a/usr.bin/systat/main.c b/usr.bin/systat/main.c index 09e9f77506b..5a3321ea3aa 100644 --- a/usr.bin/systat/main.c +++ b/usr.bin/systat/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.71 2019/10/14 19:22:17 deraadt Exp $ */ +/* $OpenBSD: main.c,v 1.72 2020/01/12 20:51:08 martijn Exp $ */ /* * Copyright (c) 2001, 2007 Can Erkin Acar * Copyright (c) 2001 Daniel Hartmeier @@ -212,7 +212,7 @@ void usage(void) { extern char *__progname; - fprintf(stderr, "usage: %s [-aBbiNn] [-d count] " + fprintf(stderr, "usage: %s [-aBbhiNn] [-d count] " "[-s delay] [-w width] [view] [delay]\n", __progname); exit(1); } @@ -305,6 +305,10 @@ cmd_compat(const char *buf) need_update = 1; return; } + if (strncasecmp(buf, "human", 5) == 0) { + humanreadable = !humanreadable; + return; + } for (s = buf; *s && strchr("0123456789+-.eE", *s) != NULL; s++) ; @@ -438,7 +442,7 @@ main(int argc, char *argv[]) if (setresgid(gid, gid, gid) == -1) err(1, "setresgid"); - while ((ch = getopt(argc, argv, "BNabd:ins:w:")) != -1) { + while ((ch = getopt(argc, argv, "BNabd:hins:w:")) != -1) { switch (ch) { case 'a': maxlines = -1; @@ -457,6 +461,9 @@ main(int argc, char *argv[]) if (errstr) errx(1, "-d %s: %s", optarg, errstr); break; + case 'h': + humanreadable = 1; + break; case 'i': interactive = 1; break; diff --git a/usr.bin/systat/systat.1 b/usr.bin/systat/systat.1 index 4316e7cd943..75a8648e017 100644 --- a/usr.bin/systat/systat.1 +++ b/usr.bin/systat/systat.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: systat.1,v 1.112 2019/05/08 23:54:13 tedu Exp $ +.\" $OpenBSD: systat.1,v 1.113 2020/01/12 20:51:08 martijn Exp $ .\" $NetBSD: systat.1,v 1.6 1996/05/10 23:16:39 thorpej Exp $ .\" .\" Copyright (c) 1985, 1990, 1993 @@ -30,7 +30,7 @@ .\" .\" @(#)systat.1 8.2 (Berkeley) 12/30/93 .\" -.Dd $Mdocdate: May 8 2019 $ +.Dd $Mdocdate: January 12 2020 $ .Dt SYSTAT 1 .Os .Sh NAME @@ -38,7 +38,7 @@ .Nd display system statistics .Sh SYNOPSIS .Nm systat -.Op Fl aBbiNn +.Op Fl aBbhiNn .Op Fl d Ar count .Op Fl s Ar delay .Op Fl w Ar width @@ -101,6 +101,8 @@ with statistics displayed every update. Exit after .Ar count screen updates. +.It Fl h +Human readable mode, where applicable. .It Fl i Interactive mode. .It Fl N @@ -220,6 +222,8 @@ command interpreter. .Bl -tag -width Fl .It Ic help Print the names of the available views on the command line. +.It Ic human +Toggle human readable mode, where applicable. .It Ic order Print the names of the available orderings on the command line. .It Ic quit |