1 /* vi: set sw=4 ts=4: */
3 * Mini df implementation for busybox
5 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
7 * based on original code by (I think) Bruce Perens <bruce@pixar.com>.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 /* BB_AUDIT SUSv3 _NOT_ compliant -- options -P and -t missing. Also blocksize. */
26 /* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */
28 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
30 * Size reduction. Removed floating point dependency. Added error checking
31 * on output. Output stats on 0-sized filesystems if specificly listed on
32 * the command line. Properly round *-blocks, Used, and Available quantities.
43 #ifndef CONFIG_FEATURE_HUMAN_READABLE
44 static long kscale(long b, long bs)
46 return ( b * (long long) bs + KILOBYTE/2 ) / KILOBYTE;
50 extern int df_main(int argc, char **argv)
53 long blocks_percent_used;
54 #ifdef CONFIG_FEATURE_HUMAN_READABLE
55 unsigned long df_disp_hr = KILOBYTE;
57 int status = EXIT_SUCCESS;
60 struct mntent *mount_entry;
62 static const char hdr_1k[] = "1k-blocks"; /* default display is kilobytes */;
63 const char *disp_units_hdr = hdr_1k;
65 while ((opt = getopt(argc, argv, "k"
66 #ifdef CONFIG_FEATURE_HUMAN_READABLE
72 #ifdef CONFIG_FEATURE_HUMAN_READABLE
75 disp_units_hdr = " Size";
78 df_disp_hr = MEGABYTE;
79 disp_units_hdr = "1M-blocks";
83 /* default display is kilobytes */
84 #ifdef CONFIG_FEATURE_HUMAN_READABLE
85 df_disp_hr = KILOBYTE;
86 disp_units_hdr = hdr_1k;
94 bb_printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n",
100 if (!(mount_table = setmntent(bb_path_mtab_file, "r"))) {
101 bb_perror_msg_and_die(bb_path_mtab_file);
107 const char *mount_point;
110 if (!(mount_entry = getmntent(mount_table))) {
111 endmntent(mount_table);
115 if (!(mount_point = *argv++)) {
118 if (!(mount_entry = find_mount_point(mount_point, bb_path_mtab_file))) {
119 bb_error_msg("%s: can't find mount point.", mount_point);
121 status = EXIT_FAILURE;
126 device = mount_entry->mnt_fsname;
127 mount_point = mount_entry->mnt_dir;
129 if (statfs(mount_point, &s) != 0) {
130 bb_perror_msg("%s", mount_point);
134 if ((s.f_blocks > 0) || !mount_table){
135 blocks_used = s.f_blocks - s.f_bfree;
136 blocks_percent_used = 0;
137 if (blocks_used + s.f_bavail) {
138 blocks_percent_used = (((long long) blocks_used) * 100
139 + (blocks_used + s.f_bavail)/2
140 ) / (blocks_used + s.f_bavail);
143 if (strcmp(device, "/dev/root") == 0) {
144 /* Adjusts device to be the real root device,
145 * or leaves device alone if it can't find it */
146 if ((device = find_real_root_device_name(device)) != NULL) {
151 #ifdef CONFIG_FEATURE_HUMAN_READABLE
152 bb_printf("%-21s%9s ", device,
153 make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
156 make_human_readable_str( (s.f_blocks - s.f_bfree),
157 s.f_bsize, df_disp_hr));
159 bb_printf("%9s %3ld%% %s\n",
160 make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
161 blocks_percent_used, mount_point);
163 bb_printf("%-21s%9ld %9ld %9ld %3ld%% %s\n",
165 kscale(s.f_blocks, s.f_bsize),
166 kscale(s.f_blocks-s.f_bfree, s.f_bsize),
167 kscale(s.f_bavail, s.f_bsize),
168 blocks_percent_used, mount_point);
174 bb_fflush_stdout_and_exit(status);
179 c-file-style: "linux"