blob: 7d007a0034068abcb3378fd907f9cd8ba4ed3ac5 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
3 * Mini df implementation for busybox
4 *
Eric Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
Eric Andersenc4996011999-10-20 22:08:37 +00007 * based on original code by (I think) Bruce Perens <bruce@pixar.com>.
8 *
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.
13 *
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.
18 *
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
22 *
23 */
24
Manuel Novoa III cad53642003-03-19 09:13:01 +000025/* BB_AUDIT SUSv3 _NOT_ compliant -- options -P and -t missing. Also blocksize. */
26/* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */
27
28/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
29 *
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.
33 */
34
Eric Andersencc8ed391999-10-05 16:24:54 +000035#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000036#include <stdlib.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000037#include <string.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000038#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000039#include <mntent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000040#include <sys/vfs.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000041#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000042
Manuel Novoa III cad53642003-03-19 09:13:01 +000043#ifndef CONFIG_FEATURE_HUMAN_READABLE
44static long kscale(long b, long bs)
Eric Andersencc8ed391999-10-05 16:24:54 +000045{
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 return ( b * (long long) bs + KILOBYTE/2 ) / KILOBYTE;
Eric Andersencc8ed391999-10-05 16:24:54 +000047}
Manuel Novoa III cad53642003-03-19 09:13:01 +000048#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000049
Eric Andersenc4996011999-10-20 22:08:37 +000050extern int df_main(int argc, char **argv)
51{
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 long blocks_used;
53 long blocks_percent_used;
54#ifdef CONFIG_FEATURE_HUMAN_READABLE
55 unsigned long df_disp_hr = KILOBYTE;
56#endif
Matt Kraai92ed8a32000-12-06 15:55:23 +000057 int status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 int opt;
59 FILE *mount_table;
60 struct mntent *mount_entry;
61 struct statfs s;
62 static const char hdr_1k[] = "1k-blocks"; /* default display is kilobytes */;
63 const char *disp_units_hdr = hdr_1k;
Richard June6d0921c2001-01-22 22:35:38 +000064
Mark Whitleyae5612c2001-03-07 17:42:07 +000065 while ((opt = getopt(argc, argv, "k"
Eric Andersenbdfd0d72001-10-24 05:00:29 +000066#ifdef CONFIG_FEATURE_HUMAN_READABLE
Richard June6d0921c2001-01-22 22:35:38 +000067 "hm"
68#endif
Richard June6d0921c2001-01-22 22:35:38 +000069)) > 0)
70 {
71 switch (opt) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +000072#ifdef CONFIG_FEATURE_HUMAN_READABLE
Mark Whitleyae5612c2001-03-07 17:42:07 +000073 case 'h':
74 df_disp_hr = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 disp_units_hdr = " Size";
Mark Whitleyae5612c2001-03-07 17:42:07 +000076 break;
77 case 'm':
Manuel Novoa III 8f018392001-06-30 07:48:01 +000078 df_disp_hr = MEGABYTE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 disp_units_hdr = "1M-blocks";
Mark Whitleyae5612c2001-03-07 17:42:07 +000080 break;
Richard June6d0921c2001-01-22 22:35:38 +000081#endif
Mark Whitleyae5612c2001-03-07 17:42:07 +000082 case 'k':
83 /* default display is kilobytes */
Manuel Novoa III cad53642003-03-19 09:13:01 +000084#ifdef CONFIG_FEATURE_HUMAN_READABLE
85 df_disp_hr = KILOBYTE;
86 disp_units_hdr = hdr_1k;
87#endif
Mark Whitleyae5612c2001-03-07 17:42:07 +000088 break;
Eric Andersenb50da532001-02-17 16:52:35 +000089 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 bb_show_usage();
Richard June6d0921c2001-01-22 22:35:38 +000091 }
92 }
Matt Kraai92ed8a32000-12-06 15:55:23 +000093
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 bb_printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n",
95 "", disp_units_hdr);
Eric Andersenc4996011999-10-20 22:08:37 +000096
Manuel Novoa III cad53642003-03-19 09:13:01 +000097 mount_table = NULL;
98 argv += optind;
99 if (optind >= argc) {
100 if (!(mount_table = setmntent(bb_path_mtab_file, "r"))) {
101 bb_perror_msg_and_die(bb_path_mtab_file);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 }
Eric Andersenc4996011999-10-20 22:08:37 +0000103 }
104
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 do {
106 const char *device;
107 const char *mount_point;
108
109 if (mount_table) {
110 if (!(mount_entry = getmntent(mount_table))) {
111 endmntent(mount_table);
112 break;
113 }
114 } else {
115 if (!(mount_point = *argv++)) {
116 break;
117 }
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);
120 SET_ERROR:
121 status = EXIT_FAILURE;
122 continue;
123 }
124 }
125
126 device = mount_entry->mnt_fsname;
127 mount_point = mount_entry->mnt_dir;
128
129 if (statfs(mount_point, &s) != 0) {
130 bb_perror_msg("%s", mount_point);
131 goto SET_ERROR;
132 }
133
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);
141 }
142
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) {
147 goto SET_ERROR;
148 }
149 }
150
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));
154
155 bb_printf("%9s ",
156 make_human_readable_str( (s.f_blocks - s.f_bfree),
157 s.f_bsize, df_disp_hr));
158
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);
162#else
163 bb_printf("%-21s%9ld %9ld %9ld %3ld%% %s\n",
164 device,
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);
169#endif
170 }
171
172 } while (1);
173
174 bb_fflush_stdout_and_exit(status);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000175}
Erik Andersen029011b2000-03-04 21:19:32 +0000176
177/*
178Local Variables:
179c-file-style: "linux"
180c-basic-offset: 4
181tab-width: 4
182End:
183*/