Some minor cleanups to df.c. Also, with Erik's blessing, changed name of

'format' function to 'make_human_readable_str'.
This commit is contained in:
Mark Whitley 2001-03-07 17:42:07 +00:00
parent ae20128e9f
commit ae5612ca6e
9 changed files with 87 additions and 81 deletions

View File

@ -233,7 +233,7 @@ extern int sysinfo (struct sysinfo* info);
#endif #endif
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
const char *format(unsigned long val, unsigned long hr); const char *make_human_readable_str(unsigned long val, unsigned long hr);
#endif #endif
#define KILOBYTE 1024 #define KILOBYTE 1024
#define MEGABYTE (KILOBYTE*1024) #define MEGABYTE (KILOBYTE*1024)

View File

@ -31,10 +31,10 @@
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 disp_hr = KILOBYTE; static unsigned long df_disp_hr = KILOBYTE;
#endif #endif
static int df(char *device, const char *mountPoint) static int do_df(char *device, const char *mount_point)
{ {
struct statfs s; struct statfs s;
long blocks_used; long blocks_used;
@ -43,8 +43,8 @@ static int df(char *device, const char *mountPoint)
long base; long base;
#endif #endif
if (statfs(mountPoint, &s) != 0) { if (statfs(mount_point, &s) != 0) {
perror_msg("%s", mountPoint); perror_msg("%s", mount_point);
return FALSE; return FALSE;
} }
@ -61,8 +61,8 @@ static int df(char *device, const char *mountPoint)
* or leaves device alone if it can't find it */ * or leaves device alone if it can't find it */
find_real_root_device_name( device); find_real_root_device_name( device);
} }
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
switch (disp_hr) { switch (df_disp_hr) {
case MEGABYTE: case MEGABYTE:
base = KILOBYTE; base = KILOBYTE;
break; break;
@ -73,19 +73,19 @@ static int df(char *device, const char *mountPoint)
base = 0; base = 0;
} }
printf("%-20s %9s ", device, printf("%-20s %9s ", device,
format(s.f_blocks * (s.f_bsize/KILOBYTE), base)); make_human_readable_str(s.f_blocks * (s.f_bsize/KILOBYTE), base));
printf("%9s ", printf("%9s ",
format((s.f_blocks - s.f_bfree) * (s.f_bsize/KILOBYTE), base)); make_human_readable_str((s.f_blocks - s.f_bfree) * (s.f_bsize/KILOBYTE), base));
printf("%9s %3ld%% %s\n", printf("%9s %3ld%% %s\n",
format(s.f_bavail * (s.f_bsize/KILOBYTE), base), make_human_readable_str(s.f_bavail * (s.f_bsize/KILOBYTE), base),
blocks_percent_used, mountPoint); 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,
(long) (s.f_blocks * (s.f_bsize / KILOBYTE)), (long) (s.f_blocks * (s.f_bsize / KILOBYTE)),
(long) ((s.f_blocks - s.f_bfree) * (s.f_bsize / KILOBYTE)), (long) ((s.f_blocks - s.f_bfree) * (s.f_bsize / KILOBYTE)),
(long) (s.f_bavail * (s.f_bsize / KILOBYTE)), (long) (s.f_bavail * (s.f_bsize / KILOBYTE)),
blocks_percent_used, mountPoint); blocks_percent_used, mount_point);
#endif #endif
} }
@ -97,60 +97,61 @@ extern int df_main(int argc, char **argv)
int status = EXIT_SUCCESS; int status = EXIT_SUCCESS;
int opt = 0; int opt = 0;
int i = 0; int i = 0;
char disp_units_hdr[80] = "1k-blocks"; /* default display is kilobytes */
while ((opt = getopt(argc, argv, while ((opt = getopt(argc, argv, "k"
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
"hm" "hm"
#endif #endif
"k"
)) > 0) )) > 0)
{ {
switch (opt) { switch (opt) {
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
case 'h': disp_hr = 0; break; case 'h':
case 'm': disp_hr = MEGABYTE; break; df_disp_hr = 0;
strcpy(disp_units_hdr, " Size");
break;
case 'm':
df_disp_hr = MEGABYTE;
strcpy(disp_units_hdr, "1M-blocks");
break;
#endif #endif
case 'k': break; case 'k':
/* default display is kilobytes */
break;
default: default:
show_usage(); show_usage();
} }
} }
printf("%-20s %-14s %s %s %s %s\n", "Filesystem", printf("%-20s %-14s %s %s %s %s\n", "Filesystem", disp_units_hdr,
#ifdef BB_FEATURE_HUMAN_READABLE
(KILOBYTE == disp_hr) ? "1k-blocks" :
(MEGABYTE == disp_hr) ? "1M-blocks" : " Size",
#else
"1k-blocks",
#endif
"Used", "Available", "Use%", "Mounted on"); "Used", "Available", "Use%", "Mounted on");
if(optind < argc) { if(optind < argc) {
struct mntent *mountEntry; struct mntent *mount_entry;
for(i = optind; i < argc; i++) for(i = optind; i < argc; i++)
{ {
if ((mountEntry = find_mount_point(argv[i], mtab_file)) == 0) { if ((mount_entry = find_mount_point(argv[i], mtab_file)) == 0) {
error_msg("%s: can't find mount point.", argv[i]); error_msg("%s: can't find mount point.", argv[i]);
status = EXIT_FAILURE; status = EXIT_FAILURE;
} else if (!df(mountEntry->mnt_fsname, mountEntry->mnt_dir)) } else if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir))
status = EXIT_FAILURE; status = EXIT_FAILURE;
} }
} else { } else {
FILE *mountTable; FILE *mount_table;
struct mntent *mountEntry; struct mntent *mount_entry;
mountTable = setmntent(mtab_file, "r"); mount_table = setmntent(mtab_file, "r");
if (mountTable == 0) { if (mount_table == 0) {
perror_msg("%s", mtab_file); perror_msg("%s", mtab_file);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
while ((mountEntry = getmntent(mountTable))) { while ((mount_entry = getmntent(mount_table))) {
if (!df(mountEntry->mnt_fsname, mountEntry->mnt_dir)) if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir))
status = EXIT_FAILURE; status = EXIT_FAILURE;
} }
endmntent(mountTable); endmntent(mount_table);
} }
return status; return status;

View File

@ -60,7 +60,7 @@ static void print_normal(long size, char *filename)
default: default:
base = 0; base = 0;
} }
printf("%s\t%s\n", format(size, base), filename); 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
@ -69,7 +69,7 @@ printf("%s\t%s\n", format(size, base), filename);
static void print_summary(long size, char *filename) static void print_summary(long size, char *filename)
{ {
if (du_depth == 1) { if (du_depth == 1) {
printf("summary\n"); printf("summary\n");
print_normal(size, filename); print_normal(size, filename);
} }
} }
@ -196,7 +196,7 @@ int du_main(int argc, char **argv)
return status; return status;
} }
/* $Id: du.c,v 1.41 2001/03/07 06:04:08 andersen Exp $ */ /* $Id: du.c,v 1.42 2001/03/07 17:42:07 markw Exp $ */
/* /*
Local Variables: Local Variables:
c-file-style: "linux" c-file-style: "linux"

View File

@ -610,7 +610,8 @@ 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 ", format(dn->dstat.st_blocks>>1, (ls_disp_hr==TRUE)? 0: 1)); fprintf(stdout, "%5s ", make_human_readable_str(dn->dstat.st_blocks>>1,
(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);
@ -647,7 +648,8 @@ int list_single(struct dnode *dn)
printf("%4d, %3d ", (int)MAJOR(dn->dstat.st_rdev), (int)MINOR(dn->dstat.st_rdev)); printf("%4d, %3d ", (int)MAJOR(dn->dstat.st_rdev), (int)MINOR(dn->dstat.st_rdev));
} else { } else {
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
fprintf(stdout, "%9s ", format(dn->dstat.st_size, (ls_disp_hr==TRUE)? 0: 1)); fprintf(stdout, "%9s ", make_human_readable_str(dn->dstat.st_size,
(ls_disp_hr==TRUE)? 0: 1));
#else #else
#if _FILE_OFFSET_BITS == 64 #if _FILE_OFFSET_BITS == 64
printf("%9lld ", dn->dstat.st_size>>1); printf("%9lld ", dn->dstat.st_size>>1);

69
df.c
View File

@ -31,10 +31,10 @@
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 disp_hr = KILOBYTE; static unsigned long df_disp_hr = KILOBYTE;
#endif #endif
static int df(char *device, const char *mountPoint) static int do_df(char *device, const char *mount_point)
{ {
struct statfs s; struct statfs s;
long blocks_used; long blocks_used;
@ -43,8 +43,8 @@ static int df(char *device, const char *mountPoint)
long base; long base;
#endif #endif
if (statfs(mountPoint, &s) != 0) { if (statfs(mount_point, &s) != 0) {
perror_msg("%s", mountPoint); perror_msg("%s", mount_point);
return FALSE; return FALSE;
} }
@ -61,8 +61,8 @@ static int df(char *device, const char *mountPoint)
* or leaves device alone if it can't find it */ * or leaves device alone if it can't find it */
find_real_root_device_name( device); find_real_root_device_name( device);
} }
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
switch (disp_hr) { switch (df_disp_hr) {
case MEGABYTE: case MEGABYTE:
base = KILOBYTE; base = KILOBYTE;
break; break;
@ -73,19 +73,19 @@ static int df(char *device, const char *mountPoint)
base = 0; base = 0;
} }
printf("%-20s %9s ", device, printf("%-20s %9s ", device,
format(s.f_blocks * (s.f_bsize/KILOBYTE), base)); make_human_readable_str(s.f_blocks * (s.f_bsize/KILOBYTE), base));
printf("%9s ", printf("%9s ",
format((s.f_blocks - s.f_bfree) * (s.f_bsize/KILOBYTE), base)); make_human_readable_str((s.f_blocks - s.f_bfree) * (s.f_bsize/KILOBYTE), base));
printf("%9s %3ld%% %s\n", printf("%9s %3ld%% %s\n",
format(s.f_bavail * (s.f_bsize/KILOBYTE), base), make_human_readable_str(s.f_bavail * (s.f_bsize/KILOBYTE), base),
blocks_percent_used, mountPoint); 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,
(long) (s.f_blocks * (s.f_bsize / KILOBYTE)), (long) (s.f_blocks * (s.f_bsize / KILOBYTE)),
(long) ((s.f_blocks - s.f_bfree) * (s.f_bsize / KILOBYTE)), (long) ((s.f_blocks - s.f_bfree) * (s.f_bsize / KILOBYTE)),
(long) (s.f_bavail * (s.f_bsize / KILOBYTE)), (long) (s.f_bavail * (s.f_bsize / KILOBYTE)),
blocks_percent_used, mountPoint); blocks_percent_used, mount_point);
#endif #endif
} }
@ -97,60 +97,61 @@ extern int df_main(int argc, char **argv)
int status = EXIT_SUCCESS; int status = EXIT_SUCCESS;
int opt = 0; int opt = 0;
int i = 0; int i = 0;
char disp_units_hdr[80] = "1k-blocks"; /* default display is kilobytes */
while ((opt = getopt(argc, argv, while ((opt = getopt(argc, argv, "k"
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
"hm" "hm"
#endif #endif
"k"
)) > 0) )) > 0)
{ {
switch (opt) { switch (opt) {
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
case 'h': disp_hr = 0; break; case 'h':
case 'm': disp_hr = MEGABYTE; break; df_disp_hr = 0;
strcpy(disp_units_hdr, " Size");
break;
case 'm':
df_disp_hr = MEGABYTE;
strcpy(disp_units_hdr, "1M-blocks");
break;
#endif #endif
case 'k': break; case 'k':
/* default display is kilobytes */
break;
default: default:
show_usage(); show_usage();
} }
} }
printf("%-20s %-14s %s %s %s %s\n", "Filesystem", printf("%-20s %-14s %s %s %s %s\n", "Filesystem", disp_units_hdr,
#ifdef BB_FEATURE_HUMAN_READABLE
(KILOBYTE == disp_hr) ? "1k-blocks" :
(MEGABYTE == disp_hr) ? "1M-blocks" : " Size",
#else
"1k-blocks",
#endif
"Used", "Available", "Use%", "Mounted on"); "Used", "Available", "Use%", "Mounted on");
if(optind < argc) { if(optind < argc) {
struct mntent *mountEntry; struct mntent *mount_entry;
for(i = optind; i < argc; i++) for(i = optind; i < argc; i++)
{ {
if ((mountEntry = find_mount_point(argv[i], mtab_file)) == 0) { if ((mount_entry = find_mount_point(argv[i], mtab_file)) == 0) {
error_msg("%s: can't find mount point.", argv[i]); error_msg("%s: can't find mount point.", argv[i]);
status = EXIT_FAILURE; status = EXIT_FAILURE;
} else if (!df(mountEntry->mnt_fsname, mountEntry->mnt_dir)) } else if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir))
status = EXIT_FAILURE; status = EXIT_FAILURE;
} }
} else { } else {
FILE *mountTable; FILE *mount_table;
struct mntent *mountEntry; struct mntent *mount_entry;
mountTable = setmntent(mtab_file, "r"); mount_table = setmntent(mtab_file, "r");
if (mountTable == 0) { if (mount_table == 0) {
perror_msg("%s", mtab_file); perror_msg("%s", mtab_file);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
while ((mountEntry = getmntent(mountTable))) { while ((mount_entry = getmntent(mount_table))) {
if (!df(mountEntry->mnt_fsname, mountEntry->mnt_dir)) if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir))
status = EXIT_FAILURE; status = EXIT_FAILURE;
} }
endmntent(mountTable); endmntent(mount_table);
} }
return status; return status;

6
du.c
View File

@ -60,7 +60,7 @@ static void print_normal(long size, char *filename)
default: default:
base = 0; base = 0;
} }
printf("%s\t%s\n", format(size, base), filename); 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
@ -69,7 +69,7 @@ printf("%s\t%s\n", format(size, base), filename);
static void print_summary(long size, char *filename) static void print_summary(long size, char *filename)
{ {
if (du_depth == 1) { if (du_depth == 1) {
printf("summary\n"); printf("summary\n");
print_normal(size, filename); print_normal(size, filename);
} }
} }
@ -196,7 +196,7 @@ int du_main(int argc, char **argv)
return status; return status;
} }
/* $Id: du.c,v 1.41 2001/03/07 06:04:08 andersen Exp $ */ /* $Id: du.c,v 1.42 2001/03/07 17:42:07 markw Exp $ */
/* /*
Local Variables: Local Variables:
c-file-style: "linux" c-file-style: "linux"

View File

@ -233,7 +233,7 @@ extern int sysinfo (struct sysinfo* info);
#endif #endif
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
const char *format(unsigned long val, unsigned long hr); const char *make_human_readable_str(unsigned long val, unsigned long hr);
#endif #endif
#define KILOBYTE 1024 #define KILOBYTE 1024
#define MEGABYTE (KILOBYTE*1024) #define MEGABYTE (KILOBYTE*1024)

6
ls.c
View File

@ -610,7 +610,8 @@ 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 ", format(dn->dstat.st_blocks>>1, (ls_disp_hr==TRUE)? 0: 1)); fprintf(stdout, "%5s ", make_human_readable_str(dn->dstat.st_blocks>>1,
(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);
@ -647,7 +648,8 @@ int list_single(struct dnode *dn)
printf("%4d, %3d ", (int)MAJOR(dn->dstat.st_rdev), (int)MINOR(dn->dstat.st_rdev)); printf("%4d, %3d ", (int)MAJOR(dn->dstat.st_rdev), (int)MINOR(dn->dstat.st_rdev));
} else { } else {
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
fprintf(stdout, "%9s ", format(dn->dstat.st_size, (ls_disp_hr==TRUE)? 0: 1)); fprintf(stdout, "%9s ", make_human_readable_str(dn->dstat.st_size,
(ls_disp_hr==TRUE)? 0: 1));
#else #else
#if _FILE_OFFSET_BITS == 64 #if _FILE_OFFSET_BITS == 64
printf("%9lld ", dn->dstat.st_size>>1); printf("%9lld ", dn->dstat.st_size>>1);

View File

@ -1798,7 +1798,7 @@ ssize_t safe_read(int fd, void *buf, size_t count)
#endif #endif
#ifdef BB_FEATURE_HUMAN_READABLE #ifdef BB_FEATURE_HUMAN_READABLE
const char *format(unsigned long val, unsigned long hr) const char *make_human_readable_str(unsigned long val, unsigned long hr)
{ {
int i=0; int i=0;
static char str[10] = "\0"; static char str[10] = "\0";