diff options
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/human_readable.c | 48 | ||||
-rw-r--r-- | libbb/libbb.h | 2 |
2 files changed, 27 insertions, 23 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: |
diff --git a/libbb/libbb.h b/libbb/libbb.h index f24a38109..893a9f07f 100644 --- a/libbb/libbb.h +++ b/libbb/libbb.h | |||
@@ -198,12 +198,12 @@ struct sysinfo { | |||
198 | }; | 198 | }; |
199 | extern int sysinfo (struct sysinfo* info); | 199 | extern int sysinfo (struct sysinfo* info); |
200 | 200 | ||
201 | const char *make_human_readable_str(unsigned long val, unsigned long not_hr); | ||
202 | enum { | 201 | enum { |
203 | KILOBYTE = 1024, | 202 | KILOBYTE = 1024, |
204 | MEGABYTE = (KILOBYTE*1024), | 203 | MEGABYTE = (KILOBYTE*1024), |
205 | GIGABYTE = (MEGABYTE*1024) | 204 | GIGABYTE = (MEGABYTE*1024) |
206 | }; | 205 | }; |
206 | const char *make_human_readable_str(unsigned long size, unsigned long block_size, unsigned long display_unit); | ||
207 | 207 | ||
208 | int ask_confirmation(void); | 208 | int ask_confirmation(void); |
209 | int klogctl(int type, char * b, int len); | 209 | int klogctl(int type, char * b, int len); |