From 0536f45747a991a21b2b544f556da704eed22bd4 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 16 May 2019 17:58:43 -0500 Subject: Add human_readable_long() for more than 3 digits of output. --- lib/lib.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'lib/lib.c') diff --git a/lib/lib.c b/lib/lib.c index 7ae2472f..fe15c990 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -1136,17 +1136,17 @@ match: closedir(dp); } -// display first few digits of number with power of two units -int human_readable(char *buf, unsigned long long num, int style) +// display first "dgt" many digits of number plus unit (kilo-exabytes) +int human_readable_long(char *buf, unsigned long long num, int dgt, int style) { unsigned long long snap = 0; int len, unit, divisor = (style&HR_1000) ? 1000 : 1024; // Divide rounding up until we have 3 or fewer digits. Since the part we // print is decimal, the test is 999 even when we divide by 1024. - // We can't run out of units because 2<<64 is 18 exabytes. - // test 5675 is 5.5k not 5.6k. - for (unit = 0; num > 999; unit++) num = ((snap = num)+(divisor/2))/divisor; + // We can't run out of units because 1<<64 is 18 exabytes. + for (unit = 0; snprintf(0, 0, "%llu", num)>dgt; unit++) + num = ((snap = num)+(divisor/2))/divisor; len = sprintf(buf, "%llu", num); if (unit && len == 1) { // Redo rounding for 1.2M case, this works with and without HR_1000. @@ -1168,6 +1168,12 @@ int human_readable(char *buf, unsigned long long num, int style) return len; } +// Give 3 digit estimate + units ala 999M or 1.7T +int human_readable(char *buf, unsigned long long num, int style) +{ + return human_readable_long(buf, num, 3, style); +} + // The qsort man page says you can use alphasort, the posix committee // disagreed, and doubled down: http://austingroupbugs.net/view.php?id=142 // So just do our own. (The const is entirely to humor the stupid compiler.) -- cgit v1.2.3