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
#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
#define KILOBYTE 1024
#define MEGABYTE (KILOBYTE*1024)

View File

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

View File

@ -60,7 +60,7 @@ static void print_normal(long size, char *filename)
default:
base = 0;
}
printf("%s\t%s\n", format(size, base), filename);
printf("%s\t%s\n", make_human_readable_str(size, base), filename);
#else
printf("%ld\t%s\n", size, filename);
#endif
@ -69,7 +69,7 @@ printf("%s\t%s\n", format(size, base), filename);
static void print_summary(long size, char *filename)
{
if (du_depth == 1) {
printf("summary\n");
printf("summary\n");
print_normal(size, filename);
}
}
@ -196,7 +196,7 @@ int du_main(int argc, char **argv)
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:
c-file-style: "linux"

View File

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

6
du.c
View File

@ -60,7 +60,7 @@ static void print_normal(long size, char *filename)
default:
base = 0;
}
printf("%s\t%s\n", format(size, base), filename);
printf("%s\t%s\n", make_human_readable_str(size, base), filename);
#else
printf("%ld\t%s\n", size, filename);
#endif
@ -69,7 +69,7 @@ printf("%s\t%s\n", format(size, base), filename);
static void print_summary(long size, char *filename)
{
if (du_depth == 1) {
printf("summary\n");
printf("summary\n");
print_normal(size, filename);
}
}
@ -196,7 +196,7 @@ int du_main(int argc, char **argv)
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:
c-file-style: "linux"

View File

@ -233,7 +233,7 @@ extern int sysinfo (struct sysinfo* info);
#endif
#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
#define KILOBYTE 1024
#define MEGABYTE (KILOBYTE*1024)

6
ls.c
View File

@ -610,7 +610,8 @@ int list_single(struct dnode *dn)
break;
case LIST_BLOCKS:
#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
#if _FILE_OFFSET_BITS == 64
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));
} else {
#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
#if _FILE_OFFSET_BITS == 64
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
#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;
static char str[10] = "\0";