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
This commit is contained in:
Eric Andersen 2001-06-13 08:02:45 +00:00
parent 17822cd60a
commit f429baca86
9 changed files with 58 additions and 108 deletions

View File

@ -32,7 +32,7 @@
extern const char mtab_file[]; /* Defined in utility.c */ extern const char mtab_file[]; /* Defined in utility.c */
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
static unsigned long df_disp_hr = KILOBYTE; static unsigned long df_disp_hr = 1;
#endif #endif
static int do_df(char *device, const char *mount_point) static int do_df(char *device, const char *mount_point)
@ -40,9 +40,6 @@ static int do_df(char *device, const char *mount_point)
struct statfs s; struct statfs s;
long blocks_used; long blocks_used;
long blocks_percent_used; long blocks_percent_used;
#ifdef BB_FEATURE_HUMAN_READABLE
long base;
#endif
if (statfs(mount_point, &s) != 0) { if (statfs(mount_point, &s) != 0) {
perror_msg("%s", mount_point); perror_msg("%s", mount_point);
@ -65,27 +62,15 @@ static int do_df(char *device, const char *mount_point)
return FALSE; return FALSE;
} }
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
switch (df_disp_hr) {
case MEGABYTE:
base = KILOBYTE;
break;
case KILOBYTE:
base = 1;
break;
default:
base = 0;
}
printf("%-20s %9s ", device, printf("%-20s %9s ", device,
make_human_readable_str((unsigned long)(s.f_blocks * make_human_readable_str(s.f_blocks, s.f_bsize/KILOBYTE, df_disp_hr));
(s.f_bsize/(double)KILOBYTE)), base));
printf("%9s ", printf("%9s ",
make_human_readable_str((unsigned long)( make_human_readable_str( (s.f_blocks - s.f_bfree), s.f_bsize/KILOBYTE, df_disp_hr));
(s.f_blocks - s.f_bfree) *
(s.f_bsize/(double)KILOBYTE)), base));
printf("%9s %3ld%% %s\n", printf("%9s %3ld%% %s\n",
make_human_readable_str((unsigned long)(s.f_bavail * make_human_readable_str(s.f_bavail, s.f_bsize/KILOBYTE, df_disp_hr),
(s.f_bsize/(double)KILOBYTE)), base), blocks_percent_used, mount_point);
blocks_percent_used, mount_point);
#else #else
printf("%-20s %9ld %9ld %9ld %3ld%% %s\n", printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
device, device,
@ -119,7 +104,7 @@ extern int df_main(int argc, char **argv)
strcpy(disp_units_hdr, " Size"); strcpy(disp_units_hdr, " Size");
break; break;
case 'm': case 'm':
df_disp_hr = MEGABYTE; df_disp_hr = KILOBYTE;
strcpy(disp_units_hdr, "1M-blocks"); strcpy(disp_units_hdr, "1M-blocks");
break; break;
#endif #endif

View File

@ -46,19 +46,8 @@ static Display *print;
static void print_normal(long size, char *filename) static void print_normal(long size, char *filename)
{ {
unsigned long base;
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
switch (disp_hr) { printf("%s\t%s\n", make_human_readable_str(size<<10, 1, disp_hr), filename);
case MEGABYTE:
base = KILOBYTE;
break;
case KILOBYTE:
base = 1;
break;
default:
base = 0;
}
printf("%s\t%s\n", make_human_readable_str(size, base), filename);
#else #else
printf("%ld\t%s\n", size, filename); printf("%ld\t%s\n", size, filename);
#endif #endif
@ -259,7 +248,7 @@ int du_main(int argc, char **argv)
return status; return status;
} }
/* $Id: du.c,v 1.48 2001/06/01 21:47:15 andersen Exp $ */ /* $Id: du.c,v 1.49 2001/06/13 08:02:44 andersen Exp $ */
/* /*
Local Variables: Local Variables:
c-file-style: "linux" c-file-style: "linux"

View File

@ -610,8 +610,8 @@ static int list_single(struct dnode *dn)
break; break;
case LIST_BLOCKS: case LIST_BLOCKS:
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
fprintf(stdout, "%5s ", make_human_readable_str(dn->dstat.st_blocks>>1, fprintf(stdout, "%4s ", make_human_readable_str(dn->dstat.st_blocks>>1,
(ls_disp_hr==TRUE)? 0: 1)); KILOBYTE, (ls_disp_hr==TRUE)? 0: 1));
#else #else
#if _FILE_OFFSET_BITS == 64 #if _FILE_OFFSET_BITS == 64
printf("%4lld ", dn->dstat.st_blocks>>1); printf("%4lld ", dn->dstat.st_blocks>>1);
@ -622,7 +622,7 @@ static int list_single(struct dnode *dn)
column += 5; column += 5;
break; break;
case LIST_MODEBITS: case LIST_MODEBITS:
printf("%10s", (char *)mode_string(dn->dstat.st_mode)); printf("%-10s ", (char *)mode_string(dn->dstat.st_mode));
column += 10; column += 10;
break; break;
case LIST_NLINKS: case LIST_NLINKS:
@ -634,7 +634,7 @@ static int list_single(struct dnode *dn)
my_getpwuid(scratch, dn->dstat.st_uid); my_getpwuid(scratch, dn->dstat.st_uid);
printf("%-8.8s ", scratch); printf("%-8.8s ", scratch);
my_getgrgid(scratch, dn->dstat.st_gid); my_getgrgid(scratch, dn->dstat.st_gid);
printf("%-8.8s", scratch); printf("%-8.8s ", scratch);
column += 17; column += 17;
break; break;
#endif #endif
@ -649,8 +649,7 @@ static int list_single(struct dnode *dn)
} else { } else {
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
if (ls_disp_hr==TRUE) { if (ls_disp_hr==TRUE) {
fprintf(stdout, "%9s ", make_human_readable_str( fprintf(stdout, "%8s ", make_human_readable_str(dn->dstat.st_size, 1, 0));
dn->dstat.st_size>>10, 0));
} else } else
#endif #endif
{ {

31
df.c
View File

@ -32,7 +32,7 @@
extern const char mtab_file[]; /* Defined in utility.c */ extern const char mtab_file[]; /* Defined in utility.c */
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
static unsigned long df_disp_hr = KILOBYTE; static unsigned long df_disp_hr = 1;
#endif #endif
static int do_df(char *device, const char *mount_point) static int do_df(char *device, const char *mount_point)
@ -40,9 +40,6 @@ static int do_df(char *device, const char *mount_point)
struct statfs s; struct statfs s;
long blocks_used; long blocks_used;
long blocks_percent_used; long blocks_percent_used;
#ifdef BB_FEATURE_HUMAN_READABLE
long base;
#endif
if (statfs(mount_point, &s) != 0) { if (statfs(mount_point, &s) != 0) {
perror_msg("%s", mount_point); perror_msg("%s", mount_point);
@ -65,27 +62,15 @@ static int do_df(char *device, const char *mount_point)
return FALSE; return FALSE;
} }
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
switch (df_disp_hr) {
case MEGABYTE:
base = KILOBYTE;
break;
case KILOBYTE:
base = 1;
break;
default:
base = 0;
}
printf("%-20s %9s ", device, printf("%-20s %9s ", device,
make_human_readable_str((unsigned long)(s.f_blocks * make_human_readable_str(s.f_blocks, s.f_bsize/KILOBYTE, df_disp_hr));
(s.f_bsize/(double)KILOBYTE)), base));
printf("%9s ", printf("%9s ",
make_human_readable_str((unsigned long)( make_human_readable_str( (s.f_blocks - s.f_bfree), s.f_bsize/KILOBYTE, df_disp_hr));
(s.f_blocks - s.f_bfree) *
(s.f_bsize/(double)KILOBYTE)), base));
printf("%9s %3ld%% %s\n", printf("%9s %3ld%% %s\n",
make_human_readable_str((unsigned long)(s.f_bavail * make_human_readable_str(s.f_bavail, s.f_bsize/KILOBYTE, df_disp_hr),
(s.f_bsize/(double)KILOBYTE)), base), blocks_percent_used, mount_point);
blocks_percent_used, mount_point);
#else #else
printf("%-20s %9ld %9ld %9ld %3ld%% %s\n", printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
device, device,
@ -119,7 +104,7 @@ extern int df_main(int argc, char **argv)
strcpy(disp_units_hdr, " Size"); strcpy(disp_units_hdr, " Size");
break; break;
case 'm': case 'm':
df_disp_hr = MEGABYTE; df_disp_hr = KILOBYTE;
strcpy(disp_units_hdr, "1M-blocks"); strcpy(disp_units_hdr, "1M-blocks");
break; break;
#endif #endif

15
du.c
View File

@ -46,19 +46,8 @@ static Display *print;
static void print_normal(long size, char *filename) static void print_normal(long size, char *filename)
{ {
unsigned long base;
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
switch (disp_hr) { printf("%s\t%s\n", make_human_readable_str(size<<10, 1, disp_hr), filename);
case MEGABYTE:
base = KILOBYTE;
break;
case KILOBYTE:
base = 1;
break;
default:
base = 0;
}
printf("%s\t%s\n", make_human_readable_str(size, base), filename);
#else #else
printf("%ld\t%s\n", size, filename); printf("%ld\t%s\n", size, filename);
#endif #endif
@ -259,7 +248,7 @@ int du_main(int argc, char **argv)
return status; return status;
} }
/* $Id: du.c,v 1.48 2001/06/01 21:47:15 andersen Exp $ */ /* $Id: du.c,v 1.49 2001/06/13 08:02:44 andersen Exp $ */
/* /*
Local Variables: Local Variables:
c-file-style: "linux" c-file-style: "linux"

View File

@ -198,12 +198,12 @@ struct sysinfo {
}; };
extern int sysinfo (struct sysinfo* info); extern int sysinfo (struct sysinfo* info);
const char *make_human_readable_str(unsigned long val, unsigned long not_hr);
enum { enum {
KILOBYTE = 1024, KILOBYTE = 1024,
MEGABYTE = (KILOBYTE*1024), MEGABYTE = (KILOBYTE*1024),
GIGABYTE = (MEGABYTE*1024) GIGABYTE = (MEGABYTE*1024)
}; };
const char *make_human_readable_str(unsigned long size, unsigned long block_size, unsigned long display_unit);
int ask_confirmation(void); int ask_confirmation(void);
int klogctl(int type, char * b, int len); int klogctl(int type, char * b, int len);

View File

@ -1,10 +1,8 @@
/* vi: set sw=4 ts=4: */ /* vi: set sw=4 ts=4: */
/* /*
* Utility routines. * make_human_readable_str
* *
* Copyright (C) tons of folks. Tracking down who wrote what * Copyright (C) 1999-2001 Erik Andersen <andersee@debian.org>
* isn't something I'm going to worry about... If you wrote something
* here, please feel free to acknowledge your work.
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -19,10 +17,6 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Based in part on code from sash, Copyright (c) 1999 by David I. Bell
* Permission has been granted to redistribute this code under the GPL.
*
*/ */
#include <stdio.h> #include <stdio.h>
@ -30,28 +24,38 @@
const char *make_human_readable_str(unsigned long val, unsigned long hr) const char *make_human_readable_str(unsigned long size,
unsigned long block_size, unsigned long display_unit)
{ {
int i=0;
static char str[10] = "\0"; static char str[10] = "\0";
static const char strings[] = { 'k', 'M', 'G', 'T', 0 }; static const char strings[] = { 0, 'k', 'M', 'G', 'T', 0 };
unsigned long divisor = 1;
if(val == 0) if(size == 0 || block_size == 0)
return("0"); return("0");
if(hr)
snprintf(str, 9, "%ld", val/hr); if(display_unit) {
else { snprintf(str, 9, "%ld", (size/display_unit)*block_size);
while(val >= divisor && i <= 4) { } else {
divisor=divisor<<10, i++; /* Ok, looks like they want us to autoscale */
} int i=0;
divisor=divisor>>10, i--; unsigned long divisor = 1;
snprintf(str, 9, "%.1Lf%c", (long double)(val)/divisor, strings[i]); long double result = size*block_size;
for(i=0; i <= 4; i++) {
divisor<<=10;
if (result <= divisor) {
divisor>>=10;
break;
}
}
result/=divisor;
if (result > 10)
snprintf(str, 9, "%.0Lf%c", result, strings[i]);
else
snprintf(str, 9, "%.1Lf%c", result, strings[i]);
} }
return(str); return(str);
} }
/* END CODE */ /* END CODE */
/* /*
Local Variables: Local Variables:

View File

@ -198,12 +198,12 @@ struct sysinfo {
}; };
extern int sysinfo (struct sysinfo* info); extern int sysinfo (struct sysinfo* info);
const char *make_human_readable_str(unsigned long val, unsigned long not_hr);
enum { enum {
KILOBYTE = 1024, KILOBYTE = 1024,
MEGABYTE = (KILOBYTE*1024), MEGABYTE = (KILOBYTE*1024),
GIGABYTE = (MEGABYTE*1024) GIGABYTE = (MEGABYTE*1024)
}; };
const char *make_human_readable_str(unsigned long size, unsigned long block_size, unsigned long display_unit);
int ask_confirmation(void); int ask_confirmation(void);
int klogctl(int type, char * b, int len); int klogctl(int type, char * b, int len);

11
ls.c
View File

@ -610,8 +610,8 @@ static int list_single(struct dnode *dn)
break; break;
case LIST_BLOCKS: case LIST_BLOCKS:
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
fprintf(stdout, "%5s ", make_human_readable_str(dn->dstat.st_blocks>>1, fprintf(stdout, "%4s ", make_human_readable_str(dn->dstat.st_blocks>>1,
(ls_disp_hr==TRUE)? 0: 1)); KILOBYTE, (ls_disp_hr==TRUE)? 0: 1));
#else #else
#if _FILE_OFFSET_BITS == 64 #if _FILE_OFFSET_BITS == 64
printf("%4lld ", dn->dstat.st_blocks>>1); printf("%4lld ", dn->dstat.st_blocks>>1);
@ -622,7 +622,7 @@ static int list_single(struct dnode *dn)
column += 5; column += 5;
break; break;
case LIST_MODEBITS: case LIST_MODEBITS:
printf("%10s", (char *)mode_string(dn->dstat.st_mode)); printf("%-10s ", (char *)mode_string(dn->dstat.st_mode));
column += 10; column += 10;
break; break;
case LIST_NLINKS: case LIST_NLINKS:
@ -634,7 +634,7 @@ static int list_single(struct dnode *dn)
my_getpwuid(scratch, dn->dstat.st_uid); my_getpwuid(scratch, dn->dstat.st_uid);
printf("%-8.8s ", scratch); printf("%-8.8s ", scratch);
my_getgrgid(scratch, dn->dstat.st_gid); my_getgrgid(scratch, dn->dstat.st_gid);
printf("%-8.8s", scratch); printf("%-8.8s ", scratch);
column += 17; column += 17;
break; break;
#endif #endif
@ -649,8 +649,7 @@ static int list_single(struct dnode *dn)
} else { } else {
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
if (ls_disp_hr==TRUE) { if (ls_disp_hr==TRUE) {
fprintf(stdout, "%9s ", make_human_readable_str( fprintf(stdout, "%8s ", make_human_readable_str(dn->dstat.st_size, 1, 0));
dn->dstat.st_size>>10, 0));
} else } else
#endif #endif
{ {