summaryrefslogtreecommitdiff
path: root/libbb/human_readable.c
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2001-03-28 16:42:27 +0000
committerMatt Kraai <kraai@debian.org>2001-03-28 16:42:27 +0000
commit7cd0cfeab696a4e5b2794dd66541b6d2a6cc3663 (patch)
tree8733711c52f6dd894aa6bf955b295aae5149aa8d /libbb/human_readable.c
parentf957c779104d187a035b48d1fb9ce9acb7351001 (diff)
downloadbusybox-w32-7cd0cfeab696a4e5b2794dd66541b6d2a6cc3663.tar.gz
busybox-w32-7cd0cfeab696a4e5b2794dd66541b6d2a6cc3663.tar.bz2
busybox-w32-7cd0cfeab696a4e5b2794dd66541b6d2a6cc3663.zip
Rewrite to fix bug #1140 and make it slightly smaller.
Diffstat (limited to '')
-rw-r--r--libbb/human_readable.c40
1 files changed, 14 insertions, 26 deletions
diff --git a/libbb/human_readable.c b/libbb/human_readable.c
index ff2175175..1d7a90e55 100644
--- a/libbb/human_readable.c
+++ b/libbb/human_readable.c
@@ -28,35 +28,23 @@
28#include <stdio.h> 28#include <stdio.h>
29#include "libbb.h" 29#include "libbb.h"
30 30
31 31static char buffer[10];
32static const char *suffixes[] = { "", "k", "M", "G", "T" };
32 33
33const char *make_human_readable_str(unsigned long val, unsigned long hr) 34const char *make_human_readable_str(unsigned long val, unsigned long hr)
34{ 35{
35 int i=0; 36 int suffix, base;
36 static char str[10] = "\0";
37 static const char strings[] = { 'k', 'M', 'G', 'T', 0 };
38 unsigned long divisor = 1;
39 37
40 if(val == 0) 38 for (suffix = 0, base = 1; suffix < 5; suffix++, base <<= 10) {
41 return("0"); 39 if (val < (base << 10)) {
42 if(hr) 40 if (suffix && val < 10 * base)
43 snprintf(str, 9, "%ld", val/hr); 41 sprintf(buffer, "%lu.%lu%s", val / base,
44 else { 42 (val % base) * 10 / base, suffixes[suffix]);
45 while(val >= divisor && i <= 4) { 43 else
46 divisor=divisor<<10, i++; 44 sprintf(buffer, "%lu%s", val / base, suffixes[suffix]);
47 } 45 break;
48 divisor=divisor>>10, i--; 46 }
49 snprintf(str, 9, "%.1Lf%c", (long double)(val)/divisor, strings[i]);
50 } 47 }
51 return(str);
52}
53
54 48
55/* END CODE */ 49 return buffer;
56/* 50}
57Local Variables:
58c-file-style: "linux"
59c-basic-offset: 4
60tab-width: 4
61End:
62*/