aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/df.c31
-rw-r--r--coreutils/du.c15
-rw-r--r--coreutils/ls.c11
-rw-r--r--df.c31
-rw-r--r--du.c15
-rw-r--r--include/libbb.h2
-rw-r--r--libbb/human_readable.c48
-rw-r--r--libbb/libbb.h2
-rw-r--r--ls.c11
9 files changed, 58 insertions, 108 deletions
diff --git a/coreutils/df.c b/coreutils/df.c
index df6874433..6a81086cb 100644
--- a/coreutils/df.c
+++ b/coreutils/df.c
@@ -32,7 +32,7 @@
32 32
33extern const char mtab_file[]; /* Defined in utility.c */ 33extern const char mtab_file[]; /* Defined in utility.c */
34#ifdef BB_FEATURE_HUMAN_READABLE 34#ifdef BB_FEATURE_HUMAN_READABLE
35static unsigned long df_disp_hr = KILOBYTE; 35static unsigned long df_disp_hr = 1;
36#endif 36#endif
37 37
38static int do_df(char *device, const char *mount_point) 38static int do_df(char *device, const char *mount_point)
@@ -40,9 +40,6 @@ static int do_df(char *device, const char *mount_point)
40 struct statfs s; 40 struct statfs s;
41 long blocks_used; 41 long blocks_used;
42 long blocks_percent_used; 42 long blocks_percent_used;
43#ifdef BB_FEATURE_HUMAN_READABLE
44 long base;
45#endif
46 43
47 if (statfs(mount_point, &s) != 0) { 44 if (statfs(mount_point, &s) != 0) {
48 perror_msg("%s", mount_point); 45 perror_msg("%s", mount_point);
@@ -65,27 +62,15 @@ static int do_df(char *device, const char *mount_point)
65 return FALSE; 62 return FALSE;
66 } 63 }
67#ifdef BB_FEATURE_HUMAN_READABLE 64#ifdef BB_FEATURE_HUMAN_READABLE
68 switch (df_disp_hr) {
69 case MEGABYTE:
70 base = KILOBYTE;
71 break;
72 case KILOBYTE:
73 base = 1;
74 break;
75 default:
76 base = 0;
77 }
78 printf("%-20s %9s ", device, 65 printf("%-20s %9s ", device,
79 make_human_readable_str((unsigned long)(s.f_blocks * 66 make_human_readable_str(s.f_blocks, s.f_bsize/KILOBYTE, df_disp_hr));
80 (s.f_bsize/(double)KILOBYTE)), base)); 67
81 printf("%9s ", 68 printf("%9s ",
82 make_human_readable_str((unsigned long)( 69 make_human_readable_str( (s.f_blocks - s.f_bfree), s.f_bsize/KILOBYTE, df_disp_hr));
83 (s.f_blocks - s.f_bfree) * 70
84 (s.f_bsize/(double)KILOBYTE)), base));
85 printf("%9s %3ld%% %s\n", 71 printf("%9s %3ld%% %s\n",
86 make_human_readable_str((unsigned long)(s.f_bavail * 72 make_human_readable_str(s.f_bavail, s.f_bsize/KILOBYTE, df_disp_hr),
87 (s.f_bsize/(double)KILOBYTE)), base), 73 blocks_percent_used, mount_point);
88 blocks_percent_used, mount_point);
89#else 74#else
90 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n", 75 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
91 device, 76 device,
@@ -119,7 +104,7 @@ extern int df_main(int argc, char **argv)
119 strcpy(disp_units_hdr, " Size"); 104 strcpy(disp_units_hdr, " Size");
120 break; 105 break;
121 case 'm': 106 case 'm':
122 df_disp_hr = MEGABYTE; 107 df_disp_hr = KILOBYTE;
123 strcpy(disp_units_hdr, "1M-blocks"); 108 strcpy(disp_units_hdr, "1M-blocks");
124 break; 109 break;
125#endif 110#endif
diff --git a/coreutils/du.c b/coreutils/du.c
index fd19855e1..1966e3559 100644
--- a/coreutils/du.c
+++ b/coreutils/du.c
@@ -46,19 +46,8 @@ static Display *print;
46 46
47static void print_normal(long size, char *filename) 47static void print_normal(long size, char *filename)
48{ 48{
49 unsigned long base;
50#ifdef BB_FEATURE_HUMAN_READABLE 49#ifdef BB_FEATURE_HUMAN_READABLE
51 switch (disp_hr) { 50 printf("%s\t%s\n", make_human_readable_str(size<<10, 1, disp_hr), filename);
52 case MEGABYTE:
53 base = KILOBYTE;
54 break;
55 case KILOBYTE:
56 base = 1;
57 break;
58 default:
59 base = 0;
60 }
61 printf("%s\t%s\n", make_human_readable_str(size, base), filename);
62#else 51#else
63 printf("%ld\t%s\n", size, filename); 52 printf("%ld\t%s\n", size, filename);
64#endif 53#endif
@@ -259,7 +248,7 @@ int du_main(int argc, char **argv)
259 return status; 248 return status;
260} 249}
261 250
262/* $Id: du.c,v 1.48 2001/06/01 21:47:15 andersen Exp $ */ 251/* $Id: du.c,v 1.49 2001/06/13 08:02:44 andersen Exp $ */
263/* 252/*
264Local Variables: 253Local Variables:
265c-file-style: "linux" 254c-file-style: "linux"
diff --git a/coreutils/ls.c b/coreutils/ls.c
index ec8e216fa..c54b6a42f 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -610,8 +610,8 @@ static int list_single(struct dnode *dn)
610 break; 610 break;
611 case LIST_BLOCKS: 611 case LIST_BLOCKS:
612#ifdef BB_FEATURE_HUMAN_READABLE 612#ifdef BB_FEATURE_HUMAN_READABLE
613 fprintf(stdout, "%5s ", make_human_readable_str(dn->dstat.st_blocks>>1, 613 fprintf(stdout, "%4s ", make_human_readable_str(dn->dstat.st_blocks>>1,
614 (ls_disp_hr==TRUE)? 0: 1)); 614 KILOBYTE, (ls_disp_hr==TRUE)? 0: 1));
615#else 615#else
616#if _FILE_OFFSET_BITS == 64 616#if _FILE_OFFSET_BITS == 64
617 printf("%4lld ", dn->dstat.st_blocks>>1); 617 printf("%4lld ", dn->dstat.st_blocks>>1);
@@ -622,7 +622,7 @@ static int list_single(struct dnode *dn)
622 column += 5; 622 column += 5;
623 break; 623 break;
624 case LIST_MODEBITS: 624 case LIST_MODEBITS:
625 printf("%10s", (char *)mode_string(dn->dstat.st_mode)); 625 printf("%-10s ", (char *)mode_string(dn->dstat.st_mode));
626 column += 10; 626 column += 10;
627 break; 627 break;
628 case LIST_NLINKS: 628 case LIST_NLINKS:
@@ -634,7 +634,7 @@ static int list_single(struct dnode *dn)
634 my_getpwuid(scratch, dn->dstat.st_uid); 634 my_getpwuid(scratch, dn->dstat.st_uid);
635 printf("%-8.8s ", scratch); 635 printf("%-8.8s ", scratch);
636 my_getgrgid(scratch, dn->dstat.st_gid); 636 my_getgrgid(scratch, dn->dstat.st_gid);
637 printf("%-8.8s", scratch); 637 printf("%-8.8s ", scratch);
638 column += 17; 638 column += 17;
639 break; 639 break;
640#endif 640#endif
@@ -649,8 +649,7 @@ static int list_single(struct dnode *dn)
649 } else { 649 } else {
650#ifdef BB_FEATURE_HUMAN_READABLE 650#ifdef BB_FEATURE_HUMAN_READABLE
651 if (ls_disp_hr==TRUE) { 651 if (ls_disp_hr==TRUE) {
652 fprintf(stdout, "%9s ", make_human_readable_str( 652 fprintf(stdout, "%8s ", make_human_readable_str(dn->dstat.st_size, 1, 0));
653 dn->dstat.st_size>>10, 0));
654 } else 653 } else
655#endif 654#endif
656 { 655 {
diff --git a/df.c b/df.c
index df6874433..6a81086cb 100644
--- a/df.c
+++ b/df.c
@@ -32,7 +32,7 @@
32 32
33extern const char mtab_file[]; /* Defined in utility.c */ 33extern const char mtab_file[]; /* Defined in utility.c */
34#ifdef BB_FEATURE_HUMAN_READABLE 34#ifdef BB_FEATURE_HUMAN_READABLE
35static unsigned long df_disp_hr = KILOBYTE; 35static unsigned long df_disp_hr = 1;
36#endif 36#endif
37 37
38static int do_df(char *device, const char *mount_point) 38static int do_df(char *device, const char *mount_point)
@@ -40,9 +40,6 @@ static int do_df(char *device, const char *mount_point)
40 struct statfs s; 40 struct statfs s;
41 long blocks_used; 41 long blocks_used;
42 long blocks_percent_used; 42 long blocks_percent_used;
43#ifdef BB_FEATURE_HUMAN_READABLE
44 long base;
45#endif
46 43
47 if (statfs(mount_point, &s) != 0) { 44 if (statfs(mount_point, &s) != 0) {
48 perror_msg("%s", mount_point); 45 perror_msg("%s", mount_point);
@@ -65,27 +62,15 @@ static int do_df(char *device, const char *mount_point)
65 return FALSE; 62 return FALSE;
66 } 63 }
67#ifdef BB_FEATURE_HUMAN_READABLE 64#ifdef BB_FEATURE_HUMAN_READABLE
68 switch (df_disp_hr) {
69 case MEGABYTE:
70 base = KILOBYTE;
71 break;
72 case KILOBYTE:
73 base = 1;
74 break;
75 default:
76 base = 0;
77 }
78 printf("%-20s %9s ", device, 65 printf("%-20s %9s ", device,
79 make_human_readable_str((unsigned long)(s.f_blocks * 66 make_human_readable_str(s.f_blocks, s.f_bsize/KILOBYTE, df_disp_hr));
80 (s.f_bsize/(double)KILOBYTE)), base)); 67
81 printf("%9s ", 68 printf("%9s ",
82 make_human_readable_str((unsigned long)( 69 make_human_readable_str( (s.f_blocks - s.f_bfree), s.f_bsize/KILOBYTE, df_disp_hr));
83 (s.f_blocks - s.f_bfree) * 70
84 (s.f_bsize/(double)KILOBYTE)), base));
85 printf("%9s %3ld%% %s\n", 71 printf("%9s %3ld%% %s\n",
86 make_human_readable_str((unsigned long)(s.f_bavail * 72 make_human_readable_str(s.f_bavail, s.f_bsize/KILOBYTE, df_disp_hr),
87 (s.f_bsize/(double)KILOBYTE)), base), 73 blocks_percent_used, mount_point);
88 blocks_percent_used, mount_point);
89#else 74#else
90 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n", 75 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
91 device, 76 device,
@@ -119,7 +104,7 @@ extern int df_main(int argc, char **argv)
119 strcpy(disp_units_hdr, " Size"); 104 strcpy(disp_units_hdr, " Size");
120 break; 105 break;
121 case 'm': 106 case 'm':
122 df_disp_hr = MEGABYTE; 107 df_disp_hr = KILOBYTE;
123 strcpy(disp_units_hdr, "1M-blocks"); 108 strcpy(disp_units_hdr, "1M-blocks");
124 break; 109 break;
125#endif 110#endif
diff --git a/du.c b/du.c
index fd19855e1..1966e3559 100644
--- a/du.c
+++ b/du.c
@@ -46,19 +46,8 @@ static Display *print;
46 46
47static void print_normal(long size, char *filename) 47static void print_normal(long size, char *filename)
48{ 48{
49 unsigned long base;
50#ifdef BB_FEATURE_HUMAN_READABLE 49#ifdef BB_FEATURE_HUMAN_READABLE
51 switch (disp_hr) { 50 printf("%s\t%s\n", make_human_readable_str(size<<10, 1, disp_hr), filename);
52 case MEGABYTE:
53 base = KILOBYTE;
54 break;
55 case KILOBYTE:
56 base = 1;
57 break;
58 default:
59 base = 0;
60 }
61 printf("%s\t%s\n", make_human_readable_str(size, base), filename);
62#else 51#else
63 printf("%ld\t%s\n", size, filename); 52 printf("%ld\t%s\n", size, filename);
64#endif 53#endif
@@ -259,7 +248,7 @@ int du_main(int argc, char **argv)
259 return status; 248 return status;
260} 249}
261 250
262/* $Id: du.c,v 1.48 2001/06/01 21:47:15 andersen Exp $ */ 251/* $Id: du.c,v 1.49 2001/06/13 08:02:44 andersen Exp $ */
263/* 252/*
264Local Variables: 253Local Variables:
265c-file-style: "linux" 254c-file-style: "linux"
diff --git a/include/libbb.h b/include/libbb.h
index f24a38109..893a9f07f 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -198,12 +198,12 @@ struct sysinfo {
198}; 198};
199extern int sysinfo (struct sysinfo* info); 199extern int sysinfo (struct sysinfo* info);
200 200
201const char *make_human_readable_str(unsigned long val, unsigned long not_hr);
202enum { 201enum {
203 KILOBYTE = 1024, 202 KILOBYTE = 1024,
204 MEGABYTE = (KILOBYTE*1024), 203 MEGABYTE = (KILOBYTE*1024),
205 GIGABYTE = (MEGABYTE*1024) 204 GIGABYTE = (MEGABYTE*1024)
206}; 205};
206const char *make_human_readable_str(unsigned long size, unsigned long block_size, unsigned long display_unit);
207 207
208int ask_confirmation(void); 208int ask_confirmation(void);
209int klogctl(int type, char * b, int len); 209int klogctl(int type, char * b, int len);
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
33const char *make_human_readable_str(unsigned long val, unsigned long hr) 27const 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/*
57Local Variables: 61Local 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};
199extern int sysinfo (struct sysinfo* info); 199extern int sysinfo (struct sysinfo* info);
200 200
201const char *make_human_readable_str(unsigned long val, unsigned long not_hr);
202enum { 201enum {
203 KILOBYTE = 1024, 202 KILOBYTE = 1024,
204 MEGABYTE = (KILOBYTE*1024), 203 MEGABYTE = (KILOBYTE*1024),
205 GIGABYTE = (MEGABYTE*1024) 204 GIGABYTE = (MEGABYTE*1024)
206}; 205};
206const char *make_human_readable_str(unsigned long size, unsigned long block_size, unsigned long display_unit);
207 207
208int ask_confirmation(void); 208int ask_confirmation(void);
209int klogctl(int type, char * b, int len); 209int klogctl(int type, char * b, int len);
diff --git a/ls.c b/ls.c
index ec8e216fa..c54b6a42f 100644
--- a/ls.c
+++ b/ls.c
@@ -610,8 +610,8 @@ static int list_single(struct dnode *dn)
610 break; 610 break;
611 case LIST_BLOCKS: 611 case LIST_BLOCKS:
612#ifdef BB_FEATURE_HUMAN_READABLE 612#ifdef BB_FEATURE_HUMAN_READABLE
613 fprintf(stdout, "%5s ", make_human_readable_str(dn->dstat.st_blocks>>1, 613 fprintf(stdout, "%4s ", make_human_readable_str(dn->dstat.st_blocks>>1,
614 (ls_disp_hr==TRUE)? 0: 1)); 614 KILOBYTE, (ls_disp_hr==TRUE)? 0: 1));
615#else 615#else
616#if _FILE_OFFSET_BITS == 64 616#if _FILE_OFFSET_BITS == 64
617 printf("%4lld ", dn->dstat.st_blocks>>1); 617 printf("%4lld ", dn->dstat.st_blocks>>1);
@@ -622,7 +622,7 @@ static int list_single(struct dnode *dn)
622 column += 5; 622 column += 5;
623 break; 623 break;
624 case LIST_MODEBITS: 624 case LIST_MODEBITS:
625 printf("%10s", (char *)mode_string(dn->dstat.st_mode)); 625 printf("%-10s ", (char *)mode_string(dn->dstat.st_mode));
626 column += 10; 626 column += 10;
627 break; 627 break;
628 case LIST_NLINKS: 628 case LIST_NLINKS:
@@ -634,7 +634,7 @@ static int list_single(struct dnode *dn)
634 my_getpwuid(scratch, dn->dstat.st_uid); 634 my_getpwuid(scratch, dn->dstat.st_uid);
635 printf("%-8.8s ", scratch); 635 printf("%-8.8s ", scratch);
636 my_getgrgid(scratch, dn->dstat.st_gid); 636 my_getgrgid(scratch, dn->dstat.st_gid);
637 printf("%-8.8s", scratch); 637 printf("%-8.8s ", scratch);
638 column += 17; 638 column += 17;
639 break; 639 break;
640#endif 640#endif
@@ -649,8 +649,7 @@ static int list_single(struct dnode *dn)
649 } else { 649 } else {
650#ifdef BB_FEATURE_HUMAN_READABLE 650#ifdef BB_FEATURE_HUMAN_READABLE
651 if (ls_disp_hr==TRUE) { 651 if (ls_disp_hr==TRUE) {
652 fprintf(stdout, "%9s ", make_human_readable_str( 652 fprintf(stdout, "%8s ", make_human_readable_str(dn->dstat.st_size, 1, 0));
653 dn->dstat.st_size>>10, 0));
654 } else 653 } else
655#endif 654#endif
656 { 655 {