blob: 9c0d13f62b46cd64bb3a9fe9dce4c3e58a0e89d0 [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 Andersencb81e642003-07-14 21:21:08 +00005 * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
Eric Andersenc4996011999-10-20 22:08:37 +00006 * based on original code by (I think) Bruce Perens <bruce@pixar.com>.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Manuel Novoa III cad53642003-03-19 09:13:01 +000024/* BB_AUDIT SUSv3 _NOT_ compliant -- options -P and -t missing. Also blocksize. */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */
26
27/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
28 *
29 * Size reduction. Removed floating point dependency. Added error checking
30 * on output. Output stats on 0-sized filesystems if specificly listed on
31 * the command line. Properly round *-blocks, Used, and Available quantities.
32 */
33
Eric Andersencc8ed391999-10-05 16:24:54 +000034#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000035#include <stdlib.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000036#include <string.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000037#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000038#include <mntent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000039#include <sys/vfs.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000040#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000041
Manuel Novoa III cad53642003-03-19 09:13:01 +000042#ifndef CONFIG_FEATURE_HUMAN_READABLE
43static long kscale(long b, long bs)
Eric Andersencc8ed391999-10-05 16:24:54 +000044{
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 return ( b * (long long) bs + KILOBYTE/2 ) / KILOBYTE;
Eric Andersencc8ed391999-10-05 16:24:54 +000046}
Manuel Novoa III cad53642003-03-19 09:13:01 +000047#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000048
Eric Andersenc4996011999-10-20 22:08:37 +000049extern int df_main(int argc, char **argv)
50{
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 long blocks_used;
52 long blocks_percent_used;
53#ifdef CONFIG_FEATURE_HUMAN_READABLE
54 unsigned long df_disp_hr = KILOBYTE;
55#endif
Matt Kraai92ed8a32000-12-06 15:55:23 +000056 int status = EXIT_SUCCESS;
Eric Andersen8876fb22003-06-20 09:01:58 +000057 unsigned long opt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 FILE *mount_table;
59 struct mntent *mount_entry;
60 struct statfs s;
Manuel Novoa III 4eff1812003-03-19 09:42:02 +000061 static const char hdr_1k[] = "1k-blocks"; /* default display is kilobytes */
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 const char *disp_units_hdr = hdr_1k;
Richard June6d0921c2001-01-22 22:35:38 +000063
Eric Andersenbdfd0d72001-10-24 05:00:29 +000064#ifdef CONFIG_FEATURE_HUMAN_READABLE
Eric Andersen8876fb22003-06-20 09:01:58 +000065 bb_opt_complementaly = "h-km:k-hm:m-hk";
66 opt = bb_getopt_ulflags(argc, argv, "hmk");
67 if(opt & 1) {
Mark Whitleyae5612c2001-03-07 17:42:07 +000068 df_disp_hr = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 disp_units_hdr = " Size";
Eric Andersen8876fb22003-06-20 09:01:58 +000070 }
71 if(opt & 2) {
Manuel Novoa III 8f018392001-06-30 07:48:01 +000072 df_disp_hr = MEGABYTE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 disp_units_hdr = "1M-blocks";
Richard June6d0921c2001-01-22 22:35:38 +000074 }
Eric Andersen8876fb22003-06-20 09:01:58 +000075#else
76 opt = bb_getopt_ulflags(argc, argv, "k");
77#endif
Matt Kraai92ed8a32000-12-06 15:55:23 +000078
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 bb_printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n",
80 "", disp_units_hdr);
Eric Andersenc4996011999-10-20 22:08:37 +000081
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 mount_table = NULL;
83 argv += optind;
84 if (optind >= argc) {
85 if (!(mount_table = setmntent(bb_path_mtab_file, "r"))) {
86 bb_perror_msg_and_die(bb_path_mtab_file);
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 }
Eric Andersenc4996011999-10-20 22:08:37 +000088 }
89
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 do {
91 const char *device;
92 const char *mount_point;
93
94 if (mount_table) {
95 if (!(mount_entry = getmntent(mount_table))) {
96 endmntent(mount_table);
97 break;
98 }
99 } else {
100 if (!(mount_point = *argv++)) {
101 break;
102 }
103 if (!(mount_entry = find_mount_point(mount_point, bb_path_mtab_file))) {
104 bb_error_msg("%s: can't find mount point.", mount_point);
105 SET_ERROR:
106 status = EXIT_FAILURE;
107 continue;
108 }
109 }
110
111 device = mount_entry->mnt_fsname;
112 mount_point = mount_entry->mnt_dir;
113
114 if (statfs(mount_point, &s) != 0) {
115 bb_perror_msg("%s", mount_point);
116 goto SET_ERROR;
117 }
118
119 if ((s.f_blocks > 0) || !mount_table){
120 blocks_used = s.f_blocks - s.f_bfree;
121 blocks_percent_used = 0;
122 if (blocks_used + s.f_bavail) {
123 blocks_percent_used = (((long long) blocks_used) * 100
124 + (blocks_used + s.f_bavail)/2
125 ) / (blocks_used + s.f_bavail);
126 }
127
Eric Andersen9d7f0f02003-06-20 09:36:49 +0000128 if (strcmp(device, "rootfs") == 0) {
129 continue;
130 } else if (strcmp(device, "/dev/root") == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 /* Adjusts device to be the real root device,
132 * or leaves device alone if it can't find it */
Manuel Novoa III 3a9b0bf2003-03-23 20:27:33 +0000133 if ((device = find_real_root_device_name(device)) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000134 goto SET_ERROR;
135 }
136 }
137
138#ifdef CONFIG_FEATURE_HUMAN_READABLE
139 bb_printf("%-21s%9s ", device,
140 make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
141
142 bb_printf("%9s ",
143 make_human_readable_str( (s.f_blocks - s.f_bfree),
144 s.f_bsize, df_disp_hr));
145
146 bb_printf("%9s %3ld%% %s\n",
147 make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
148 blocks_percent_used, mount_point);
149#else
150 bb_printf("%-21s%9ld %9ld %9ld %3ld%% %s\n",
151 device,
152 kscale(s.f_blocks, s.f_bsize),
153 kscale(s.f_blocks-s.f_bfree, s.f_bsize),
154 kscale(s.f_bavail, s.f_bsize),
155 blocks_percent_used, mount_point);
156#endif
157 }
158
159 } while (1);
160
161 bb_fflush_stdout_and_exit(status);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000162}
Erik Andersen029011b2000-03-04 21:19:32 +0000163
164/*
165Local Variables:
166c-file-style: "linux"
167c-basic-offset: 4
168tab-width: 4
169End:
170*/