summaryrefslogtreecommitdiff
path: root/libbb/human_readable.c
diff options
context:
space:
mode:
authorDenis Vlasenko2008-08-28 22:42:52 +0000
committerDenis Vlasenko2008-08-28 22:42:52 +0000
commitd66aa3c701ffb83343239e71a8b294407ff5df86 (patch)
treeeeebccfe994962aa8a33312c8726eac5adf63f5a /libbb/human_readable.c
parent3b80cac953b0627ba9b3337d3f9386678d436871 (diff)
downloadbusybox-d66aa3c701ffb83343239e71a8b294407ff5df86.zip
busybox-d66aa3c701ffb83343239e71a8b294407ff5df86.tar.gz
df: add support for more options, add some coreutils 6.10 compat.
by Bernhard Reutner-Fischer function old new delta df_main 664 795 +131 packed_usage 24812 24862 +50 make_human_readable_str 213 262 +49 static.ignored_mounts - 8 +8 static.unit_chars - 7 +7 static.zero_and_units 6 - -6 ------------------------------------------------------------------------------ (add/remove: 2/1 grow/shrink: 3/0 up/down: 245/-6) Total: 239 bytes
Diffstat (limited to 'libbb/human_readable.c')
-rw-r--r--libbb/human_readable.c33
1 files changed, 21 insertions, 12 deletions
diff --git a/libbb/human_readable.c b/libbb/human_readable.c
index dad26ed..61c8567 100644
--- a/libbb/human_readable.c
+++ b/libbb/human_readable.c
@@ -32,7 +32,9 @@ const char* FAST_FUNC make_human_readable_str(unsigned long long size,
unsigned long block_size, unsigned long display_unit)
{
/* The code will adjust for additional (appended) units */
- static const char zero_and_units[] ALIGN1 = { '0', 0, 'k', 'M', 'G', 'T' };
+ static const char unit_chars[] ALIGN1 = {
+ '\0', 'K', 'M', 'G', 'T', 'P', 'E'
+ };
static const char fmt[] ALIGN1 = "%llu";
static const char fmt_tenths[] ALIGN1 = "%llu.%d%c";
@@ -42,26 +44,33 @@ const char* FAST_FUNC make_human_readable_str(unsigned long long size,
int frac;
const char *u;
const char *f;
+ smallint no_tenths;
- u = zero_and_units;
- f = fmt;
- frac = 0;
+ if (size == 0)
+ return "0";
- val = size * block_size;
- if (val == 0) {
- return u;
+ /* If block_size is 0 then do not print tenths */
+ no_tenths = 0;
+ if (block_size == 0) {
+ no_tenths = 1;
+ block_size = 1;
}
+ u = unit_chars;
+ val = size * block_size;
+ f = fmt;
+ frac = 0;
+
if (display_unit) {
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 {
- ++u;
while ((val >= 1024)
- && (u < zero_and_units + sizeof(zero_and_units) - 1)
+ && (u < unit_chars + sizeof(unit_chars) - 1)
) {
f = fmt_tenths;
- ++u;
+ u++;
frac = (((int)(val % 1024)) * 10 + 1024/2) / 1024;
val /= 1024;
}
@@ -69,9 +78,9 @@ const char* FAST_FUNC make_human_readable_str(unsigned long long size,
++val;
frac = 0;
}
-#if 0
+#if 1
/* Sample code to omit decimal point and tenths digit. */
- if (/* no_tenths */ 1) {
+ if (no_tenths) {
if (frac >= 5) {
++val;
}