aboutsummaryrefslogtreecommitdiff
path: root/df.c
diff options
context:
space:
mode:
authorrjune <rjune@69ca8d6d-28ef-0310-b511-8ec308f3f277>2001-01-22 22:35:38 +0000
committerrjune <rjune@69ca8d6d-28ef-0310-b511-8ec308f3f277>2001-01-22 22:35:38 +0000
commit6be1fd967c69696684f75eeab758b56bd5c2eaf9 (patch)
tree97ed53aec9cbe3a3021804e9758abb3e04f05879 /df.c
parent6ddaebeb3829d0865b3b29be747c894d12843763 (diff)
downloadbusybox-w32-6be1fd967c69696684f75eeab758b56bd5c2eaf9.tar.gz
busybox-w32-6be1fd967c69696684f75eeab758b56bd5c2eaf9.tar.bz2
busybox-w32-6be1fd967c69696684f75eeab758b56bd5c2eaf9.zip
Add HUMAN_READABLE define for -m and -h support in du, df, and ls
Add support for -k in du, df, and ls(no define, it's for compatibliity with the GNU utils as bb does -k by default) Fix bug #1084 git-svn-id: svn://busybox.net/trunk/busybox@1635 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'df.c')
-rw-r--r--df.c68
1 files changed, 55 insertions, 13 deletions
diff --git a/df.c b/df.c
index dc4849049..aa04682a7 100644
--- a/df.c
+++ b/df.c
@@ -28,6 +28,9 @@
28#include <sys/vfs.h> 28#include <sys/vfs.h>
29 29
30extern const char mtab_file[]; /* Defined in utility.c */ 30extern const char mtab_file[]; /* Defined in utility.c */
31#ifdef BB_FEATURE_HUMAN_READABLE
32unsigned long disp_hr = KILOBYTE;
33#endif
31 34
32static int df(char *device, const char *mountPoint) 35static int df(char *device, const char *mountPoint)
33{ 36{
@@ -42,19 +45,32 @@ static int df(char *device, const char *mountPoint)
42 45
43 if (s.f_blocks > 0) { 46 if (s.f_blocks > 0) {
44 blocks_used = s.f_blocks - s.f_bfree; 47 blocks_used = s.f_blocks - s.f_bfree;
45 blocks_percent_used = (long) 48 if(0 == blocks_used)
46 (blocks_used * 100.0 / (blocks_used + s.f_bavail) + 0.5); 49 blocs_percent_used = 0;
50 else
51 blocks_percent_used = (long)
52 (blocks_used * 100.0 / (blocks_used + s.f_bavail) + 0.5);
47 if (strcmp(device, "/dev/root") == 0) { 53 if (strcmp(device, "/dev/root") == 0) {
48 /* Adjusts device to be the real root device, 54 /* Adjusts device to be the real root device,
49 * or leaves device alone if it can't find it */ 55 * or leaves device alone if it can't find it */
50 find_real_root_device_name( device); 56 find_real_root_device_name( device);
51 } 57 }
58#ifdef BB_FEATURE_HUMAN_READABLE
59 printf("%-20s %9s",
60 device,
61 format((s.f_blocks * s.f_bsize), disp_hr));
62 printf(" %9s", format((s.f_blocks - s.f_bfree) * s.f_bsize, disp_hr));
63 printf(" %9s %3ld%% %s\n",
64 format(s.f_bavail * s.f_bsize, disp_hr),
65 blocks_percent_used, mountPoint);
66#else
52 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n", 67 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
53 device, 68 device,
54 (long) (s.f_blocks * (s.f_bsize / 1024.0)), 69 (long) (s.f_blocks * (s.f_bsize / 1024.0)),
55 (long) ((s.f_blocks - s.f_bfree) * (s.f_bsize / 1024.0)), 70 (long) ((s.f_blocks - s.f_bfree) * (s.f_bsize / 1024.0)),
56 (long) (s.f_bavail * (s.f_bsize / 1024.0)), 71 (long) (s.f_bavail * (s.f_bsize / 1024.0)),
57 blocks_percent_used, mountPoint); 72 blocks_percent_used, mountPoint);
73#endif
58 74
59 } 75 }
60 76
@@ -64,24 +80,46 @@ static int df(char *device, const char *mountPoint)
64extern int df_main(int argc, char **argv) 80extern int df_main(int argc, char **argv)
65{ 81{
66 int status = EXIT_SUCCESS; 82 int status = EXIT_SUCCESS;
83 int opt = 0;
84 int i = 0;
85
86 while ((opt = getopt(argc, argv, "?"
87#ifdef BB_FEATURE_HUMAN_READABLE
88 "hm"
89#endif
90 "k"
91)) > 0)
92 {
93 switch (opt) {
94#ifdef BB_FEATURE_HUMAN_READABLE
95 case 'h': disp_hr = 0; break;
96 case 'm': disp_hr = MEGABYTE; break;
97 case 'k': disp_hr = KILOBYTE; break;
98#else
99 case 'k': break;
100#endif
101 case '?': goto print_df_usage; break;
102 }
103 }
67 104
68 printf("%-20s %-14s %s %s %s %s\n", "Filesystem", 105 printf("%-20s %-14s %s %s %s %s\n", "Filesystem",
69 "1k-blocks", "Used", "Available", "Use%", "Mounted on"); 106#ifdef BB_FEATURE_HUMAN_READABLE
107 (KILOBYTE == disp_hr) ? "1k-blocks" : " Size",
108#else
109 "1k-blocks",
110#endif
111 "Used", "Available", "Use%", "Mounted on");
70 112
71 if (argc > 1) {
72 struct mntent *mountEntry;
73 113
74 if (**(argv + 1) == '-') { 114 if(optind < argc) {
75 usage(df_usage); 115 struct mntent *mountEntry;
76 } 116 for(i = optind; i < argc; i++)
77 while (argc > 1) { 117 {
78 if ((mountEntry = find_mount_point(argv[1], mtab_file)) == 0) { 118 if ((mountEntry = find_mount_point(argv[i], mtab_file)) == 0) {
79 error_msg("%s: can't find mount point.\n", argv[1]); 119 error_msg("%s: can't find mount point.\n", argv[i]);
80 status = EXIT_FAILURE; 120 status = EXIT_FAILURE;
81 } else if (!df(mountEntry->mnt_fsname, mountEntry->mnt_dir)) 121 } else if (!df(mountEntry->mnt_fsname, mountEntry->mnt_dir))
82 status = EXIT_FAILURE; 122 status = EXIT_FAILURE;
83 argc--;
84 argv++;
85 } 123 }
86 } else { 124 } else {
87 FILE *mountTable; 125 FILE *mountTable;
@@ -101,6 +139,10 @@ extern int df_main(int argc, char **argv)
101 } 139 }
102 140
103 return status; 141 return status;
142
143print_df_usage:
144 usage(df_usage);
145 return(FALSE);
104} 146}
105 147
106/* 148/*