From 960100aa9cb588a73125d43ec0b0a7152a95b43f Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 6 Sep 2015 20:10:04 -0500 Subject: Switch HR_SI to HR_1000, make binary the default, make HR_B only affect bytes, and update the tests. --- lib/lib.c | 9 ++++----- lib/lib.h | 8 ++++---- tests/test_human_readable.test | 17 +++++++++++++---- toys/posix/df.c | 2 +- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 27305ec9..058c6386 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -870,7 +870,7 @@ void names_to_pid(char **names, int (*callback)(pid_t pid, char *name)) int human_readable(char *buf, unsigned long long num, int style) { unsigned long long snap = 0; - int len, unit, divisor = (style&HR_SI) ? 1024 : 1000; + 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. @@ -879,7 +879,7 @@ int human_readable(char *buf, unsigned long long num, int style) 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. + // Redo rounding for 1.2M case, this works with and without HR_1000. num = snap/divisor; snap -= num*divisor; snap = ((snap*100)+50)/divisor; @@ -890,10 +890,9 @@ int human_readable(char *buf, unsigned long long num, int style) if (unit) { unit = " kMGTPE"[unit]; - if (!(style&HR_SI)) unit = toupper(unit); + if (!(style&HR_1000)) unit = toupper(unit); buf[len++] = unit; - } - if (style & HR_B) buf[len++] = 'B'; + } else if (style & HR_B) buf[len++] = 'B'; buf[len] = 0; return len; diff --git a/lib/lib.h b/lib/lib.h index 41f38d1b..5bfba22a 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -176,13 +176,13 @@ void delete_tempfile(int fdin, int fdout, char **tempname); 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); +int yesno(int def); 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 +#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(char *buf, unsigned long long num, int style); // interestingtimes.c diff --git a/tests/test_human_readable.test b/tests/test_human_readable.test index a66b4fda..a7533b7f 100755 --- a/tests/test_human_readable.test +++ b/tests/test_human_readable.test @@ -4,7 +4,16 @@ #testing "name" "command" "result" "infile" "stdin" -testing "human_readable" "test_human_readable 123456789" "123M\n" "" "" -testing "human_readable -b" "test_human_readable -b 123456789" "123MB\n" "" "" -testing "human_readable -s" "test_human_readable -s 123456789" "123 M\n" "" "" -testing "human_readable -i" "test_human_readable -i 5675" "5.5k\n" "" "" +testing "human_readable l 1024" "test_human_readable 123456789" "118M\n" "" "" +testing "human_readable l 1000" "test_human_readable -i 123456789" "123M\n" "" "" +testing "human_readable s 1024" "test_human_readable 5675" "5.5K\n" "" "" +testing "human_readable s 1000" "test_human_readable -i 5675" "5.6k\n" "" "" + +# An example input where we give a better result than coreutils. +# 267350/1024=261.08. We say 261K and coreutils says 262K. +testing "human_readable" "test_human_readable 267350" "261K\n" "" "" + +testing "human_readable -b" "test_human_readable -b 123" "123B\n" "" "" +testing "human_readable -b" "test_human_readable -b 123456789" "118M\n" "" "" +testing "human_readable -s" "test_human_readable -s 123456789" "118 M\n" "" "" +testing "human_readable -bs" "test_human_readable -bs 123456789" "118 M\n" "" "" diff --git a/toys/posix/df.c b/toys/posix/df.c index 8fc26eca..bc153615 100644 --- a/toys/posix/df.c +++ b/toys/posix/df.c @@ -77,7 +77,7 @@ static void show_mt(struct mtab_list *mt) if (len < 1) len = 1; if (toys.optflags & (FLAG_H|FLAG_h)) { char *size_str = toybuf, *used_str = toybuf+64, *avail_str = toybuf+128; - int hr_flags = (toys.optflags & FLAG_H) ? HR_SI : 0; + int hr_flags = (toys.optflags & FLAG_H) ? HR_1000 : 0; human_readable(size_str, size, hr_flags); human_readable(used_str, used, hr_flags); -- cgit v1.2.3