aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2009-10-13 01:25:09 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2009-10-13 01:25:09 +0200
commit0bf44d00a42dec70514c2e51926f4ca37b4b2367 (patch)
treebe01fbf44da8040c5ef271e0d0bba147ce0c525e
parent76ace254e171ee9ca7a13f36335ccad9cc6ae6e1 (diff)
downloadbusybox-w32-0bf44d00a42dec70514c2e51926f4ca37b4b2367.tar.gz
busybox-w32-0bf44d00a42dec70514c2e51926f4ca37b4b2367.tar.bz2
busybox-w32-0bf44d00a42dec70514c2e51926f4ca37b4b2367.zip
libbb/human_readable.c: shrink; and reduce bss usage
also, move smart_ulltoaN there and comment usage locations function old new delta static.unit_chars 7 9 +2 utoa_to_buf 110 108 -2 make_human_readable_str 262 258 -4 fallbackSort 1723 1719 -4 static.fmt 97 92 -5 static.fmt_tenths 10 - -10 static.str 21 4 -17 ------------------------------------------------------------------------------ (add/remove: 0/1 grow/shrink: 1/5 up/down: 2/-42) Total: -40 bytes text data bss dec hex filename 820981 453 6932 828366 ca3ce busybox_old 820968 453 6916 828337 ca3b1 busybox_unstripped Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/df.c23
-rw-r--r--coreutils/du.c7
-rw-r--r--coreutils/ls.c4
-rw-r--r--include/libbb.h4
-rw-r--r--libbb/human_readable.c162
-rw-r--r--libbb/xfuncs.c104
6 files changed, 158 insertions, 146 deletions
diff --git a/coreutils/df.c b/coreutils/df.c
index c37b18893..624ad94ba 100644
--- a/coreutils/df.c
+++ b/coreutils/df.c
@@ -92,7 +92,10 @@ int df_main(int argc, char **argv)
92 if (disp_units_hdr == NULL) { 92 if (disp_units_hdr == NULL) {
93#if ENABLE_FEATURE_HUMAN_READABLE 93#if ENABLE_FEATURE_HUMAN_READABLE
94 disp_units_hdr = xasprintf("%s-blocks", 94 disp_units_hdr = xasprintf("%s-blocks",
95 make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX))); 95 /* print df_disp_hr, show no fractionals,
96 * use suffixes if OPT_POSIX is set in opt */
97 make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX))
98 );
96#else 99#else
97 disp_units_hdr = xasprintf("%lu-blocks", df_disp_hr); 100 disp_units_hdr = xasprintf("%lu-blocks", df_disp_hr);
98#endif 101#endif
@@ -189,21 +192,27 @@ int df_main(int argc, char **argv)
189 192
190#if ENABLE_FEATURE_HUMAN_READABLE 193#if ENABLE_FEATURE_HUMAN_READABLE
191 printf(" %9s ", 194 printf(" %9s ",
195 /* f_blocks x f_bsize / df_disp_hr, show one fractional,
196 * use suffixes if df_disp_hr == 0 */
192 make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr)); 197 make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
193 198
194 printf(" %9s " + 1, 199 printf(" %9s " + 1,
200 /* EXPR x f_bsize / df_disp_hr, show one fractional,
201 * use suffixes if df_disp_hr == 0 */
195 make_human_readable_str((s.f_blocks - s.f_bfree), 202 make_human_readable_str((s.f_blocks - s.f_bfree),
196 s.f_bsize, df_disp_hr)); 203 s.f_bsize, df_disp_hr));
197 204
198 printf("%9s %3u%% %s\n", 205 printf("%9s %3u%% %s\n",
199 make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr), 206 /* f_bavail x f_bsize / df_disp_hr, show one fractional,
200 blocks_percent_used, mount_point); 207 * use suffixes if df_disp_hr == 0 */
208 make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
209 blocks_percent_used, mount_point);
201#else 210#else
202 printf(" %9lu %9lu %9lu %3u%% %s\n", 211 printf(" %9lu %9lu %9lu %3u%% %s\n",
203 kscale(s.f_blocks, s.f_bsize), 212 kscale(s.f_blocks, s.f_bsize),
204 kscale(s.f_blocks - s.f_bfree, s.f_bsize), 213 kscale(s.f_blocks - s.f_bfree, s.f_bsize),
205 kscale(s.f_bavail, s.f_bsize), 214 kscale(s.f_bavail, s.f_bsize),
206 blocks_percent_used, mount_point); 215 blocks_percent_used, mount_point);
207#endif 216#endif
208 } 217 }
209 } 218 }
diff --git a/coreutils/du.c b/coreutils/du.c
index ec283f85e..730d6d162 100644
--- a/coreutils/du.c
+++ b/coreutils/du.c
@@ -58,14 +58,17 @@ static void print(unsigned long size, const char *filename)
58{ 58{
59 /* TODO - May not want to defer error checking here. */ 59 /* TODO - May not want to defer error checking here. */
60#if ENABLE_FEATURE_HUMAN_READABLE 60#if ENABLE_FEATURE_HUMAN_READABLE
61 printf("%s\t%s\n", make_human_readable_str(size, 512, G.disp_hr), 61 printf("%s\t%s\n",
62 /* size x 512 / G.disp_hr, show one fractional,
63 * use suffixes if G.disp_hr == 0 */
64 make_human_readable_str(size, 512, G.disp_hr),
62 filename); 65 filename);
63#else 66#else
64 if (G.disp_k) { 67 if (G.disp_k) {
65 size++; 68 size++;
66 size >>= 1; 69 size >>= 1;
67 } 70 }
68 printf("%ld\t%s\n", size, filename); 71 printf("%lu\t%s\n", size, filename);
69#endif 72#endif
70} 73}
71 74
diff --git a/coreutils/ls.c b/coreutils/ls.c
index a067aa36c..38cabcab6 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -829,7 +829,9 @@ static NOINLINE unsigned list_single(const struct dnode *dn)
829 } else { 829 } else {
830 if (all_fmt & LS_DISP_HR) { 830 if (all_fmt & LS_DISP_HR) {
831 column += printf("%9s ", 831 column += printf("%9s ",
832 make_human_readable_str(dn->dstat.st_size, 1, 0)); 832 /* print st_size, show one fractional, use suffixes */
833 make_human_readable_str(dn->dstat.st_size, 1, 0)
834 );
833 } else { 835 } else {
834 column += printf("%9"OFF_FMT"u ", (off_t) dn->dstat.st_size); 836 column += printf("%9"OFF_FMT"u ", (off_t) dn->dstat.st_size);
835 } 837 }
diff --git a/include/libbb.h b/include/libbb.h
index dca14b40d..be175d79f 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -691,6 +691,10 @@ char *itoa_to_buf(int n, char *buf, unsigned buflen) FAST_FUNC;
691/* Intelligent formatters of bignums */ 691/* Intelligent formatters of bignums */
692void smart_ulltoa4(unsigned long long ul, char buf[5], const char *scale) FAST_FUNC; 692void smart_ulltoa4(unsigned long long ul, char buf[5], const char *scale) FAST_FUNC;
693void smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale) FAST_FUNC; 693void smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale) FAST_FUNC;
694/* If block_size == 0, display size without fractional part,
695 * else display (size * block_size) with one decimal digit.
696 * If display_unit == 0, add suffix (K,M,G...),
697 * else divide by display_unit and do not use suffix. */
694//TODO: provide pointer to buf (avoid statics)? 698//TODO: provide pointer to buf (avoid statics)?
695const char *make_human_readable_str(unsigned long long size, 699const char *make_human_readable_str(unsigned long long size,
696 unsigned long block_size, unsigned long display_unit) FAST_FUNC; 700 unsigned long block_size, unsigned long display_unit) FAST_FUNC;
diff --git a/libbb/human_readable.c b/libbb/human_readable.c
index 05e7d86ec..3050d7d1e 100644
--- a/libbb/human_readable.c
+++ b/libbb/human_readable.c
@@ -30,70 +30,168 @@
30 30
31#include "libbb.h" 31#include "libbb.h"
32 32
33const char* FAST_FUNC make_human_readable_str(unsigned long long size, 33const char* FAST_FUNC make_human_readable_str(unsigned long long val,
34 unsigned long block_size, unsigned long display_unit) 34 unsigned long block_size, unsigned long display_unit)
35{ 35{
36 /* The code will adjust for additional (appended) units */
37 static const char unit_chars[] ALIGN1 = { 36 static const char unit_chars[] ALIGN1 = {
38 '\0', 'K', 'M', 'G', 'T', 'P', 'E' 37 '\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
39 }; 38 };
40 static const char fmt[] ALIGN1 = "%llu";
41 static const char fmt_tenths[] ALIGN1 = "%llu.%d%c";
42 39
43 static char str[21] ALIGN1; /* Sufficient for 64 bit unsigned integers */ 40 static char *str;
44 41
45 unsigned long long val; 42 unsigned frac; /* 0..9 - the fractional digit */
46 int frac;
47 const char *u; 43 const char *u;
48 const char *f; 44 const char *fmt;
49 smallint no_tenths;
50 45
51 if (size == 0) 46 if (val == 0)
52 return "0"; 47 return "0";
53 48
54 /* If block_size is 0 then do not print tenths */ 49 fmt = "%llu";
55 no_tenths = 0; 50 if (block_size > 1)
56 if (block_size == 0) { 51 val *= block_size;
57 no_tenths = 1;
58 block_size = 1;
59 }
60
61 u = unit_chars;
62 val = size * block_size;
63 f = fmt;
64 frac = 0; 52 frac = 0;
53 u = unit_chars;
65 54
66 if (display_unit) { 55 if (display_unit) {
67 val += display_unit/2; /* Deal with rounding */ 56 val += display_unit/2; /* Deal with rounding */
68 val /= display_unit; /* Don't combine with the line above!!! */ 57 val /= display_unit; /* Don't combine with the line above! */
69 /* will just print it as ulonglong (below) */ 58 /* will just print it as ulonglong (below) */
70 } else { 59 } else {
71 while ((val >= 1024) 60 while ((val >= 1024)
72 && (u < unit_chars + sizeof(unit_chars) - 1) 61 /* && (u < unit_chars + sizeof(unit_chars) - 1) - never happens */
73 ) { 62 ) {
74 f = fmt_tenths; 63 fmt = "%llu.%u%c";
75 u++; 64 u++;
76 frac = (((int)(val % 1024)) * 10 + 1024/2) / 1024; 65 frac = (((unsigned)val % 1024) * 10 + 1024/2) / 1024;
77 val /= 1024; 66 val /= 1024;
78 } 67 }
79 if (frac >= 10) { /* We need to round up here. */ 68 if (frac >= 10) { /* we need to round up here */
80 ++val; 69 ++val;
81 frac = 0; 70 frac = 0;
82 } 71 }
83#if 1 72#if 1
84 /* Sample code to omit decimal point and tenths digit. */ 73 /* If block_size is 0, dont print fractional part */
85 if (no_tenths) { 74 if (block_size == 0) {
86 if (frac >= 5) { 75 if (frac >= 5) {
87 ++val; 76 ++val;
88 } 77 }
89 f = "%llu%*c" /* fmt_no_tenths */; 78 fmt = "%llu%*c";
90 frac = 1; 79 frac = 1;
91 } 80 }
92#endif 81#endif
93 } 82 }
94 83
95 /* If f==fmt then 'frac' and 'u' are ignored. */ 84 if (!str) {
96 snprintf(str, sizeof(str), f, val, frac, *u); 85 /* sufficient for any width of val */
97 86 str = xmalloc(sizeof(val)*3 + 2 + 3);
87 }
88 sprintf(str, fmt, val, frac, *u);
98 return str; 89 return str;
99} 90}
91
92
93/* vda's implementations of the similar idea */
94
95/* Convert unsigned long long value into compact 5-char representation.
96 * String is not terminated (buf[5] is untouched) */
97void FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[6], const char *scale)
98{
99 const char *fmt;
100 char c;
101 unsigned v, u, idx = 0;
102
103 if (ul > 99999) { // do not scale if 99999 or less
104 ul *= 10;
105 do {
106 ul /= 1024;
107 idx++;
108 } while (ul >= 100000);
109 }
110 v = ul; // ullong divisions are expensive, avoid them
111
112 fmt = " 123456789";
113 u = v / 10;
114 v = v % 10;
115 if (!idx) {
116 // 99999 or less: use "12345" format
117 // u is value/10, v is last digit
118 c = buf[0] = " 123456789"[u/1000];
119 if (c != ' ') fmt = "0123456789";
120 c = buf[1] = fmt[u/100%10];
121 if (c != ' ') fmt = "0123456789";
122 c = buf[2] = fmt[u/10%10];
123 if (c != ' ') fmt = "0123456789";
124 buf[3] = fmt[u%10];
125 buf[4] = "0123456789"[v];
126 } else {
127 // value has been scaled into 0..9999.9 range
128 // u is value, v is 1/10ths (allows for 92.1M format)
129 if (u >= 100) {
130 // value is >= 100: use "1234M', " 123M" formats
131 c = buf[0] = " 123456789"[u/1000];
132 if (c != ' ') fmt = "0123456789";
133 c = buf[1] = fmt[u/100%10];
134 if (c != ' ') fmt = "0123456789";
135 v = u % 10;
136 u = u / 10;
137 buf[2] = fmt[u%10];
138 } else {
139 // value is < 100: use "92.1M" format
140 c = buf[0] = " 123456789"[u/10];
141 if (c != ' ') fmt = "0123456789";
142 buf[1] = fmt[u%10];
143 buf[2] = '.';
144 }
145 buf[3] = "0123456789"[v];
146 buf[4] = scale[idx]; /* typically scale = " kmgt..." */
147 }
148}
149
150/* Convert unsigned long long value into compact 4-char
151 * representation. Examples: "1234", "1.2k", " 27M", "123T"
152 * String is not terminated (buf[4] is untouched) */
153void FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[5], const char *scale)
154{
155 const char *fmt;
156 char c;
157 unsigned v, u, idx = 0;
158
159 if (ul > 9999) { // do not scale if 9999 or less
160 ul *= 10;
161 do {
162 ul /= 1024;
163 idx++;
164 } while (ul >= 10000);
165 }
166 v = ul; // ullong divisions are expensive, avoid them
167
168 fmt = " 123456789";
169 u = v / 10;
170 v = v % 10;
171 if (!idx) {
172 // 9999 or less: use "1234" format
173 // u is value/10, v is last digit
174 c = buf[0] = " 123456789"[u/100];
175 if (c != ' ') fmt = "0123456789";
176 c = buf[1] = fmt[u/10%10];
177 if (c != ' ') fmt = "0123456789";
178 buf[2] = fmt[u%10];
179 buf[3] = "0123456789"[v];
180 } else {
181 // u is value, v is 1/10ths (allows for 9.2M format)
182 if (u >= 10) {
183 // value is >= 10: use "123M', " 12M" formats
184 c = buf[0] = " 123456789"[u/100];
185 if (c != ' ') fmt = "0123456789";
186 v = u % 10;
187 u = u / 10;
188 buf[1] = fmt[u%10];
189 } else {
190 // value is < 10: use "9.2M" format
191 buf[0] = "0123456789"[u];
192 buf[1] = '.';
193 }
194 buf[2] = "0123456789"[v];
195 buf[3] = scale[idx]; /* typically scale = " kmgt..." */
196 }
197}
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index a86efc298..aa0812914 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -48,110 +48,6 @@ char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src)
48 return strncpy(dst, src, IFNAMSIZ); 48 return strncpy(dst, src, IFNAMSIZ);
49} 49}
50 50
51/* Convert unsigned long long value into compact 4-char
52 * representation. Examples: "1234", "1.2k", " 27M", "123T"
53 * String is not terminated (buf[4] is untouched) */
54void FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[5], const char *scale)
55{
56 const char *fmt;
57 char c;
58 unsigned v, u, idx = 0;
59
60 if (ul > 9999) { // do not scale if 9999 or less
61 ul *= 10;
62 do {
63 ul /= 1024;
64 idx++;
65 } while (ul >= 10000);
66 }
67 v = ul; // ullong divisions are expensive, avoid them
68
69 fmt = " 123456789";
70 u = v / 10;
71 v = v % 10;
72 if (!idx) {
73 // 9999 or less: use "1234" format
74 // u is value/10, v is last digit
75 c = buf[0] = " 123456789"[u/100];
76 if (c != ' ') fmt = "0123456789";
77 c = buf[1] = fmt[u/10%10];
78 if (c != ' ') fmt = "0123456789";
79 buf[2] = fmt[u%10];
80 buf[3] = "0123456789"[v];
81 } else {
82 // u is value, v is 1/10ths (allows for 9.2M format)
83 if (u >= 10) {
84 // value is >= 10: use "123M', " 12M" formats
85 c = buf[0] = " 123456789"[u/100];
86 if (c != ' ') fmt = "0123456789";
87 v = u % 10;
88 u = u / 10;
89 buf[1] = fmt[u%10];
90 } else {
91 // value is < 10: use "9.2M" format
92 buf[0] = "0123456789"[u];
93 buf[1] = '.';
94 }
95 buf[2] = "0123456789"[v];
96 buf[3] = scale[idx]; /* typically scale = " kmgt..." */
97 }
98}
99
100/* Convert unsigned long long value into compact 5-char representation.
101 * String is not terminated (buf[5] is untouched) */
102void FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[6], const char *scale)
103{
104 const char *fmt;
105 char c;
106 unsigned v, u, idx = 0;
107
108 if (ul > 99999) { // do not scale if 99999 or less
109 ul *= 10;
110 do {
111 ul /= 1024;
112 idx++;
113 } while (ul >= 100000);
114 }
115 v = ul; // ullong divisions are expensive, avoid them
116
117 fmt = " 123456789";
118 u = v / 10;
119 v = v % 10;
120 if (!idx) {
121 // 99999 or less: use "12345" format
122 // u is value/10, v is last digit
123 c = buf[0] = " 123456789"[u/1000];
124 if (c != ' ') fmt = "0123456789";
125 c = buf[1] = fmt[u/100%10];
126 if (c != ' ') fmt = "0123456789";
127 c = buf[2] = fmt[u/10%10];
128 if (c != ' ') fmt = "0123456789";
129 buf[3] = fmt[u%10];
130 buf[4] = "0123456789"[v];
131 } else {
132 // value has been scaled into 0..9999.9 range
133 // u is value, v is 1/10ths (allows for 92.1M format)
134 if (u >= 100) {
135 // value is >= 100: use "1234M', " 123M" formats
136 c = buf[0] = " 123456789"[u/1000];
137 if (c != ' ') fmt = "0123456789";
138 c = buf[1] = fmt[u/100%10];
139 if (c != ' ') fmt = "0123456789";
140 v = u % 10;
141 u = u / 10;
142 buf[2] = fmt[u%10];
143 } else {
144 // value is < 100: use "92.1M" format
145 c = buf[0] = " 123456789"[u/10];
146 if (c != ' ') fmt = "0123456789";
147 buf[1] = fmt[u%10];
148 buf[2] = '.';
149 }
150 buf[3] = "0123456789"[v];
151 buf[4] = scale[idx]; /* typically scale = " kmgt..." */
152 }
153}
154
155 51
156// Convert unsigned integer to ascii, writing into supplied buffer. 52// Convert unsigned integer to ascii, writing into supplied buffer.
157// A truncated result contains the first few digits of the result ala strncpy. 53// A truncated result contains the first few digits of the result ala strncpy.