blob: 6ae3eed3358e9f17f56611afe4c1233dccf170e0 [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{
Denis Vlasenkodca0b702006-10-27 09:05:02 +000032 return ( b * (long long) bs + 1024/2 ) / 1024;
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
Denis Vlasenkodca0b702006-10-27 09:05:02 +000041 unsigned long df_disp_hr = 1024;
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");
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000054 if (opt & 1) {
55 df_disp_hr = 0;
56 disp_units_hdr = " Size";
Eric Andersen8876fb22003-06-20 09:01:58 +000057 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000058 if (opt & 2) {
Denis Vlasenkodca0b702006-10-27 09:05:02 +000059 df_disp_hr = 1024*1024;
Denis Vlasenkof0ed3762006-10-26 23:21:47 +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
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000066 printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n",
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 "", 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) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000072 mount_table = setmntent(bb_path_mtab_file, "r");
73 if (!mount_table) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 bb_perror_msg_and_die(bb_path_mtab_file);
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 }
Eric Andersenc4996011999-10-20 22:08:37 +000076 }
77
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 do {
79 const char *device;
80 const char *mount_point;
81
82 if (mount_table) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000083 mount_entry = getmntent(mount_table);
84 if (!mount_entry) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000085 endmntent(mount_table);
86 break;
87 }
88 } else {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000089 mount_point = *argv++;
90 if (!mount_point) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 break;
92 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000093 mount_entry = find_mount_point(mount_point, bb_path_mtab_file);
94 if (!mount_entry) {
95 bb_error_msg("%s: can't find mount point", mount_point);
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 SET_ERROR:
97 status = EXIT_FAILURE;
98 continue;
99 }
100 }
101
102 device = mount_entry->mnt_fsname;
103 mount_point = mount_entry->mnt_dir;
104
105 if (statfs(mount_point, &s) != 0) {
106 bb_perror_msg("%s", mount_point);
107 goto SET_ERROR;
108 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000109
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 if ((s.f_blocks > 0) || !mount_table){
111 blocks_used = s.f_blocks - s.f_bfree;
112 blocks_percent_used = 0;
113 if (blocks_used + s.f_bavail) {
114 blocks_percent_used = (((long long) blocks_used) * 100
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000115 + (blocks_used + s.f_bavail)/2
116 ) / (blocks_used + s.f_bavail);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000118
Eric Andersen9d7f0f02003-06-20 09:36:49 +0000119 if (strcmp(device, "rootfs") == 0) {
120 continue;
121 } else if (strcmp(device, "/dev/root") == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000122 /* Adjusts device to be the real root device,
123 * or leaves device alone if it can't find it */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000124 device = find_block_device("/");
125 if (!device) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126 goto SET_ERROR;
127 }
128 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000129
Manuel Novoa III cad53642003-03-19 09:13:01 +0000130#ifdef CONFIG_FEATURE_HUMAN_READABLE
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000131 printf("%-20s %9s ", device,
132 make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000133
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000134 printf("%9s ",
135 make_human_readable_str( (s.f_blocks - s.f_bfree),
136 s.f_bsize, df_disp_hr));
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000137
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000138 printf("%9s %3ld%% %s\n",
Manuel Novoa III cad53642003-03-19 09:13:01 +0000139 make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
140 blocks_percent_used, mount_point);
141#else
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000142 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 device,
144 kscale(s.f_blocks, s.f_bsize),
145 kscale(s.f_blocks-s.f_bfree, s.f_bsize),
146 kscale(s.f_bavail, s.f_bsize),
147 blocks_percent_used, mount_point);
148#endif
149 }
150
151 } while (1);
152
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000153 fflush_stdout_and_exit(status);
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000154}