aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c16
-rw-r--r--lib/lib.h1
2 files changed, 12 insertions, 5 deletions
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.)
diff --git a/lib/lib.h b/lib/lib.h
index 707e388c..5ce29380 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -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