aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-04-03 23:14:29 +0000
committerEric Andersen <andersen@codepoet.org>2001-04-03 23:14:29 +0000
commit91c9388715182a71173f2da71d74173221460412 (patch)
tree93fed8e5cfa179b95e937f5a754a3ba1a9587422 /libbb
parent24073c76658b93172df8a7a397b84c91a789008a (diff)
downloadbusybox-w32-91c9388715182a71173f2da71d74173221460412.tar.gz
busybox-w32-91c9388715182a71173f2da71d74173221460412.tar.bz2
busybox-w32-91c9388715182a71173f2da71d74173221460412.zip
Place a temporary bandaid on the ls/du/df human-readable issue. This method is
not going to scale up as well as I would like, and Matt Kraai and I have discussed a better long term solution. But for now this will at least make all the human-readable apps give correct answers. Please test the human readable/non-human readable options on your systems!!! -Erik
Diffstat (limited to 'libbb')
-rw-r--r--libbb/human_readable.c47
1 files changed, 28 insertions, 19 deletions
diff --git a/libbb/human_readable.c b/libbb/human_readable.c
index 36783fac7..ff2175175 100644
--- a/libbb/human_readable.c
+++ b/libbb/human_readable.c
@@ -28,26 +28,35 @@
28#include <stdio.h> 28#include <stdio.h>
29#include "libbb.h" 29#include "libbb.h"
30 30
31static char buffer[10];
32static const char *suffixes[] = { "", "k", "M", "G", "T" };
33 31
34const char *make_human_readable_str(unsigned long val, unsigned long not_hr)
35{
36 int suffix, base;
37 32
38 if (not_hr) 33const char *make_human_readable_str(unsigned long val, unsigned long hr)
39 sprintf(buffer, "%lu", val); 34{
40 else 35 int i=0;
41 for (suffix = 0, base = 1; suffix < 5; suffix++, base <<= 10) { 36 static char str[10] = "\0";
42 if (val < (base << 10)) { 37 static const char strings[] = { 'k', 'M', 'G', 'T', 0 };
43 if (suffix && val < 10 * base) 38 unsigned long divisor = 1;
44 sprintf(buffer, "%lu.%lu%s", val / base,
45 (val % base) * 10 / base, suffixes[suffix]);
46 else
47 sprintf(buffer, "%lu%s", val / base, suffixes[suffix]);
48 break;
49 }
50 }
51 39
52 return buffer; 40 if(val == 0)
41 return("0");
42 if(hr)
43 snprintf(str, 9, "%ld", val/hr);
44 else {
45 while(val >= divisor && i <= 4) {
46 divisor=divisor<<10, i++;
47 }
48 divisor=divisor>>10, i--;
49 snprintf(str, 9, "%.1Lf%c", (long double)(val)/divisor, strings[i]);
50 }
51 return(str);
53} 52}
53
54
55/* END CODE */
56/*
57Local Variables:
58c-file-style: "linux"
59c-basic-offset: 4
60tab-width: 4
61End:
62*/