blob: 9f8770fe6949b36e3fdd83ea076fce523e7b734a [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 Andersencc8ed391999-10-05 16:24:54 +000027#include <mntent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000028#include <sys/vfs.h>
Eric Andersened3ef502001-01-27 08:24:39 +000029#include <getopt.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000030#include "busybox.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000031
Erik Andersene49d5ec2000-02-08 19:58:47 +000032extern const char mtab_file[]; /* Defined in utility.c */
Richard June6d0921c2001-01-22 22:35:38 +000033#ifdef BB_FEATURE_HUMAN_READABLE
Eric Andersenec9fad92001-03-07 06:04:08 +000034static unsigned long disp_hr = KILOBYTE;
Richard June6d0921c2001-01-22 22:35:38 +000035#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000036
Eric Andersenc4996011999-10-20 22:08:37 +000037static int df(char *device, const char *mountPoint)
Eric Andersencc8ed391999-10-05 16:24:54 +000038{
Erik Andersene49d5ec2000-02-08 19:58:47 +000039 struct statfs s;
40 long blocks_used;
41 long blocks_percent_used;
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000042#ifdef BB_FEATURE_HUMAN_READABLE
Eric Andersen5986f8d2001-03-07 03:50:03 +000043 long base;
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000044#endif
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Erik Andersene49d5ec2000-02-08 19:58:47 +000046 if (statfs(mountPoint, &s) != 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000047 perror_msg("%s", mountPoint);
Erik Andersene49d5ec2000-02-08 19:58:47 +000048 return FALSE;
Eric Andersen29d2e361999-11-06 06:07:27 +000049 }
Eric Andersencc8ed391999-10-05 16:24:54 +000050
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 if (s.f_blocks > 0) {
52 blocks_used = s.f_blocks - s.f_bfree;
Eric Andersen9e370072001-02-20 21:52:49 +000053 if(blocks_used == 0)
Eric Andersene1321192001-01-22 22:50:01 +000054 blocks_percent_used = 0;
Eric Andersen9e370072001-02-20 21:52:49 +000055 else {
Richard June6d0921c2001-01-22 22:35:38 +000056 blocks_percent_used = (long)
57 (blocks_used * 100.0 / (blocks_used + s.f_bavail) + 0.5);
Eric Andersen9e370072001-02-20 21:52:49 +000058 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 if (strcmp(device, "/dev/root") == 0) {
Erik Andersenec5bd902000-03-22 07:12:05 +000060 /* Adjusts device to be the real root device,
61 * or leaves device alone if it can't find it */
62 find_real_root_device_name( device);
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 }
Eric Andersen5986f8d2001-03-07 03:50:03 +000064 #ifdef BB_FEATURE_HUMAN_READABLE
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000065 switch (disp_hr) {
66 case MEGABYTE:
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000067 base = KILOBYTE;
68 break;
69 case KILOBYTE:
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000070 base = 1;
71 break;
72 default:
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000073 base = 0;
74 }
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000075 printf("%-20s %9s ", device,
Eric Andersen5986f8d2001-03-07 03:50:03 +000076 format(s.f_blocks * (s.f_bsize/KILOBYTE), base));
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000077 printf("%9s ",
Eric Andersen5986f8d2001-03-07 03:50:03 +000078 format((s.f_blocks - s.f_bfree) * (s.f_bsize/KILOBYTE), base));
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000079 printf("%9s %3ld%% %s\n",
Eric Andersen5986f8d2001-03-07 03:50:03 +000080 format(s.f_bavail * (s.f_bsize/KILOBYTE), base),
Richard June6d0921c2001-01-22 22:35:38 +000081 blocks_percent_used, mountPoint);
82#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000084 device,
85 (long) (s.f_blocks * (s.f_bsize / KILOBYTE)),
86 (long) ((s.f_blocks - s.f_bfree) * (s.f_bsize / KILOBYTE)),
87 (long) (s.f_bavail * (s.f_bsize / KILOBYTE)),
88 blocks_percent_used, mountPoint);
Eric Andersen9e370072001-02-20 21:52:49 +000089#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 }
91
92 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +000093}
94
Eric Andersenc4996011999-10-20 22:08:37 +000095extern int df_main(int argc, char **argv)
96{
Matt Kraai92ed8a32000-12-06 15:55:23 +000097 int status = EXIT_SUCCESS;
Richard June6d0921c2001-01-22 22:35:38 +000098 int opt = 0;
99 int i = 0;
100
Eric Andersenb50da532001-02-17 16:52:35 +0000101 while ((opt = getopt(argc, argv,
Richard June6d0921c2001-01-22 22:35:38 +0000102#ifdef BB_FEATURE_HUMAN_READABLE
103 "hm"
104#endif
105 "k"
106)) > 0)
107 {
108 switch (opt) {
109#ifdef BB_FEATURE_HUMAN_READABLE
110 case 'h': disp_hr = 0; break;
111 case 'm': disp_hr = MEGABYTE; break;
Richard June6d0921c2001-01-22 22:35:38 +0000112#endif
Eric Andersen9e370072001-02-20 21:52:49 +0000113 case 'k': break;
Eric Andersenb50da532001-02-17 16:52:35 +0000114 default:
115 show_usage();
Richard June6d0921c2001-01-22 22:35:38 +0000116 }
117 }
Matt Kraai92ed8a32000-12-06 15:55:23 +0000118
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 printf("%-20s %-14s %s %s %s %s\n", "Filesystem",
Richard June6d0921c2001-01-22 22:35:38 +0000120#ifdef BB_FEATURE_HUMAN_READABLE
Mark Whitley8d7bdc92001-02-20 21:57:55 +0000121 (KILOBYTE == disp_hr) ? "1k-blocks" :
122 (MEGABYTE == disp_hr) ? "1M-blocks" : " Size",
Richard June6d0921c2001-01-22 22:35:38 +0000123#else
124 "1k-blocks",
125#endif
126 "Used", "Available", "Use%", "Mounted on");
Eric Andersenc4996011999-10-20 22:08:37 +0000127
Richard June6d0921c2001-01-22 22:35:38 +0000128
129 if(optind < argc) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 struct mntent *mountEntry;
Richard June6d0921c2001-01-22 22:35:38 +0000131 for(i = optind; i < argc; i++)
132 {
133 if ((mountEntry = find_mount_point(argv[i], mtab_file)) == 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000134 error_msg("%s: can't find mount point.", argv[i]);
Matt Kraai92ed8a32000-12-06 15:55:23 +0000135 status = EXIT_FAILURE;
136 } else if (!df(mountEntry->mnt_fsname, mountEntry->mnt_dir))
137 status = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000138 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139 } else {
140 FILE *mountTable;
141 struct mntent *mountEntry;
Eric Andersenc4996011999-10-20 22:08:37 +0000142
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 mountTable = setmntent(mtab_file, "r");
144 if (mountTable == 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000145 perror_msg("%s", mtab_file);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000146 return EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 }
148
149 while ((mountEntry = getmntent(mountTable))) {
Matt Kraai92ed8a32000-12-06 15:55:23 +0000150 if (!df(mountEntry->mnt_fsname, mountEntry->mnt_dir))
151 status = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 }
153 endmntent(mountTable);
Eric Andersenc4996011999-10-20 22:08:37 +0000154 }
155
Matt Kraai92ed8a32000-12-06 15:55:23 +0000156 return status;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000157}
Erik Andersen029011b2000-03-04 21:19:32 +0000158
159/*
160Local Variables:
161c-file-style: "linux"
162c-basic-offset: 4
163tab-width: 4
164End:
165*/