blob: 5c7fc076ff52a9e9f0b46d869446ec4f2bc5b727 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Eric Andersenaad1a882001-03-16 22:47:14 +00002/*
Manuel Novoa III d877d442001-06-30 07:40:44 +00003 * June 30, 2001 Manuel Novoa III
Eric Andersenaad1a882001-03-16 22:47:14 +00004 *
Manuel Novoa III d877d442001-06-30 07:40:44 +00005 * 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.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 *
Manuel Novoa III d877d442001-06-30 07:40:44 +00009 * Notes:
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.
Eric Andersenaad1a882001-03-16 22:47:14 +000016 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +000017 * 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
Eric Andersen01595972001-06-30 18:08:36 +000019 * to have a length of 5 or 6.
Eric Andersenaad1a882001-03-16 22:47:14 +000020 *
Manuel Novoa III d877d442001-06-30 07:40:44 +000021 * 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.
24 *
25 * Some code to omit the decimal point and tenths digit is sketched out
26 * and "#if 0"'d below.
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +000027 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020028 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +000029 */
30
Eric Andersenaad1a882001-03-16 22:47:14 +000031#include "libbb.h"
32
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020033const char* FAST_FUNC make_human_readable_str(unsigned long long val,
Eric Andersen97e24262003-08-22 23:08:37 +000034 unsigned long block_size, unsigned long display_unit)
Eric Andersenaad1a882001-03-16 22:47:14 +000035{
Denis Vlasenkod66aa3c2008-08-28 22:42:52 +000036 static const char unit_chars[] ALIGN1 = {
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020037 '\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
Denis Vlasenkod66aa3c2008-08-28 22:42:52 +000038 };
Eric Andersenaad1a882001-03-16 22:47:14 +000039
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020040 unsigned frac; /* 0..9 - the fractional digit */
Manuel Novoa III d877d442001-06-30 07:40:44 +000041 const char *u;
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020042 const char *fmt;
Eric Andersen91c93882001-04-03 23:14:29 +000043
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020044 if (val == 0)
Denis Vlasenkod66aa3c2008-08-28 22:42:52 +000045 return "0";
46
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020047 fmt = "%llu";
48 if (block_size > 1)
49 val *= block_size;
Manuel Novoa III d877d442001-06-30 07:40:44 +000050 frac = 0;
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020051 u = unit_chars;
Manuel Novoa III d877d442001-06-30 07:40:44 +000052
Manuel Novoa III d877d442001-06-30 07:40:44 +000053 if (display_unit) {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020054 val += display_unit/2; /* Deal with rounding */
55 val /= display_unit; /* Don't combine with the line above! */
Denis Vlasenkod66aa3c2008-08-28 22:42:52 +000056 /* will just print it as ulonglong (below) */
Manuel Novoa III d877d442001-06-30 07:40:44 +000057 } else {
Denis Vlasenkodca0b702006-10-27 09:05:02 +000058 while ((val >= 1024)
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +010059 /* && (u < unit_chars + sizeof(unit_chars) - 1) - always true */
Denis Vlasenkodca0b702006-10-27 09:05:02 +000060 ) {
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020061 fmt = "%llu.%u%c";
Denis Vlasenkod66aa3c2008-08-28 22:42:52 +000062 u++;
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020063 frac = (((unsigned)val % 1024) * 10 + 1024/2) / 1024;
Denis Vlasenkodca0b702006-10-27 09:05:02 +000064 val /= 1024;
Manuel Novoa III d877d442001-06-30 07:40:44 +000065 }
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020066 if (frac >= 10) { /* we need to round up here */
Manuel Novoa III d877d442001-06-30 07:40:44 +000067 ++val;
68 frac = 0;
69 }
Denis Vlasenkod66aa3c2008-08-28 22:42:52 +000070#if 1
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020071 /* If block_size is 0, dont print fractional part */
72 if (block_size == 0) {
Denis Vlasenko219d14d2007-03-24 15:40:16 +000073 if (frac >= 5) {
Manuel Novoa III d877d442001-06-30 07:40:44 +000074 ++val;
75 }
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020076 fmt = "%llu%*c";
Manuel Novoa III d877d442001-06-30 07:40:44 +000077 frac = 1;
78 }
79#endif
80 }
81
Denys Vlasenko02859aa2015-10-09 18:16:40 +020082 return auto_string(xasprintf(fmt, val, frac, *u));
Manuel Novoa III d877d442001-06-30 07:40:44 +000083}
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020084
85
86/* vda's implementations of the similar idea */
87
88/* Convert unsigned long long value into compact 5-char representation.
89 * String is not terminated (buf[5] is untouched) */
Denys Vlasenkoa407cf72013-09-06 12:53:14 +020090char* FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale)
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020091{
92 const char *fmt;
93 char c;
94 unsigned v, u, idx = 0;
95
96 if (ul > 99999) { // do not scale if 99999 or less
97 ul *= 10;
98 do {
99 ul /= 1024;
100 idx++;
101 } while (ul >= 100000);
102 }
103 v = ul; // ullong divisions are expensive, avoid them
104
105 fmt = " 123456789";
106 u = v / 10;
107 v = v % 10;
108 if (!idx) {
109 // 99999 or less: use "12345" format
110 // u is value/10, v is last digit
111 c = buf[0] = " 123456789"[u/1000];
112 if (c != ' ') fmt = "0123456789";
113 c = buf[1] = fmt[u/100%10];
114 if (c != ' ') fmt = "0123456789";
115 c = buf[2] = fmt[u/10%10];
116 if (c != ' ') fmt = "0123456789";
117 buf[3] = fmt[u%10];
118 buf[4] = "0123456789"[v];
119 } else {
120 // value has been scaled into 0..9999.9 range
121 // u is value, v is 1/10ths (allows for 92.1M format)
122 if (u >= 100) {
123 // value is >= 100: use "1234M', " 123M" formats
124 c = buf[0] = " 123456789"[u/1000];
125 if (c != ' ') fmt = "0123456789";
126 c = buf[1] = fmt[u/100%10];
127 if (c != ' ') fmt = "0123456789";
128 v = u % 10;
129 u = u / 10;
130 buf[2] = fmt[u%10];
131 } else {
132 // value is < 100: use "92.1M" format
133 c = buf[0] = " 123456789"[u/10];
134 if (c != ' ') fmt = "0123456789";
135 buf[1] = fmt[u%10];
136 buf[2] = '.';
137 }
138 buf[3] = "0123456789"[v];
139 buf[4] = scale[idx]; /* typically scale = " kmgt..." */
140 }
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200141 return buf + 5;
Denys Vlasenko0bf44d02009-10-13 01:25:09 +0200142}
143
144/* Convert unsigned long long value into compact 4-char
145 * representation. Examples: "1234", "1.2k", " 27M", "123T"
146 * String is not terminated (buf[4] is untouched) */
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200147char* FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale)
Denys Vlasenko0bf44d02009-10-13 01:25:09 +0200148{
149 const char *fmt;
150 char c;
151 unsigned v, u, idx = 0;
152
153 if (ul > 9999) { // do not scale if 9999 or less
154 ul *= 10;
155 do {
156 ul /= 1024;
157 idx++;
158 } while (ul >= 10000);
159 }
160 v = ul; // ullong divisions are expensive, avoid them
161
162 fmt = " 123456789";
163 u = v / 10;
164 v = v % 10;
165 if (!idx) {
166 // 9999 or less: use "1234" format
167 // u is value/10, v is last digit
168 c = buf[0] = " 123456789"[u/100];
169 if (c != ' ') fmt = "0123456789";
170 c = buf[1] = fmt[u/10%10];
171 if (c != ' ') fmt = "0123456789";
172 buf[2] = fmt[u%10];
173 buf[3] = "0123456789"[v];
174 } else {
175 // u is value, v is 1/10ths (allows for 9.2M format)
176 if (u >= 10) {
177 // value is >= 10: use "123M', " 12M" formats
178 c = buf[0] = " 123456789"[u/100];
179 if (c != ' ') fmt = "0123456789";
180 v = u % 10;
181 u = u / 10;
182 buf[1] = fmt[u%10];
183 } else {
184 // value is < 10: use "9.2M" format
185 buf[0] = "0123456789"[u];
186 buf[1] = '.';
187 }
188 buf[2] = "0123456789"[v];
189 buf[3] = scale[idx]; /* typically scale = " kmgt..." */
190 }
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200191 return buf + 4;
Denys Vlasenko0bf44d02009-10-13 01:25:09 +0200192}