From ec5e6e12ee6d7047ab013f3a2826abdb52666ea0 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 4 Sep 2020 01:04:43 -0500 Subject: Add commas to top display, and adjust memory units to megabytes if >10G RAM. --- lib/lib.c | 29 ++++++++++++++++++++++------- lib/lib.h | 10 ++++++---- 2 files changed, 28 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/lib.c b/lib/lib.c index 7fe3611e..28c7e9c4 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -1148,18 +1148,33 @@ match: } // 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) +int human_readable_long(char *buf, unsigned long long num, int dgt, int unit, + int style) { unsigned long long snap = 0; - int len, unit, divisor = (style&HR_1000) ? 1000 : 1024; + int len, commas = 0, off, ii, 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 1<<64 is 18 exabytes. - for (unit = 0; snprintf(0, 0, "%llu", num)>dgt; unit++) + // The largest unit we can detect is 1<<64 = 18 Exabytes, but we added + // Zettabyte and Yottabyte in case "unit" starts above zero. + for (;;unit++) { + len = snprintf(0, 0, "%llu", num); + if (style&HR_COMMAS) commas = (len>4)*((len-1)/3); + if (len<=(dgt-commas)) break; num = ((snap = num)+(divisor/2))/divisor; + } + if (CFG_TOYBOX_DEBUG && unit>8) return sprintf(buf, "%.*s", dgt, "TILT"); + len = sprintf(buf, "%llu", num); - if (unit && len == 1) { + if (commas) { + for (ii = 0; ii