diff options
author | Rob Landley <rob@landley.net> | 2019-05-16 17:58:43 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2019-05-16 17:58:43 -0500 |
commit | 0536f45747a991a21b2b544f556da704eed22bd4 (patch) | |
tree | c1a131f9b2aee09c01e96afb9d96ddb34d68766c /lib | |
parent | 506b264702008c470492bbe0a27085d25fd24878 (diff) | |
download | toybox-0536f45747a991a21b2b544f556da704eed22bd4.tar.gz |
Add human_readable_long() for more than 3 digits of output.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lib.c | 16 | ||||
-rw-r--r-- | lib/lib.h | 1 |
2 files changed, 12 insertions, 5 deletions
@@ -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.) @@ -269,6 +269,7 @@ void loggit(int priority, char *format, ...); #define HR_SPACE 1 // Space between number and units #define HR_B 2 // Use "B" for single byte units #define HR_1000 4 // Use decimal instead of binary units +int human_readable_long(char *buf, unsigned long long num, int dgt, int style); int human_readable(char *buf, unsigned long long num, int style); // env.c |