aboutsummaryrefslogtreecommitdiff
path: root/coreutils/df.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-08-28 22:42:52 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-08-28 22:42:52 +0000
commitd66aa3c701ffb83343239e71a8b294407ff5df86 (patch)
treeeeebccfe994962aa8a33312c8726eac5adf63f5a /coreutils/df.c
parent3b80cac953b0627ba9b3337d3f9386678d436871 (diff)
downloadbusybox-w32-d66aa3c701ffb83343239e71a8b294407ff5df86.tar.gz
busybox-w32-d66aa3c701ffb83343239e71a8b294407ff5df86.tar.bz2
busybox-w32-d66aa3c701ffb83343239e71a8b294407ff5df86.zip
df: add support for more options, add some coreutils 6.10 compat.
by Bernhard Reutner-Fischer function old new delta df_main 664 795 +131 packed_usage 24812 24862 +50 make_human_readable_str 213 262 +49 static.ignored_mounts - 8 +8 static.unit_chars - 7 +7 static.zero_and_units 6 - -6 ------------------------------------------------------------------------------ (add/remove: 2/1 grow/shrink: 3/0 up/down: 245/-6) Total: 239 bytes
Diffstat (limited to 'coreutils/df.c')
-rw-r--r--coreutils/df.c93
1 files changed, 58 insertions, 35 deletions
diff --git a/coreutils/df.c b/coreutils/df.c
index 9cb328aa3..fd2502309 100644
--- a/coreutils/df.c
+++ b/coreutils/df.c
@@ -8,7 +8,7 @@
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 */ 9 */
10 10
11/* BB_AUDIT SUSv3 _NOT_ compliant -- options -P and -t missing. Also blocksize. */ 11/* BB_AUDIT SUSv3 _NOT_ compliant -- option -t missing. */
12/* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */ 12/* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */
13 13
14/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) 14/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
@@ -16,6 +16,10 @@
16 * Size reduction. Removed floating point dependency. Added error checking 16 * Size reduction. Removed floating point dependency. Added error checking
17 * on output. Output stats on 0-sized filesystems if specifically listed on 17 * on output. Output stats on 0-sized filesystems if specifically listed on
18 * the command line. Properly round *-blocks, Used, and Available quantities. 18 * the command line. Properly round *-blocks, Used, and Available quantities.
19 *
20 * Aug 28, 2008 Bernhard Reutner-Fischer
21 *
22 * Implement -P and -B; better coreutils compat; cleanup
19 */ 23 */
20 24
21#include <mntent.h> 25#include <mntent.h>
@@ -34,51 +38,73 @@ int df_main(int argc, char **argv)
34{ 38{
35 unsigned long blocks_used; 39 unsigned long blocks_used;
36 unsigned blocks_percent_used; 40 unsigned blocks_percent_used;
37#if ENABLE_FEATURE_HUMAN_READABLE 41 unsigned long df_disp_hr = 1024;
38 unsigned df_disp_hr = 1024;
39#endif
40 int status = EXIT_SUCCESS; 42 int status = EXIT_SUCCESS;
41 unsigned opt; 43 unsigned opt;
42 FILE *mount_table; 44 FILE *mount_table;
43 struct mntent *mount_entry; 45 struct mntent *mount_entry;
44 struct statfs s; 46 struct statfs s;
45 /* default display is kilobytes */ 47 static const char ignored_mounts[] ALIGN1 =
46 const char *disp_units_hdr = "1k-blocks"; 48 "rootfs\0";
47 49
48 enum { 50 enum {
49 OPT_ALL = (1 << 0), 51 OPT_KILO = (1 << 0),
50 OPT_INODE = (ENABLE_FEATURE_HUMAN_READABLE ? (1 << 4) : (1 << 2)) 52 OPT_POSIX = (1 << 1),
51 * ENABLE_FEATURE_DF_INODE 53 OPT_ALL = (1 << 2) * ENABLE_FEATURE_DF_FANCY,
54 OPT_INODE = (1 << 3) * ENABLE_FEATURE_DF_FANCY,
55 OPT_BSIZE = (1 << 4) * ENABLE_FEATURE_DF_FANCY,
56 OPT_HUMAN = (1 << 5) * ENABLE_FEATURE_HUMAN_READABLE,
57 OPT_MEGA = (1 << 6) * ENABLE_FEATURE_HUMAN_READABLE,
52 }; 58 };
59 const char *disp_units_hdr = NULL;
60 char *chp;
53 61
54#if ENABLE_FEATURE_HUMAN_READABLE 62#if ENABLE_FEATURE_HUMAN_READABLE && ENABLE_FEATURE_DF_FANCY
55 opt_complementary = "h-km:k-hm:m-hk"; 63 opt_complementary = "k-mB:m-Bk:B-km";
56 opt = getopt32(argv, "ahmk" USE_FEATURE_DF_INODE("i")); 64#elif ENABLE_FEATURE_HUMAN_READABLE
57 if (opt & (1 << 1)) { // -h 65 opt_complementary = "k-m:m-k";
66#endif
67 opt = getopt32(argv, "kP"
68 USE_FEATURE_DF_FANCY("aiB:")
69 USE_FEATURE_HUMAN_READABLE("hm")
70 USE_FEATURE_DF_FANCY(, &chp));
71 if (opt & OPT_MEGA)
72 df_disp_hr = 1024*1024;
73
74 if (opt & OPT_BSIZE)
75 df_disp_hr = xatoul_range(chp, 1, ULONG_MAX); /* disallow 0 */
76
77 /* From the manpage of df from coreutils-6.10:
78 Disk space is shown in 1K blocks by default, unless the environment
79 variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.
80 */
81 if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
82 df_disp_hr = 512;
83
84 if (opt & OPT_HUMAN) {
58 df_disp_hr = 0; 85 df_disp_hr = 0;
59 disp_units_hdr = " Size"; 86 disp_units_hdr = " Size";
60 } 87 }
61 if (opt & (1 << 2)) { // -m 88 if (opt & OPT_INODE)
62 df_disp_hr = 1024*1024;
63 disp_units_hdr = "1M-blocks";
64 }
65 if (opt & OPT_INODE) {
66 disp_units_hdr = " Inodes"; 89 disp_units_hdr = " Inodes";
67 } 90
91 if (disp_units_hdr == NULL) {
92#if ENABLE_FEATURE_HUMAN_READABLE
93 disp_units_hdr = xasprintf("%s-blocks",
94 make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX)));
68#else 95#else
69 opt = getopt32(argv, "ak" USE_FEATURE_DF_INODE("i")); 96 disp_units_hdr = xasprintf("%d-blocks", df_disp_hr);
70#endif 97#endif
71 98 }
72 printf("Filesystem %-15sUsed Available Use%% Mounted on\n", 99 printf("Filesystem %-15sUsed Available %s Mounted on\n",
73 disp_units_hdr); 100 disp_units_hdr, (opt & OPT_POSIX) ? "Capacity" : "Use%");
74 101
75 mount_table = NULL; 102 mount_table = NULL;
76 argv += optind; 103 argv += optind;
77 if (optind >= argc) { 104 if (optind >= argc) {
78 mount_table = setmntent(bb_path_mtab_file, "r"); 105 mount_table = setmntent(bb_path_mtab_file, "r");
79 if (!mount_table) { 106 if (!mount_table)
80 bb_perror_msg_and_die(bb_path_mtab_file); 107 bb_perror_msg_and_die(bb_path_mtab_file);
81 }
82 } 108 }
83 109
84 while (1) { 110 while (1) {
@@ -93,9 +119,8 @@ int df_main(int argc, char **argv)
93 } 119 }
94 } else { 120 } else {
95 mount_point = *argv++; 121 mount_point = *argv++;
96 if (!mount_point) { 122 if (!mount_point)
97 break; 123 break;
98 }
99 mount_entry = find_mount_point(mount_point, bb_path_mtab_file); 124 mount_entry = find_mount_point(mount_point, bb_path_mtab_file);
100 if (!mount_entry) { 125 if (!mount_entry) {
101 bb_error_msg("%s: can't find mount point", mount_point); 126 bb_error_msg("%s: can't find mount point", mount_point);
@@ -118,10 +143,9 @@ int df_main(int argc, char **argv)
118 s.f_blocks = s.f_files; 143 s.f_blocks = s.f_files;
119 s.f_bavail = s.f_bfree = s.f_ffree; 144 s.f_bavail = s.f_bfree = s.f_ffree;
120 s.f_bsize = 1; 145 s.f_bsize = 1;
121#if ENABLE_FEATURE_HUMAN_READABLE 146
122 if (df_disp_hr) 147 if (df_disp_hr)
123 df_disp_hr = 1; 148 df_disp_hr = 1;
124#endif
125 } 149 }
126 blocks_used = s.f_blocks - s.f_bfree; 150 blocks_used = s.f_blocks - s.f_bfree;
127 blocks_percent_used = 0; 151 blocks_percent_used = 0;
@@ -131,11 +155,10 @@ int df_main(int argc, char **argv)
131 ) / (blocks_used + s.f_bavail); 155 ) / (blocks_used + s.f_bavail);
132 } 156 }
133 157
134#ifdef WHY_IT_SHOULD_BE_HIDDEN 158 /* GNU coreutils 6.10 skip certain mounts, try to be compatible. */
135 if (strcmp(device, "rootfs") == 0) { 159 if (index_in_strings(device, ignored_mounts) != -1)
136 continue; 160 continue;
137 } 161
138#endif
139#ifdef WHY_WE_DO_IT_FOR_DEV_ROOT_ONLY 162#ifdef WHY_WE_DO_IT_FOR_DEV_ROOT_ONLY
140/* ... and also this is the only user of find_block_device */ 163/* ... and also this is the only user of find_block_device */
141 if (strcmp(device, "/dev/root") == 0) { 164 if (strcmp(device, "/dev/root") == 0) {
@@ -164,12 +187,12 @@ int df_main(int argc, char **argv)
164#else 187#else
165 printf(" %9lu %9lu %9lu %3u%% %s\n", 188 printf(" %9lu %9lu %9lu %3u%% %s\n",
166 kscale(s.f_blocks, s.f_bsize), 189 kscale(s.f_blocks, s.f_bsize),
167 kscale(s.f_blocks-s.f_bfree, s.f_bsize), 190 kscale(s.f_blocks - s.f_bfree, s.f_bsize),
168 kscale(s.f_bavail, s.f_bsize), 191 kscale(s.f_bavail, s.f_bsize),
169 blocks_percent_used, mount_point); 192 blocks_percent_used, mount_point);
170#endif 193#endif
171 } 194 }
172 } 195 }
173 196
174 fflush_stdout_and_exit(status); 197 return status;
175} 198}