1 /* vi: set sw=4 ts=4: */
3 * June 30, 2001 Manuel Novoa III
5 * All-integer version (hey, not everyone has floating point) of
6 * make_human_readable_str, modified from similar code I had written
7 * for busybox several months ago.
10 * 1) I'm using an unsigned long long to hold the product size * block_size,
11 * as df (which calls this routine) could request a representation of a
12 * partition size in bytes > max of unsigned long. If long longs aren't
13 * available, it would be possible to do what's needed using polynomial
14 * representations (say, powers of 1024) and manipulating coefficients.
15 * The base ten "bytes" output could be handled similarly.
17 * 2) This routine always outputs a decimal point and a tenths digit when
18 * display_unit != 0. Hence, it isn't uncommon for the returned string
19 * to have a length of 5 or 6.
21 * It might be nice to add a flag to indicate no decimal digits in
22 * that case. This could be either an additional parameter, or a
23 * special value of display_unit. Such a flag would also be nice for du.
25 * Some code to omit the decimal point and tenths digit is sketched out
26 * and "#if 0"'d below.
28 * Licensed under GPLv2, see file LICENSE in this source tree.
33 const char* FAST_FUNC make_human_readable_str(unsigned long long val,
34 unsigned long block_size, unsigned long display_unit)
36 static const char unit_chars[] ALIGN1 = {
37 '\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
42 unsigned frac; /* 0..9 - the fractional digit */
56 val += display_unit/2; /* Deal with rounding */
57 val /= display_unit; /* Don't combine with the line above! */
58 /* will just print it as ulonglong (below) */
61 /* && (u < unit_chars + sizeof(unit_chars) - 1) - always true */
65 frac = (((unsigned)val % 1024) * 10 + 1024/2) / 1024;
68 if (frac >= 10) { /* we need to round up here */
73 /* If block_size is 0, dont print fractional part */
74 if (block_size == 0) {
85 /* sufficient for any width of val */
86 str = xmalloc(sizeof(val)*3 + 2 + 3);
88 sprintf(str, fmt, val, frac, *u);
93 /* vda's implementations of the similar idea */
95 /* Convert unsigned long long value into compact 5-char representation.
96 * String is not terminated (buf[5] is untouched) */
97 char* FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale)
101 unsigned v, u, idx = 0;
103 if (ul > 99999) { // do not scale if 99999 or less
108 } while (ul >= 100000);
110 v = ul; // ullong divisions are expensive, avoid them
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";
125 buf[4] = "0123456789"[v];
127 // value has been scaled into 0..9999.9 range
128 // u is value, v is 1/10ths (allows for 92.1M format)
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";
139 // value is < 100: use "92.1M" format
140 c = buf[0] = " 123456789"[u/10];
141 if (c != ' ') fmt = "0123456789";
145 buf[3] = "0123456789"[v];
146 buf[4] = scale[idx]; /* typically scale = " kmgt..." */
151 /* Convert unsigned long long value into compact 4-char
152 * representation. Examples: "1234", "1.2k", " 27M", "123T"
153 * String is not terminated (buf[4] is untouched) */
154 char* FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale)
158 unsigned v, u, idx = 0;
160 if (ul > 9999) { // do not scale if 9999 or less
165 } while (ul >= 10000);
167 v = ul; // ullong divisions are expensive, avoid them
173 // 9999 or less: use "1234" format
174 // u is value/10, v is last digit
175 c = buf[0] = " 123456789"[u/100];
176 if (c != ' ') fmt = "0123456789";
177 c = buf[1] = fmt[u/10%10];
178 if (c != ' ') fmt = "0123456789";
180 buf[3] = "0123456789"[v];
182 // u is value, v is 1/10ths (allows for 9.2M format)
184 // value is >= 10: use "123M', " 12M" formats
185 c = buf[0] = " 123456789"[u/100];
186 if (c != ' ') fmt = "0123456789";
191 // value is < 10: use "9.2M" format
192 buf[0] = "0123456789"[u];
195 buf[2] = "0123456789"[v];
196 buf[3] = scale[idx]; /* typically scale = " kmgt..." */