2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2001-03-17 04:17:14 +05:30
|
|
|
/*
|
2001-06-30 13:10:44 +05:30
|
|
|
* June 30, 2001 Manuel Novoa III
|
2001-03-17 04:17:14 +05:30
|
|
|
*
|
2001-06-30 13:10:44 +05:30
|
|
|
* All-integer version (hey, not everyone has floating point) of
|
|
|
|
* make_human_readable_str, modified from similar code I had written
|
|
|
|
* for busybox several months ago.
|
2001-03-17 04:17:14 +05:30
|
|
|
*
|
2001-06-30 13:10:44 +05:30
|
|
|
* Notes:
|
|
|
|
* 1) I'm using an unsigned long long to hold the product size * block_size,
|
|
|
|
* as df (which calls this routine) could request a representation of a
|
|
|
|
* partition size in bytes > max of unsigned long. If long longs aren't
|
|
|
|
* available, it would be possible to do what's needed using polynomial
|
|
|
|
* representations (say, powers of 1024) and manipulating coefficients.
|
|
|
|
* The base ten "bytes" output could be handled similarly.
|
2001-03-17 04:17:14 +05:30
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* 2) This routine always outputs a decimal point and a tenths digit when
|
|
|
|
* display_unit != 0. Hence, it isn't uncommon for the returned string
|
2001-06-30 23:38:36 +05:30
|
|
|
* to have a length of 5 or 6.
|
2001-03-17 04:17:14 +05:30
|
|
|
*
|
2001-06-30 13:10:44 +05:30
|
|
|
* It might be nice to add a flag to indicate no decimal digits in
|
|
|
|
* that case. This could be either an additional parameter, or a
|
|
|
|
* special value of display_unit. Such a flag would also be nice for du.
|
|
|
|
*
|
|
|
|
* Some code to omit the decimal point and tenths digit is sketched out
|
|
|
|
* and "#if 0"'d below.
|
2008-12-07 06:22:58 +05:30
|
|
|
*
|
|
|
|
* Licensed under GPLv2, see file LICENSE in this tarball for details.
|
2001-03-17 04:17:14 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2009-10-13 04:55:09 +05:30
|
|
|
const char* FAST_FUNC make_human_readable_str(unsigned long long val,
|
2003-08-23 04:38:37 +05:30
|
|
|
unsigned long block_size, unsigned long display_unit)
|
2001-04-04 04:44:29 +05:30
|
|
|
{
|
2008-08-29 04:12:52 +05:30
|
|
|
static const char unit_chars[] ALIGN1 = {
|
2009-10-13 04:55:09 +05:30
|
|
|
'\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
|
2008-08-29 04:12:52 +05:30
|
|
|
};
|
2001-03-17 04:17:14 +05:30
|
|
|
|
2009-10-13 04:55:09 +05:30
|
|
|
static char *str;
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2009-10-13 04:55:09 +05:30
|
|
|
unsigned frac; /* 0..9 - the fractional digit */
|
2001-06-30 13:10:44 +05:30
|
|
|
const char *u;
|
2009-10-13 04:55:09 +05:30
|
|
|
const char *fmt;
|
2001-06-30 13:10:44 +05:30
|
|
|
|
2009-10-13 04:55:09 +05:30
|
|
|
if (val == 0)
|
2008-08-29 04:12:52 +05:30
|
|
|
return "0";
|
2001-06-30 13:10:44 +05:30
|
|
|
|
2009-10-13 04:55:09 +05:30
|
|
|
fmt = "%llu";
|
|
|
|
if (block_size > 1)
|
|
|
|
val *= block_size;
|
2008-08-29 04:12:52 +05:30
|
|
|
frac = 0;
|
2009-10-13 04:55:09 +05:30
|
|
|
u = unit_chars;
|
2008-08-29 04:12:52 +05:30
|
|
|
|
2001-06-30 13:10:44 +05:30
|
|
|
if (display_unit) {
|
2007-08-13 02:28:27 +05:30
|
|
|
val += display_unit/2; /* Deal with rounding */
|
2009-10-13 04:55:09 +05:30
|
|
|
val /= display_unit; /* Don't combine with the line above! */
|
2008-08-29 04:12:52 +05:30
|
|
|
/* will just print it as ulonglong (below) */
|
2001-06-13 13:32:45 +05:30
|
|
|
} else {
|
2006-10-27 14:35:02 +05:30
|
|
|
while ((val >= 1024)
|
2009-10-13 04:55:09 +05:30
|
|
|
/* && (u < unit_chars + sizeof(unit_chars) - 1) - never happens */
|
2006-10-27 14:35:02 +05:30
|
|
|
) {
|
2009-10-13 04:55:09 +05:30
|
|
|
fmt = "%llu.%u%c";
|
2008-08-29 04:12:52 +05:30
|
|
|
u++;
|
2009-10-13 04:55:09 +05:30
|
|
|
frac = (((unsigned)val % 1024) * 10 + 1024/2) / 1024;
|
2006-10-27 14:35:02 +05:30
|
|
|
val /= 1024;
|
2001-06-30 13:10:44 +05:30
|
|
|
}
|
2009-10-13 04:55:09 +05:30
|
|
|
if (frac >= 10) { /* we need to round up here */
|
2001-06-30 13:10:44 +05:30
|
|
|
++val;
|
|
|
|
frac = 0;
|
|
|
|
}
|
2008-08-29 04:12:52 +05:30
|
|
|
#if 1
|
2009-10-13 04:55:09 +05:30
|
|
|
/* If block_size is 0, dont print fractional part */
|
|
|
|
if (block_size == 0) {
|
2007-03-24 21:10:16 +05:30
|
|
|
if (frac >= 5) {
|
2001-06-30 13:10:44 +05:30
|
|
|
++val;
|
2001-06-13 13:32:45 +05:30
|
|
|
}
|
2009-10-13 04:55:09 +05:30
|
|
|
fmt = "%llu%*c";
|
2001-06-30 13:10:44 +05:30
|
|
|
frac = 1;
|
2001-06-13 13:32:45 +05:30
|
|
|
}
|
2001-06-30 13:10:44 +05:30
|
|
|
#endif
|
2001-04-04 04:44:29 +05:30
|
|
|
}
|
|
|
|
|
2009-10-13 04:55:09 +05:30
|
|
|
if (!str) {
|
|
|
|
/* sufficient for any width of val */
|
|
|
|
str = xmalloc(sizeof(val)*3 + 2 + 3);
|
|
|
|
}
|
|
|
|
sprintf(str, fmt, val, frac, *u);
|
2001-06-30 13:10:44 +05:30
|
|
|
return str;
|
|
|
|
}
|
2009-10-13 04:55:09 +05:30
|
|
|
|
|
|
|
|
|
|
|
/* vda's implementations of the similar idea */
|
|
|
|
|
|
|
|
/* Convert unsigned long long value into compact 5-char representation.
|
|
|
|
* String is not terminated (buf[5] is untouched) */
|
|
|
|
void FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[6], const char *scale)
|
|
|
|
{
|
|
|
|
const char *fmt;
|
|
|
|
char c;
|
|
|
|
unsigned v, u, idx = 0;
|
|
|
|
|
|
|
|
if (ul > 99999) { // do not scale if 99999 or less
|
|
|
|
ul *= 10;
|
|
|
|
do {
|
|
|
|
ul /= 1024;
|
|
|
|
idx++;
|
|
|
|
} while (ul >= 100000);
|
|
|
|
}
|
|
|
|
v = ul; // ullong divisions are expensive, avoid them
|
|
|
|
|
|
|
|
fmt = " 123456789";
|
|
|
|
u = v / 10;
|
|
|
|
v = v % 10;
|
|
|
|
if (!idx) {
|
|
|
|
// 99999 or less: use "12345" format
|
|
|
|
// u is value/10, v is last digit
|
|
|
|
c = buf[0] = " 123456789"[u/1000];
|
|
|
|
if (c != ' ') fmt = "0123456789";
|
|
|
|
c = buf[1] = fmt[u/100%10];
|
|
|
|
if (c != ' ') fmt = "0123456789";
|
|
|
|
c = buf[2] = fmt[u/10%10];
|
|
|
|
if (c != ' ') fmt = "0123456789";
|
|
|
|
buf[3] = fmt[u%10];
|
|
|
|
buf[4] = "0123456789"[v];
|
|
|
|
} else {
|
|
|
|
// value has been scaled into 0..9999.9 range
|
|
|
|
// u is value, v is 1/10ths (allows for 92.1M format)
|
|
|
|
if (u >= 100) {
|
|
|
|
// value is >= 100: use "1234M', " 123M" formats
|
|
|
|
c = buf[0] = " 123456789"[u/1000];
|
|
|
|
if (c != ' ') fmt = "0123456789";
|
|
|
|
c = buf[1] = fmt[u/100%10];
|
|
|
|
if (c != ' ') fmt = "0123456789";
|
|
|
|
v = u % 10;
|
|
|
|
u = u / 10;
|
|
|
|
buf[2] = fmt[u%10];
|
|
|
|
} else {
|
|
|
|
// value is < 100: use "92.1M" format
|
|
|
|
c = buf[0] = " 123456789"[u/10];
|
|
|
|
if (c != ' ') fmt = "0123456789";
|
|
|
|
buf[1] = fmt[u%10];
|
|
|
|
buf[2] = '.';
|
|
|
|
}
|
|
|
|
buf[3] = "0123456789"[v];
|
|
|
|
buf[4] = scale[idx]; /* typically scale = " kmgt..." */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert unsigned long long value into compact 4-char
|
|
|
|
* representation. Examples: "1234", "1.2k", " 27M", "123T"
|
|
|
|
* String is not terminated (buf[4] is untouched) */
|
|
|
|
void FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[5], const char *scale)
|
|
|
|
{
|
|
|
|
const char *fmt;
|
|
|
|
char c;
|
|
|
|
unsigned v, u, idx = 0;
|
|
|
|
|
|
|
|
if (ul > 9999) { // do not scale if 9999 or less
|
|
|
|
ul *= 10;
|
|
|
|
do {
|
|
|
|
ul /= 1024;
|
|
|
|
idx++;
|
|
|
|
} while (ul >= 10000);
|
|
|
|
}
|
|
|
|
v = ul; // ullong divisions are expensive, avoid them
|
|
|
|
|
|
|
|
fmt = " 123456789";
|
|
|
|
u = v / 10;
|
|
|
|
v = v % 10;
|
|
|
|
if (!idx) {
|
|
|
|
// 9999 or less: use "1234" format
|
|
|
|
// u is value/10, v is last digit
|
|
|
|
c = buf[0] = " 123456789"[u/100];
|
|
|
|
if (c != ' ') fmt = "0123456789";
|
|
|
|
c = buf[1] = fmt[u/10%10];
|
|
|
|
if (c != ' ') fmt = "0123456789";
|
|
|
|
buf[2] = fmt[u%10];
|
|
|
|
buf[3] = "0123456789"[v];
|
|
|
|
} else {
|
|
|
|
// u is value, v is 1/10ths (allows for 9.2M format)
|
|
|
|
if (u >= 10) {
|
|
|
|
// value is >= 10: use "123M', " 12M" formats
|
|
|
|
c = buf[0] = " 123456789"[u/100];
|
|
|
|
if (c != ' ') fmt = "0123456789";
|
|
|
|
v = u % 10;
|
|
|
|
u = u / 10;
|
|
|
|
buf[1] = fmt[u%10];
|
|
|
|
} else {
|
|
|
|
// value is < 10: use "9.2M" format
|
|
|
|
buf[0] = "0123456789"[u];
|
|
|
|
buf[1] = '.';
|
|
|
|
}
|
|
|
|
buf[2] = "0123456789"[v];
|
|
|
|
buf[3] = scale[idx]; /* typically scale = " kmgt..." */
|
|
|
|
}
|
|
|
|
}
|