blob: b4e0ef181a0decb9ea43f2f76a4a99f151c3cacc [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 *
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +020017 * 2) This routine 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 *
Denys Vlasenko93dd9fd2015-10-15 21:33:34 +020021 * If block_size is also 0, no decimal digits are printed.
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +000022 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020023 * Licensed under GPLv2, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +000024 */
25
Eric Andersenaad1a882001-03-16 22:47:14 +000026#include "libbb.h"
27
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020028const char* FAST_FUNC make_human_readable_str(unsigned long long val,
Eric Andersen97e24262003-08-22 23:08:37 +000029 unsigned long block_size, unsigned long display_unit)
Eric Andersenaad1a882001-03-16 22:47:14 +000030{
Denis Vlasenkod66aa3c2008-08-28 22:42:52 +000031 static const char unit_chars[] ALIGN1 = {
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020032 '\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
Denis Vlasenkod66aa3c2008-08-28 22:42:52 +000033 };
Eric Andersenaad1a882001-03-16 22:47:14 +000034
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020035 unsigned frac; /* 0..9 - the fractional digit */
Manuel Novoa III d877d442001-06-30 07:40:44 +000036 const char *u;
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020037 const char *fmt;
Eric Andersen91c93882001-04-03 23:14:29 +000038
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020039 if (val == 0)
Denis Vlasenkod66aa3c2008-08-28 22:42:52 +000040 return "0";
41
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020042 fmt = "%llu";
43 if (block_size > 1)
44 val *= block_size;
Manuel Novoa III d877d442001-06-30 07:40:44 +000045 frac = 0;
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020046 u = unit_chars;
Manuel Novoa III d877d442001-06-30 07:40:44 +000047
Manuel Novoa III d877d442001-06-30 07:40:44 +000048 if (display_unit) {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020049 val += display_unit/2; /* Deal with rounding */
50 val /= display_unit; /* Don't combine with the line above! */
Denis Vlasenkod66aa3c2008-08-28 22:42:52 +000051 /* will just print it as ulonglong (below) */
Manuel Novoa III d877d442001-06-30 07:40:44 +000052 } else {
Denis Vlasenkodca0b702006-10-27 09:05:02 +000053 while ((val >= 1024)
Denys Vlasenko9c3b84a2010-01-18 01:55:00 +010054 /* && (u < unit_chars + sizeof(unit_chars) - 1) - always true */
Denis Vlasenkodca0b702006-10-27 09:05:02 +000055 ) {
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020056 fmt = "%llu.%u%c";
Denis Vlasenkod66aa3c2008-08-28 22:42:52 +000057 u++;
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020058 frac = (((unsigned)val % 1024) * 10 + 1024/2) / 1024;
Denis Vlasenkodca0b702006-10-27 09:05:02 +000059 val /= 1024;
Manuel Novoa III d877d442001-06-30 07:40:44 +000060 }
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020061 if (frac >= 10) { /* we need to round up here */
Manuel Novoa III d877d442001-06-30 07:40:44 +000062 ++val;
63 frac = 0;
64 }
Denis Vlasenkod66aa3c2008-08-28 22:42:52 +000065#if 1
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020066 /* If block_size is 0, dont print fractional part */
67 if (block_size == 0) {
Denis Vlasenko219d14d2007-03-24 15:40:16 +000068 if (frac >= 5) {
Manuel Novoa III d877d442001-06-30 07:40:44 +000069 ++val;
70 }
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020071 fmt = "%llu%*c";
Manuel Novoa III d877d442001-06-30 07:40:44 +000072 frac = 1;
73 }
74#endif
75 }
76
Denys Vlasenko02859aa2015-10-09 18:16:40 +020077 return auto_string(xasprintf(fmt, val, frac, *u));
Manuel Novoa III d877d442001-06-30 07:40:44 +000078}
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020079
80
81/* vda's implementations of the similar idea */
82
83/* Convert unsigned long long value into compact 5-char representation.
84 * String is not terminated (buf[5] is untouched) */
Denys Vlasenkoa407cf72013-09-06 12:53:14 +020085char* FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale)
Denys Vlasenko0bf44d02009-10-13 01:25:09 +020086{
87 const char *fmt;
88 char c;
89 unsigned v, u, idx = 0;
90
91 if (ul > 99999) { // do not scale if 99999 or less
92 ul *= 10;
93 do {
94 ul /= 1024;
95 idx++;
96 } while (ul >= 100000);
97 }
98 v = ul; // ullong divisions are expensive, avoid them
99
100 fmt = " 123456789";
101 u = v / 10;
102 v = v % 10;
103 if (!idx) {
104 // 99999 or less: use "12345" format
105 // u is value/10, v is last digit
106 c = buf[0] = " 123456789"[u/1000];
107 if (c != ' ') fmt = "0123456789";
108 c = buf[1] = fmt[u/100%10];
109 if (c != ' ') fmt = "0123456789";
110 c = buf[2] = fmt[u/10%10];
111 if (c != ' ') fmt = "0123456789";
112 buf[3] = fmt[u%10];
113 buf[4] = "0123456789"[v];
114 } else {
115 // value has been scaled into 0..9999.9 range
116 // u is value, v is 1/10ths (allows for 92.1M format)
117 if (u >= 100) {
118 // value is >= 100: use "1234M', " 123M" formats
119 c = buf[0] = " 123456789"[u/1000];
120 if (c != ' ') fmt = "0123456789";
121 c = buf[1] = fmt[u/100%10];
122 if (c != ' ') fmt = "0123456789";
123 v = u % 10;
124 u = u / 10;
125 buf[2] = fmt[u%10];
126 } else {
127 // value is < 100: use "92.1M" format
128 c = buf[0] = " 123456789"[u/10];
129 if (c != ' ') fmt = "0123456789";
130 buf[1] = fmt[u%10];
131 buf[2] = '.';
132 }
133 buf[3] = "0123456789"[v];
134 buf[4] = scale[idx]; /* typically scale = " kmgt..." */
135 }
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200136 return buf + 5;
Denys Vlasenko0bf44d02009-10-13 01:25:09 +0200137}
138
139/* Convert unsigned long long value into compact 4-char
140 * representation. Examples: "1234", "1.2k", " 27M", "123T"
141 * String is not terminated (buf[4] is untouched) */
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200142char* FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale)
Denys Vlasenko0bf44d02009-10-13 01:25:09 +0200143{
144 const char *fmt;
145 char c;
146 unsigned v, u, idx = 0;
147
148 if (ul > 9999) { // do not scale if 9999 or less
149 ul *= 10;
150 do {
151 ul /= 1024;
152 idx++;
153 } while (ul >= 10000);
154 }
155 v = ul; // ullong divisions are expensive, avoid them
156
157 fmt = " 123456789";
158 u = v / 10;
159 v = v % 10;
160 if (!idx) {
161 // 9999 or less: use "1234" format
162 // u is value/10, v is last digit
163 c = buf[0] = " 123456789"[u/100];
164 if (c != ' ') fmt = "0123456789";
165 c = buf[1] = fmt[u/10%10];
166 if (c != ' ') fmt = "0123456789";
167 buf[2] = fmt[u%10];
168 buf[3] = "0123456789"[v];
169 } else {
170 // u is value, v is 1/10ths (allows for 9.2M format)
171 if (u >= 10) {
172 // value is >= 10: use "123M', " 12M" formats
173 c = buf[0] = " 123456789"[u/100];
174 if (c != ' ') fmt = "0123456789";
175 v = u % 10;
176 u = u / 10;
177 buf[1] = fmt[u%10];
178 } else {
179 // value is < 10: use "9.2M" format
180 buf[0] = "0123456789"[u];
181 buf[1] = '.';
182 }
183 buf[2] = "0123456789"[v];
184 buf[3] = scale[idx]; /* typically scale = " kmgt..." */
185 }
Denys Vlasenkoa407cf72013-09-06 12:53:14 +0200186 return buf + 4;
Denys Vlasenko0bf44d02009-10-13 01:25:09 +0200187}