aboutsummaryrefslogtreecommitdiff
path: root/libbb/human_readable.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-08-28 22:42:52 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-08-28 22:42:52 +0000
commitd66aa3c701ffb83343239e71a8b294407ff5df86 (patch)
treeeeebccfe994962aa8a33312c8726eac5adf63f5a /libbb/human_readable.c
parent3b80cac953b0627ba9b3337d3f9386678d436871 (diff)
downloadbusybox-w32-d66aa3c701ffb83343239e71a8b294407ff5df86.tar.gz
busybox-w32-d66aa3c701ffb83343239e71a8b294407ff5df86.tar.bz2
busybox-w32-d66aa3c701ffb83343239e71a8b294407ff5df86.zip
df: add support for more options, add some coreutils 6.10 compat.
by Bernhard Reutner-Fischer function old new delta df_main 664 795 +131 packed_usage 24812 24862 +50 make_human_readable_str 213 262 +49 static.ignored_mounts - 8 +8 static.unit_chars - 7 +7 static.zero_and_units 6 - -6 ------------------------------------------------------------------------------ (add/remove: 2/1 grow/shrink: 3/0 up/down: 245/-6) Total: 239 bytes
Diffstat (limited to 'libbb/human_readable.c')
-rw-r--r--libbb/human_readable.c33
1 files changed, 21 insertions, 12 deletions
diff --git a/libbb/human_readable.c b/libbb/human_readable.c
index dad26edcf..61c856750 100644
--- a/libbb/human_readable.c
+++ b/libbb/human_readable.c
@@ -32,7 +32,9 @@ const char* FAST_FUNC make_human_readable_str(unsigned long long size,
32 unsigned long block_size, unsigned long display_unit) 32 unsigned long block_size, unsigned long display_unit)
33{ 33{
34 /* The code will adjust for additional (appended) units */ 34 /* The code will adjust for additional (appended) units */
35 static const char zero_and_units[] ALIGN1 = { '0', 0, 'k', 'M', 'G', 'T' }; 35 static const char unit_chars[] ALIGN1 = {
36 '\0', 'K', 'M', 'G', 'T', 'P', 'E'
37 };
36 static const char fmt[] ALIGN1 = "%llu"; 38 static const char fmt[] ALIGN1 = "%llu";
37 static const char fmt_tenths[] ALIGN1 = "%llu.%d%c"; 39 static const char fmt_tenths[] ALIGN1 = "%llu.%d%c";
38 40
@@ -42,26 +44,33 @@ const char* FAST_FUNC make_human_readable_str(unsigned long long size,
42 int frac; 44 int frac;
43 const char *u; 45 const char *u;
44 const char *f; 46 const char *f;
47 smallint no_tenths;
45 48
46 u = zero_and_units; 49 if (size == 0)
47 f = fmt; 50 return "0";
48 frac = 0;
49 51
50 val = size * block_size; 52 /* If block_size is 0 then do not print tenths */
51 if (val == 0) { 53 no_tenths = 0;
52 return u; 54 if (block_size == 0) {
55 no_tenths = 1;
56 block_size = 1;
53 } 57 }
54 58
59 u = unit_chars;
60 val = size * block_size;
61 f = fmt;
62 frac = 0;
63
55 if (display_unit) { 64 if (display_unit) {
56 val += display_unit/2; /* Deal with rounding */ 65 val += display_unit/2; /* Deal with rounding */
57 val /= display_unit; /* Don't combine with the line above!!! */ 66 val /= display_unit; /* Don't combine with the line above!!! */
67 /* will just print it as ulonglong (below) */
58 } else { 68 } else {
59 ++u;
60 while ((val >= 1024) 69 while ((val >= 1024)
61 && (u < zero_and_units + sizeof(zero_and_units) - 1) 70 && (u < unit_chars + sizeof(unit_chars) - 1)
62 ) { 71 ) {
63 f = fmt_tenths; 72 f = fmt_tenths;
64 ++u; 73 u++;
65 frac = (((int)(val % 1024)) * 10 + 1024/2) / 1024; 74 frac = (((int)(val % 1024)) * 10 + 1024/2) / 1024;
66 val /= 1024; 75 val /= 1024;
67 } 76 }
@@ -69,9 +78,9 @@ const char* FAST_FUNC make_human_readable_str(unsigned long long size,
69 ++val; 78 ++val;
70 frac = 0; 79 frac = 0;
71 } 80 }
72#if 0 81#if 1
73 /* Sample code to omit decimal point and tenths digit. */ 82 /* Sample code to omit decimal point and tenths digit. */
74 if (/* no_tenths */ 1) { 83 if (no_tenths) {
75 if (frac >= 5) { 84 if (frac >= 5) {
76 ++val; 85 ++val;
77 } 86 }