diff options
author | Eric Andersen <andersen@codepoet.org> | 2001-06-13 08:02:45 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2001-06-13 08:02:45 +0000 |
commit | f429baca868b7f62ffdeefbfce41abd677f97876 (patch) | |
tree | 64925420925deb361a2024b9b362e5c987334124 /libbb/human_readable.c | |
parent | 17822cd60aaf9333a9895494edcf03a0037de54c (diff) | |
download | busybox-w32-f429baca868b7f62ffdeefbfce41abd677f97876.tar.gz busybox-w32-f429baca868b7f62ffdeefbfce41abd677f97876.tar.bz2 busybox-w32-f429baca868b7f62ffdeefbfce41abd677f97876.zip |
I reworked make_human_readable_str so it now has a sane interface,
and then fixed up df, du, and ls to use the new interface. I also
fixed up some formatting issues in ls while I was in there.
-Erik
Diffstat (limited to 'libbb/human_readable.c')
-rw-r--r-- | libbb/human_readable.c | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/libbb/human_readable.c b/libbb/human_readable.c index ff2175175..2cb887563 100644 --- a/libbb/human_readable.c +++ b/libbb/human_readable.c | |||
@@ -1,10 +1,8 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* | 2 | /* |
3 | * Utility routines. | 3 | * make_human_readable_str |
4 | * | 4 | * |
5 | * Copyright (C) tons of folks. Tracking down who wrote what | 5 | * Copyright (C) 1999-2001 Erik Andersen <andersee@debian.org> |
6 | * isn't something I'm going to worry about... If you wrote something | ||
7 | * here, please feel free to acknowledge your work. | ||
8 | * | 6 | * |
9 | * This program is free software; you can redistribute it and/or modify | 7 | * This program is free software; you can redistribute it and/or modify |
10 | * it under the terms of the GNU General Public License as published by | 8 | * it under the terms of the GNU General Public License as published by |
@@ -19,10 +17,6 @@ | |||
19 | * You should have received a copy of the GNU General Public License | 17 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software | 18 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 | * | ||
23 | * Based in part on code from sash, Copyright (c) 1999 by David I. Bell | ||
24 | * Permission has been granted to redistribute this code under the GPL. | ||
25 | * | ||
26 | */ | 20 | */ |
27 | 21 | ||
28 | #include <stdio.h> | 22 | #include <stdio.h> |
@@ -30,28 +24,38 @@ | |||
30 | 24 | ||
31 | 25 | ||
32 | 26 | ||
33 | const char *make_human_readable_str(unsigned long val, unsigned long hr) | 27 | const char *make_human_readable_str(unsigned long size, |
28 | unsigned long block_size, unsigned long display_unit) | ||
34 | { | 29 | { |
35 | int i=0; | ||
36 | static char str[10] = "\0"; | 30 | static char str[10] = "\0"; |
37 | static const char strings[] = { 'k', 'M', 'G', 'T', 0 }; | 31 | static const char strings[] = { 0, 'k', 'M', 'G', 'T', 0 }; |
38 | unsigned long divisor = 1; | ||
39 | 32 | ||
40 | if(val == 0) | 33 | if(size == 0 || block_size == 0) |
41 | return("0"); | 34 | return("0"); |
42 | if(hr) | 35 | |
43 | snprintf(str, 9, "%ld", val/hr); | 36 | if(display_unit) { |
44 | else { | 37 | snprintf(str, 9, "%ld", (size/display_unit)*block_size); |
45 | while(val >= divisor && i <= 4) { | 38 | } else { |
46 | divisor=divisor<<10, i++; | 39 | /* Ok, looks like they want us to autoscale */ |
47 | } | 40 | int i=0; |
48 | divisor=divisor>>10, i--; | 41 | unsigned long divisor = 1; |
49 | snprintf(str, 9, "%.1Lf%c", (long double)(val)/divisor, strings[i]); | 42 | long double result = size*block_size; |
43 | for(i=0; i <= 4; i++) { | ||
44 | divisor<<=10; | ||
45 | if (result <= divisor) { | ||
46 | divisor>>=10; | ||
47 | break; | ||
48 | } | ||
49 | } | ||
50 | result/=divisor; | ||
51 | if (result > 10) | ||
52 | snprintf(str, 9, "%.0Lf%c", result, strings[i]); | ||
53 | else | ||
54 | snprintf(str, 9, "%.1Lf%c", result, strings[i]); | ||
50 | } | 55 | } |
51 | return(str); | 56 | return(str); |
52 | } | 57 | } |
53 | 58 | ||
54 | |||
55 | /* END CODE */ | 59 | /* END CODE */ |
56 | /* | 60 | /* |
57 | Local Variables: | 61 | Local Variables: |