libbb: better unicode width support. Hopefully fixes bug 839.
[platform/upstream/busybox.git] / coreutils / df.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini df implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  * based on original code by (I think) Bruce Perens <bruce@pixar.com>.
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 /* BB_AUDIT SUSv3 _NOT_ compliant -- option -t missing. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */
13
14 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
15  *
16  * Size reduction.  Removed floating point dependency.  Added error checking
17  * on output.  Output stats on 0-sized filesystems if specifically listed on
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
23  */
24
25 #include <mntent.h>
26 #include <sys/vfs.h>
27 #include "libbb.h"
28 #include "unicode.h"
29
30 #if !ENABLE_FEATURE_HUMAN_READABLE
31 static unsigned long kscale(unsigned long b, unsigned long bs)
32 {
33         return (b * (unsigned long long) bs + 1024/2) / 1024;
34 }
35 #endif
36
37 int df_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
38 int df_main(int argc UNUSED_PARAM, char **argv)
39 {
40         unsigned long blocks_used;
41         unsigned blocks_percent_used;
42         unsigned long df_disp_hr = 1024;
43         int status = EXIT_SUCCESS;
44         unsigned opt;
45         FILE *mount_table;
46         struct mntent *mount_entry;
47         struct statfs s;
48
49         enum {
50                 OPT_KILO  = (1 << 0),
51                 OPT_POSIX = (1 << 1),
52                 OPT_ALL   = (1 << 2) * ENABLE_FEATURE_DF_FANCY,
53                 OPT_INODE = (1 << 3) * ENABLE_FEATURE_DF_FANCY,
54                 OPT_BSIZE = (1 << 4) * ENABLE_FEATURE_DF_FANCY,
55                 OPT_HUMAN = (1 << (2 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
56                 OPT_MEGA  = (1 << (3 + 3*ENABLE_FEATURE_DF_FANCY)) * ENABLE_FEATURE_HUMAN_READABLE,
57         };
58         const char *disp_units_hdr = NULL;
59         char *chp;
60
61         init_unicode();
62
63 #if ENABLE_FEATURE_HUMAN_READABLE && ENABLE_FEATURE_DF_FANCY
64         opt_complementary = "k-mB:m-Bk:B-km";
65 #elif ENABLE_FEATURE_HUMAN_READABLE
66         opt_complementary = "k-m:m-k";
67 #endif
68         opt = getopt32(argv, "kP"
69                         IF_FEATURE_DF_FANCY("aiB:")
70                         IF_FEATURE_HUMAN_READABLE("hm")
71                         IF_FEATURE_DF_FANCY(, &chp));
72         if (opt & OPT_MEGA)
73                 df_disp_hr = 1024*1024;
74
75         if (opt & OPT_BSIZE)
76                 df_disp_hr = xatoul_range(chp, 1, ULONG_MAX); /* disallow 0 */
77
78         /* From the manpage of df from coreutils-6.10:
79            Disk space is shown in 1K blocks by default, unless the environment
80            variable POSIXLY_CORRECT is set, in which case 512-byte blocks are used.
81         */
82         if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
83                 df_disp_hr = 512;
84
85         if (opt & OPT_HUMAN) {
86                 df_disp_hr = 0;
87                 disp_units_hdr = "     Size";
88         }
89         if (opt & OPT_INODE)
90                 disp_units_hdr = "   Inodes";
91
92         if (disp_units_hdr == NULL) {
93 #if ENABLE_FEATURE_HUMAN_READABLE
94                 disp_units_hdr = xasprintf("%s-blocks",
95                         /* print df_disp_hr, show no fractionals,
96                          * use suffixes if OPT_POSIX is set in opt */
97                         make_human_readable_str(df_disp_hr, 0, !!(opt & OPT_POSIX))
98                 );
99 #else
100                 disp_units_hdr = xasprintf("%lu-blocks", df_disp_hr);
101 #endif
102         }
103         printf("Filesystem           %-15sUsed Available %s Mounted on\n",
104                         disp_units_hdr, (opt & OPT_POSIX) ? "Capacity" : "Use%");
105
106         mount_table = NULL;
107         argv += optind;
108         if (!argv[0]) {
109                 mount_table = setmntent(bb_path_mtab_file, "r");
110                 if (!mount_table)
111                         bb_perror_msg_and_die(bb_path_mtab_file);
112         }
113
114         while (1) {
115                 const char *device;
116                 const char *mount_point;
117 #if ENABLE_FEATURE_ASSUME_UNICODE
118                 size_t dev_len;
119 #endif
120
121                 if (mount_table) {
122                         mount_entry = getmntent(mount_table);
123                         if (!mount_entry) {
124                                 endmntent(mount_table);
125                                 break;
126                         }
127                 } else {
128                         mount_point = *argv++;
129                         if (!mount_point)
130                                 break;
131                         mount_entry = find_mount_point(mount_point, 1);
132                         if (!mount_entry) {
133                                 bb_error_msg("%s: can't find mount point", mount_point);
134  set_error:
135                                 status = EXIT_FAILURE;
136                                 continue;
137                         }
138                 }
139
140                 device = mount_entry->mnt_fsname;
141                 mount_point = mount_entry->mnt_dir;
142
143                 if (statfs(mount_point, &s) != 0) {
144                         bb_simple_perror_msg(mount_point);
145                         goto set_error;
146                 }
147
148                 if ((s.f_blocks > 0) || !mount_table || (opt & OPT_ALL)) {
149                         if (opt & OPT_INODE) {
150                                 s.f_blocks = s.f_files;
151                                 s.f_bavail = s.f_bfree = s.f_ffree;
152                                 s.f_bsize = 1;
153
154                                 if (df_disp_hr)
155                                         df_disp_hr = 1;
156                         }
157                         blocks_used = s.f_blocks - s.f_bfree;
158                         blocks_percent_used = 0;
159                         if (blocks_used + s.f_bavail) {
160                                 blocks_percent_used = (blocks_used * 100ULL
161                                                 + (blocks_used + s.f_bavail)/2
162                                                 ) / (blocks_used + s.f_bavail);
163                         }
164
165                         /* GNU coreutils 6.10 skips certain mounts, try to be compatible.  */
166                         if (strcmp(device, "rootfs") == 0)
167                                 continue;
168
169 #ifdef WHY_WE_DO_IT_FOR_DEV_ROOT_ONLY
170                         if (strcmp(device, "/dev/root") == 0) {
171                                 /* Adjusts device to be the real root device,
172                                  * or leaves device alone if it can't find it */
173                                 device = find_block_device("/");
174                                 if (!device) {
175                                         goto set_error;
176                                 }
177                         }
178 #endif
179
180 #if ENABLE_FEATURE_ASSUME_UNICODE
181                         dev_len = unicode_strlen(device);
182                         if (dev_len > 20) {
183                                 printf("%s\n%20s", device, "");
184                         } else {
185                                 printf("%s%*s", device, 20 - (int)dev_len, "");
186                         }
187 #else
188                         if (printf("\n%-20s" + 1, device) > 20)
189                                     printf("\n%-20s", "");
190 #endif
191
192 #if ENABLE_FEATURE_HUMAN_READABLE
193                         printf(" %9s ",
194                                 /* f_blocks x f_bsize / df_disp_hr, show one fractional,
195                                  * use suffixes if df_disp_hr == 0 */
196                                 make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
197
198                         printf(" %9s " + 1,
199                                 /* EXPR x f_bsize / df_disp_hr, show one fractional,
200                                  * use suffixes if df_disp_hr == 0 */
201                                 make_human_readable_str((s.f_blocks - s.f_bfree),
202                                                 s.f_bsize, df_disp_hr));
203
204                         printf("%9s %3u%% %s\n",
205                                 /* f_bavail x f_bsize / df_disp_hr, show one fractional,
206                                  * use suffixes if df_disp_hr == 0 */
207                                 make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
208                                 blocks_percent_used, mount_point);
209 #else
210                         printf(" %9lu %9lu %9lu %3u%% %s\n",
211                                 kscale(s.f_blocks, s.f_bsize),
212                                 kscale(s.f_blocks - s.f_bfree, s.f_bsize),
213                                 kscale(s.f_bavail, s.f_bsize),
214                                 blocks_percent_used, mount_point);
215 #endif
216                 }
217         }
218
219         return status;
220 }