blob: 94ead32eb36e276ade107f9dc8dc03f23c30f211 [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 Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 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 *
Bernhard Reutner-Fischerb1629b12006-05-19 19:29:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenc4996011999-10-20 22:08:37 +00009 */
10
Manuel Novoa III cad53642003-03-19 09:13:01 +000011/* BB_AUDIT SUSv3 _NOT_ compliant -- options -P and -t missing. Also blocksize. */
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
Eric Andersenaff114c2004-04-14 17:51:38 +000017 * on output. Output stats on 0-sized filesystems if specifically listed on
Manuel Novoa III cad53642003-03-19 09:13:01 +000018 * the command line. Properly round *-blocks, Used, and Available quantities.
19 */
20
Eric Andersencc8ed391999-10-05 16:24:54 +000021#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000022#include <stdlib.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000023#include <string.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000024#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000025#include <mntent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000026#include <sys/vfs.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000027#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000028
Manuel Novoa III cad53642003-03-19 09:13:01 +000029#ifndef CONFIG_FEATURE_HUMAN_READABLE
30static long kscale(long b, long bs)
Eric Andersencc8ed391999-10-05 16:24:54 +000031{
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 return ( b * (long long) bs + KILOBYTE/2 ) / KILOBYTE;
Eric Andersencc8ed391999-10-05 16:24:54 +000033}
Manuel Novoa III cad53642003-03-19 09:13:01 +000034#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000035
Rob Landleydfba7412006-03-06 20:47:33 +000036int df_main(int argc, char **argv)
Eric Andersenc4996011999-10-20 22:08:37 +000037{
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 long blocks_used;
39 long blocks_percent_used;
40#ifdef CONFIG_FEATURE_HUMAN_READABLE
Eric Andersenc7bda1c2004-03-15 08:29:22 +000041 unsigned long df_disp_hr = KILOBYTE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000042#endif
Matt Kraai92ed8a32000-12-06 15:55:23 +000043 int status = EXIT_SUCCESS;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000044 unsigned opt;
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 FILE *mount_table;
46 struct mntent *mount_entry;
47 struct statfs s;
Manuel Novoa III 4eff1812003-03-19 09:42:02 +000048 static const char hdr_1k[] = "1k-blocks"; /* default display is kilobytes */
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 const char *disp_units_hdr = hdr_1k;
Richard June6d0921c2001-01-22 22:35:38 +000050
Eric Andersenbdfd0d72001-10-24 05:00:29 +000051#ifdef CONFIG_FEATURE_HUMAN_READABLE
Denis Vlasenko67b23e62006-10-03 21:00:06 +000052 opt_complementary = "h-km:k-hm:m-hk";
53 opt = getopt32(argc, argv, "hmk");
Eric Andersen8876fb22003-06-20 09:01:58 +000054 if(opt & 1) {
Mark Whitleyae5612c2001-03-07 17:42:07 +000055 df_disp_hr = 0;
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 disp_units_hdr = " Size";
Eric Andersen8876fb22003-06-20 09:01:58 +000057 }
58 if(opt & 2) {
Manuel Novoa III 8f018392001-06-30 07:48:01 +000059 df_disp_hr = MEGABYTE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 disp_units_hdr = "1M-blocks";
Richard June6d0921c2001-01-22 22:35:38 +000061 }
Eric Andersen8876fb22003-06-20 09:01:58 +000062#else
Denis Vlasenko67b23e62006-10-03 21:00:06 +000063 opt = getopt32(argc, argv, "k");
Eric Andersen8876fb22003-06-20 09:01:58 +000064#endif
Matt Kraai92ed8a32000-12-06 15:55:23 +000065
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 bb_printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n",
67 "", disp_units_hdr);
Eric Andersenc4996011999-10-20 22:08:37 +000068
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 mount_table = NULL;
70 argv += optind;
71 if (optind >= argc) {
72 if (!(mount_table = setmntent(bb_path_mtab_file, "r"))) {
73 bb_perror_msg_and_die(bb_path_mtab_file);
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 }
Eric Andersenc4996011999-10-20 22:08:37 +000075 }
76
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 do {
78 const char *device;
79 const char *mount_point;
80
81 if (mount_table) {
82 if (!(mount_entry = getmntent(mount_table))) {
83 endmntent(mount_table);
84 break;
85 }
86 } else {
87 if (!(mount_point = *argv++)) {
88 break;
89 }
90 if (!(mount_entry = find_mount_point(mount_point, bb_path_mtab_file))) {
91 bb_error_msg("%s: can't find mount point.", mount_point);
92 SET_ERROR:
93 status = EXIT_FAILURE;
94 continue;
95 }
96 }
97
98 device = mount_entry->mnt_fsname;
99 mount_point = mount_entry->mnt_dir;
100
101 if (statfs(mount_point, &s) != 0) {
102 bb_perror_msg("%s", mount_point);
103 goto SET_ERROR;
104 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000105
Manuel Novoa III cad53642003-03-19 09:13:01 +0000106 if ((s.f_blocks > 0) || !mount_table){
107 blocks_used = s.f_blocks - s.f_bfree;
108 blocks_percent_used = 0;
109 if (blocks_used + s.f_bavail) {
110 blocks_percent_used = (((long long) blocks_used) * 100
111 + (blocks_used + s.f_bavail)/2
112 ) / (blocks_used + s.f_bavail);
113 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000114
Eric Andersen9d7f0f02003-06-20 09:36:49 +0000115 if (strcmp(device, "rootfs") == 0) {
116 continue;
117 } else if (strcmp(device, "/dev/root") == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118 /* Adjusts device to be the real root device,
119 * or leaves device alone if it can't find it */
Rob Landley6a6798b2005-08-10 20:35:54 +0000120 if ((device = find_block_device("/")) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 goto SET_ERROR;
122 }
123 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000124
Manuel Novoa III cad53642003-03-19 09:13:01 +0000125#ifdef CONFIG_FEATURE_HUMAN_READABLE
Rob Landley98ea8492005-09-10 02:59:35 +0000126 bb_printf("%-20s %9s ", device,
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127 make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000128
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 bb_printf("%9s ",
130 make_human_readable_str( (s.f_blocks - s.f_bfree),
131 s.f_bsize, df_disp_hr));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000132
Manuel Novoa III cad53642003-03-19 09:13:01 +0000133 bb_printf("%9s %3ld%% %s\n",
134 make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
135 blocks_percent_used, mount_point);
136#else
Rob Landley98ea8492005-09-10 02:59:35 +0000137 bb_printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
Manuel Novoa III cad53642003-03-19 09:13:01 +0000138 device,
139 kscale(s.f_blocks, s.f_bsize),
140 kscale(s.f_blocks-s.f_bfree, s.f_bsize),
141 kscale(s.f_bavail, s.f_bsize),
142 blocks_percent_used, mount_point);
143#endif
144 }
145
146 } while (1);
147
148 bb_fflush_stdout_and_exit(status);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000149}