* representations (say, powers of 1024) and manipulating coefficients.
* The base ten "bytes" output could be handled similarly.
*
- * 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
+ * 2) This routine outputs a decimal point and a tenths digit when
+ * display_unit == 0. Hence, it isn't uncommon for the returned string
* to have a length of 5 or 6.
*
- * 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.
+ * If block_size is also 0, no decimal digits are printed.
*
- * Some code to omit the decimal point and tenths digit is sketched out
- * and "#if 0"'d below.
- *
- * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
*/
-
#include "libbb.h"
const char* FAST_FUNC make_human_readable_str(unsigned long long val,
'\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
};
- static char *str;
-
unsigned frac; /* 0..9 - the fractional digit */
const char *u;
const char *fmt;
u = unit_chars;
if (display_unit) {
- val += display_unit/2; /* Deal with rounding */
- val /= display_unit; /* Don't combine with the line above! */
+ val += display_unit/2; /* Deal with rounding */
+ val /= display_unit; /* Don't combine with the line above! */
/* will just print it as ulonglong (below) */
} else {
while ((val >= 1024)
#endif
}
- if (!str) {
- /* sufficient for any width of val */
- str = xmalloc(sizeof(val)*3 + 2 + 3);
- }
- sprintf(str, fmt, val, frac, *u);
- return str;
+ return auto_string(xasprintf(fmt, val, frac, *u));
}
/* 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)
+char* FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale)
{
const char *fmt;
char c;
buf[3] = "0123456789"[v];
buf[4] = scale[idx]; /* typically scale = " kmgt..." */
}
+ return buf + 5;
}
/* 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)
+char* FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale)
{
const char *fmt;
char c;
buf[2] = "0123456789"[v];
buf[3] = scale[idx]; /* typically scale = " kmgt..." */
}
+ return buf + 4;
}