blob: df68744337e2ebf4adc52ad2c43e3fd7a7681cd6 [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 */
Eric Andersenc911a432001-05-15 17:42:16 +000063 device = find_real_root_device_name(device);
64 if(device==NULL)
65 return FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 }
Mark Whitleyae5612c2001-03-07 17:42:07 +000067#ifdef BB_FEATURE_HUMAN_READABLE
68 switch (df_disp_hr) {
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000069 case MEGABYTE:
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000070 base = KILOBYTE;
71 break;
72 case KILOBYTE:
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000073 base = 1;
74 break;
75 default:
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000076 base = 0;
77 }
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000078 printf("%-20s %9s ", device,
Eric Andersend9216842001-03-09 22:42:26 +000079 make_human_readable_str((unsigned long)(s.f_blocks *
80 (s.f_bsize/(double)KILOBYTE)), base));
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000081 printf("%9s ",
Eric Andersend9216842001-03-09 22:42:26 +000082 make_human_readable_str((unsigned long)(
83 (s.f_blocks - s.f_bfree) *
84 (s.f_bsize/(double)KILOBYTE)), base));
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000085 printf("%9s %3ld%% %s\n",
Eric Andersend9216842001-03-09 22:42:26 +000086 make_human_readable_str((unsigned long)(s.f_bavail *
87 (s.f_bsize/(double)KILOBYTE)), base),
Mark Whitleyae5612c2001-03-07 17:42:07 +000088 blocks_percent_used, mount_point);
Richard June6d0921c2001-01-22 22:35:38 +000089#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000091 device,
Eric Andersend9216842001-03-09 22:42:26 +000092 (long) (s.f_blocks * (s.f_bsize / (double)KILOBYTE)),
93 (long) ((s.f_blocks - s.f_bfree)*(s.f_bsize/(double)KILOBYTE)),
94 (long) (s.f_bavail * (s.f_bsize / (double)KILOBYTE)),
Mark Whitleyae5612c2001-03-07 17:42:07 +000095 blocks_percent_used, mount_point);
Eric Andersen9e370072001-02-20 21:52:49 +000096#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 }
98
99 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000100}
101
Eric Andersenc4996011999-10-20 22:08:37 +0000102extern int df_main(int argc, char **argv)
103{
Matt Kraai92ed8a32000-12-06 15:55:23 +0000104 int status = EXIT_SUCCESS;
Richard June6d0921c2001-01-22 22:35:38 +0000105 int opt = 0;
106 int i = 0;
Mark Whitleyae5612c2001-03-07 17:42:07 +0000107 char disp_units_hdr[80] = "1k-blocks"; /* default display is kilobytes */
Richard June6d0921c2001-01-22 22:35:38 +0000108
Mark Whitleyae5612c2001-03-07 17:42:07 +0000109 while ((opt = getopt(argc, argv, "k"
Richard June6d0921c2001-01-22 22:35:38 +0000110#ifdef BB_FEATURE_HUMAN_READABLE
111 "hm"
112#endif
Richard June6d0921c2001-01-22 22:35:38 +0000113)) > 0)
114 {
115 switch (opt) {
116#ifdef BB_FEATURE_HUMAN_READABLE
Mark Whitleyae5612c2001-03-07 17:42:07 +0000117 case 'h':
118 df_disp_hr = 0;
119 strcpy(disp_units_hdr, " Size");
120 break;
121 case 'm':
122 df_disp_hr = MEGABYTE;
123 strcpy(disp_units_hdr, "1M-blocks");
124 break;
Richard June6d0921c2001-01-22 22:35:38 +0000125#endif
Mark Whitleyae5612c2001-03-07 17:42:07 +0000126 case 'k':
127 /* default display is kilobytes */
128 break;
Eric Andersenb50da532001-02-17 16:52:35 +0000129 default:
130 show_usage();
Richard June6d0921c2001-01-22 22:35:38 +0000131 }
132 }
Matt Kraai92ed8a32000-12-06 15:55:23 +0000133
Mark Whitleyae5612c2001-03-07 17:42:07 +0000134 printf("%-20s %-14s %s %s %s %s\n", "Filesystem", disp_units_hdr,
Richard June6d0921c2001-01-22 22:35:38 +0000135 "Used", "Available", "Use%", "Mounted on");
Eric Andersenc4996011999-10-20 22:08:37 +0000136
Richard June6d0921c2001-01-22 22:35:38 +0000137 if(optind < argc) {
Mark Whitleyae5612c2001-03-07 17:42:07 +0000138 struct mntent *mount_entry;
Richard June6d0921c2001-01-22 22:35:38 +0000139 for(i = optind; i < argc; i++)
140 {
Mark Whitleyae5612c2001-03-07 17:42:07 +0000141 if ((mount_entry = find_mount_point(argv[i], mtab_file)) == 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000142 error_msg("%s: can't find mount point.", argv[i]);
Matt Kraai92ed8a32000-12-06 15:55:23 +0000143 status = EXIT_FAILURE;
Mark Whitleyae5612c2001-03-07 17:42:07 +0000144 } else if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir))
Matt Kraai92ed8a32000-12-06 15:55:23 +0000145 status = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 } else {
Mark Whitleyae5612c2001-03-07 17:42:07 +0000148 FILE *mount_table;
149 struct mntent *mount_entry;
Eric Andersenc4996011999-10-20 22:08:37 +0000150
Mark Whitleyae5612c2001-03-07 17:42:07 +0000151 mount_table = setmntent(mtab_file, "r");
152 if (mount_table == 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000153 perror_msg("%s", mtab_file);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000154 return EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000155 }
156
Mark Whitleyae5612c2001-03-07 17:42:07 +0000157 while ((mount_entry = getmntent(mount_table))) {
158 if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir))
Matt Kraai92ed8a32000-12-06 15:55:23 +0000159 status = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160 }
Mark Whitleyae5612c2001-03-07 17:42:07 +0000161 endmntent(mount_table);
Eric Andersenc4996011999-10-20 22:08:37 +0000162 }
163
Matt Kraai92ed8a32000-12-06 15:55:23 +0000164 return status;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000165}
Erik Andersen029011b2000-03-04 21:19:32 +0000166
167/*
168Local Variables:
169c-file-style: "linux"
170c-basic-offset: 4
171tab-width: 4
172End:
173*/