aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c42
-rw-r--r--lib/lib.h8
2 files changed, 31 insertions, 19 deletions
diff --git a/lib/lib.c b/lib/lib.c
index c16cffe4..27305ec9 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -866,27 +866,37 @@ void names_to_pid(char **names, int (*callback)(pid_t pid, char *name))
closedir(dp);
}
-// display first few digits of number with power of two units, except we're
-// actually just counting decimal digits and showing mil/bil/trillions.
+// display first few digits of number with power of two units
int human_readable(char *buf, unsigned long long num, int style)
{
- int end, len;
-
- len = sprintf(buf, "%lld", num)-1;
- end = (len%3)+1;
- len /= 3;
+ unsigned long long snap = 0;
+ int len, unit, divisor = (style&HR_SI) ? 1024 : 1000;
+
+ // 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;
+ len = sprintf(buf, "%llu", num);
+ if (unit && len == 1) {
+ // Redo rounding for 1.2M case, this works with HR_SI and not.
+ num = snap/divisor;
+ snap -= num*divisor;
+ snap = ((snap*100)+50)/divisor;
+ snap /= 10;
+ len = sprintf(buf, "%llu.%llu", num, snap);
+ }
+ if (style & HR_SPACE) buf[len++] = ' ';
+ if (unit) {
+ unit = " kMGTPE"[unit];
- if (len && end == 1) {
- buf[2] = buf[1];
- buf[1] = '.';
- end = 3;
+ if (!(style&HR_SI)) unit = toupper(unit);
+ buf[len++] = unit;
}
- if (style & HR_SPACE) buf[end++] = ' ';
- if (len) buf[end++] = " KMGTPE"[len];
- if (style & HR_B) buf[end++] = 'B';
- buf[end++] = 0;
+ if (style & HR_B) buf[len++] = 'B';
+ buf[len] = 0;
- return end;
+ return len;
}
// The qsort man page says you can use alphasort, the posix committee
diff --git a/lib/lib.h b/lib/lib.h
index 17a4a974..41f38d1b 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -177,12 +177,14 @@ void replace_tempfile(int fdin, int fdout, char **tempname);
void crc_init(unsigned int *crc_table, int little_endian);
void base64_init(char *p);
int yesno(char *prompt, int def);
-#define HR_SPACE 1
-#define HR_B 2
-int human_readable(char *buf, unsigned long long num, int style);
int qstrcmp(const void *a, const void *b);
int xpoll(struct pollfd *fds, int nfds, int timeout);
+#define HR_SPACE 1
+#define HR_B 2
+#define HR_SI 4
+int human_readable(char *buf, unsigned long long num, int style);
+
// interestingtimes.c
int xgettty(void);
int terminal_size(unsigned *xx, unsigned *yy);