blob: 21a1dbb319fc3a04d619606437d3d2458e412bec [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 Andersen8ec10a92001-01-27 09:33:39 +00005 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Eric Andersenc4996011999-10-20 22:08:37 +00006 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 * 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
Eric Andersencc8ed391999-10-05 16:24:54 +000025#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000026#include <stdlib.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000027#include <string.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000028#include <mntent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000029#include <sys/vfs.h>
Eric Andersened3ef502001-01-27 08:24:39 +000030#include <getopt.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000031#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000032
Erik Andersene49d5ec2000-02-08 19:58:47 +000033extern const char mtab_file[]; /* Defined in utility.c */
Richard June6d0921c2001-01-22 22:35:38 +000034#ifdef BB_FEATURE_HUMAN_READABLE
Mark Whitleyae5612c2001-03-07 17:42:07 +000035static unsigned long df_disp_hr = KILOBYTE;
Richard June6d0921c2001-01-22 22:35:38 +000036#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000037
Mark Whitleyae5612c2001-03-07 17:42:07 +000038static int do_df(char *device, const char *mount_point)
Eric Andersencc8ed391999-10-05 16:24:54 +000039{
Erik Andersene49d5ec2000-02-08 19:58:47 +000040 struct statfs s;
41 long blocks_used;
42 long blocks_percent_used;
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000043#ifdef BB_FEATURE_HUMAN_READABLE
Eric Andersen5986f8d2001-03-07 03:50:03 +000044 long base;
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000045#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000046
Mark Whitleyae5612c2001-03-07 17:42:07 +000047 if (statfs(mount_point, &s) != 0) {
48 perror_msg("%s", mount_point);
Erik Andersene49d5ec2000-02-08 19:58:47 +000049 return FALSE;
Eric Andersen29d2e361999-11-06 06:07:27 +000050 }
Eric Andersencc8ed391999-10-05 16:24:54 +000051
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 if (s.f_blocks > 0) {
53 blocks_used = s.f_blocks - s.f_bfree;
Eric Andersen9e370072001-02-20 21:52:49 +000054 if(blocks_used == 0)
Eric Andersene1321192001-01-22 22:50:01 +000055 blocks_percent_used = 0;
Eric Andersen9e370072001-02-20 21:52:49 +000056 else {
Richard June6d0921c2001-01-22 22:35:38 +000057 blocks_percent_used = (long)
58 (blocks_used * 100.0 / (blocks_used + s.f_bavail) + 0.5);
Eric Andersen9e370072001-02-20 21:52:49 +000059 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 if (strcmp(device, "/dev/root") == 0) {
Erik Andersenec5bd902000-03-22 07:12:05 +000061 /* Adjusts device to be the real root device,
62 * or leaves device alone if it can't find it */
63 find_real_root_device_name( device);
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 }
Mark Whitleyae5612c2001-03-07 17:42:07 +000065#ifdef BB_FEATURE_HUMAN_READABLE
66 switch (df_disp_hr) {
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000067 case MEGABYTE:
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000068 base = KILOBYTE;
69 break;
70 case KILOBYTE:
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000071 base = 1;
72 break;
73 default:
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000074 base = 0;
75 }
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000076 printf("%-20s %9s ", device,
Eric Andersend9216842001-03-09 22:42:26 +000077 make_human_readable_str((unsigned long)(s.f_blocks *
78 (s.f_bsize/(double)KILOBYTE)), base));
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000079 printf("%9s ",
Eric Andersend9216842001-03-09 22:42:26 +000080 make_human_readable_str((unsigned long)(
81 (s.f_blocks - s.f_bfree) *
82 (s.f_bsize/(double)KILOBYTE)), base));
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000083 printf("%9s %3ld%% %s\n",
Eric Andersend9216842001-03-09 22:42:26 +000084 make_human_readable_str((unsigned long)(s.f_bavail *
85 (s.f_bsize/(double)KILOBYTE)), base),
Mark Whitleyae5612c2001-03-07 17:42:07 +000086 blocks_percent_used, mount_point);
Richard June6d0921c2001-01-22 22:35:38 +000087#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000089 device,
Eric Andersend9216842001-03-09 22:42:26 +000090 (long) (s.f_blocks * (s.f_bsize / (double)KILOBYTE)),
91 (long) ((s.f_blocks - s.f_bfree)*(s.f_bsize/(double)KILOBYTE)),
92 (long) (s.f_bavail * (s.f_bsize / (double)KILOBYTE)),
Mark Whitleyae5612c2001-03-07 17:42:07 +000093 blocks_percent_used, mount_point);
Eric Andersen9e370072001-02-20 21:52:49 +000094#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 }
96
97 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +000098}
99
Eric Andersenc4996011999-10-20 22:08:37 +0000100extern int df_main(int argc, char **argv)
101{
Matt Kraai92ed8a32000-12-06 15:55:23 +0000102 int status = EXIT_SUCCESS;
Richard June6d0921c2001-01-22 22:35:38 +0000103 int opt = 0;
104 int i = 0;
Mark Whitleyae5612c2001-03-07 17:42:07 +0000105 char disp_units_hdr[80] = "1k-blocks"; /* default display is kilobytes */
Richard June6d0921c2001-01-22 22:35:38 +0000106
Mark Whitleyae5612c2001-03-07 17:42:07 +0000107 while ((opt = getopt(argc, argv, "k"
Richard June6d0921c2001-01-22 22:35:38 +0000108#ifdef BB_FEATURE_HUMAN_READABLE
109 "hm"
110#endif
Richard June6d0921c2001-01-22 22:35:38 +0000111)) > 0)
112 {
113 switch (opt) {
114#ifdef BB_FEATURE_HUMAN_READABLE
Mark Whitleyae5612c2001-03-07 17:42:07 +0000115 case 'h':
116 df_disp_hr = 0;
117 strcpy(disp_units_hdr, " Size");
118 break;
119 case 'm':
120 df_disp_hr = MEGABYTE;
121 strcpy(disp_units_hdr, "1M-blocks");
122 break;
Richard June6d0921c2001-01-22 22:35:38 +0000123#endif
Mark Whitleyae5612c2001-03-07 17:42:07 +0000124 case 'k':
125 /* default display is kilobytes */
126 break;
Eric Andersenb50da532001-02-17 16:52:35 +0000127 default:
128 show_usage();
Richard June6d0921c2001-01-22 22:35:38 +0000129 }
130 }
Matt Kraai92ed8a32000-12-06 15:55:23 +0000131
Mark Whitleyae5612c2001-03-07 17:42:07 +0000132 printf("%-20s %-14s %s %s %s %s\n", "Filesystem", disp_units_hdr,
Richard June6d0921c2001-01-22 22:35:38 +0000133 "Used", "Available", "Use%", "Mounted on");
Eric Andersenc4996011999-10-20 22:08:37 +0000134
Richard June6d0921c2001-01-22 22:35:38 +0000135 if(optind < argc) {
Mark Whitleyae5612c2001-03-07 17:42:07 +0000136 struct mntent *mount_entry;
Richard June6d0921c2001-01-22 22:35:38 +0000137 for(i = optind; i < argc; i++)
138 {
Mark Whitleyae5612c2001-03-07 17:42:07 +0000139 if ((mount_entry = find_mount_point(argv[i], mtab_file)) == 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000140 error_msg("%s: can't find mount point.", argv[i]);
Matt Kraai92ed8a32000-12-06 15:55:23 +0000141 status = EXIT_FAILURE;
Mark Whitleyae5612c2001-03-07 17:42:07 +0000142 } else if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir))
Matt Kraai92ed8a32000-12-06 15:55:23 +0000143 status = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145 } else {
Mark Whitleyae5612c2001-03-07 17:42:07 +0000146 FILE *mount_table;
147 struct mntent *mount_entry;
Eric Andersenc4996011999-10-20 22:08:37 +0000148
Mark Whitleyae5612c2001-03-07 17:42:07 +0000149 mount_table = setmntent(mtab_file, "r");
150 if (mount_table == 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000151 perror_msg("%s", mtab_file);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000152 return EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 }
154
Mark Whitleyae5612c2001-03-07 17:42:07 +0000155 while ((mount_entry = getmntent(mount_table))) {
156 if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir))
Matt Kraai92ed8a32000-12-06 15:55:23 +0000157 status = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000158 }
Mark Whitleyae5612c2001-03-07 17:42:07 +0000159 endmntent(mount_table);
Eric Andersenc4996011999-10-20 22:08:37 +0000160 }
161
Matt Kraai92ed8a32000-12-06 15:55:23 +0000162 return status;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000163}
Erik Andersen029011b2000-03-04 21:19:32 +0000164
165/*
166Local Variables:
167c-file-style: "linux"
168c-basic-offset: 4
169tab-width: 4
170End:
171*/