blob: 6a81086cb19f00439d6f34ae0bbe15d5dc899277 [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
Eric Andersenf429bac2001-06-13 08:02:45 +000035static unsigned long df_disp_hr = 1;
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 Andersencc8ed391999-10-05 16:24:54 +000043
Mark Whitleyae5612c2001-03-07 17:42:07 +000044 if (statfs(mount_point, &s) != 0) {
45 perror_msg("%s", mount_point);
Erik Andersene49d5ec2000-02-08 19:58:47 +000046 return FALSE;
Eric Andersen29d2e361999-11-06 06:07:27 +000047 }
Eric Andersencc8ed391999-10-05 16:24:54 +000048
Erik Andersene49d5ec2000-02-08 19:58:47 +000049 if (s.f_blocks > 0) {
50 blocks_used = s.f_blocks - s.f_bfree;
Eric Andersen9e370072001-02-20 21:52:49 +000051 if(blocks_used == 0)
Eric Andersene1321192001-01-22 22:50:01 +000052 blocks_percent_used = 0;
Eric Andersen9e370072001-02-20 21:52:49 +000053 else {
Richard June6d0921c2001-01-22 22:35:38 +000054 blocks_percent_used = (long)
55 (blocks_used * 100.0 / (blocks_used + s.f_bavail) + 0.5);
Eric Andersen9e370072001-02-20 21:52:49 +000056 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000057 if (strcmp(device, "/dev/root") == 0) {
Erik Andersenec5bd902000-03-22 07:12:05 +000058 /* Adjusts device to be the real root device,
59 * or leaves device alone if it can't find it */
Eric Andersenc911a432001-05-15 17:42:16 +000060 device = find_real_root_device_name(device);
61 if(device==NULL)
62 return FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 }
Mark Whitleyae5612c2001-03-07 17:42:07 +000064#ifdef BB_FEATURE_HUMAN_READABLE
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000065 printf("%-20s %9s ", device,
Eric Andersenf429bac2001-06-13 08:02:45 +000066 make_human_readable_str(s.f_blocks, s.f_bsize/KILOBYTE, df_disp_hr));
67
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000068 printf("%9s ",
Eric Andersenf429bac2001-06-13 08:02:45 +000069 make_human_readable_str( (s.f_blocks - s.f_bfree), s.f_bsize/KILOBYTE, df_disp_hr));
70
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000071 printf("%9s %3ld%% %s\n",
Eric Andersenf429bac2001-06-13 08:02:45 +000072 make_human_readable_str(s.f_bavail, s.f_bsize/KILOBYTE, df_disp_hr),
73 blocks_percent_used, mount_point);
Richard June6d0921c2001-01-22 22:35:38 +000074#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000076 device,
Eric Andersend9216842001-03-09 22:42:26 +000077 (long) (s.f_blocks * (s.f_bsize / (double)KILOBYTE)),
78 (long) ((s.f_blocks - s.f_bfree)*(s.f_bsize/(double)KILOBYTE)),
79 (long) (s.f_bavail * (s.f_bsize / (double)KILOBYTE)),
Mark Whitleyae5612c2001-03-07 17:42:07 +000080 blocks_percent_used, mount_point);
Eric Andersen9e370072001-02-20 21:52:49 +000081#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000082 }
83
84 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +000085}
86
Eric Andersenc4996011999-10-20 22:08:37 +000087extern int df_main(int argc, char **argv)
88{
Matt Kraai92ed8a32000-12-06 15:55:23 +000089 int status = EXIT_SUCCESS;
Richard June6d0921c2001-01-22 22:35:38 +000090 int opt = 0;
91 int i = 0;
Mark Whitleyae5612c2001-03-07 17:42:07 +000092 char disp_units_hdr[80] = "1k-blocks"; /* default display is kilobytes */
Richard June6d0921c2001-01-22 22:35:38 +000093
Mark Whitleyae5612c2001-03-07 17:42:07 +000094 while ((opt = getopt(argc, argv, "k"
Richard June6d0921c2001-01-22 22:35:38 +000095#ifdef BB_FEATURE_HUMAN_READABLE
96 "hm"
97#endif
Richard June6d0921c2001-01-22 22:35:38 +000098)) > 0)
99 {
100 switch (opt) {
101#ifdef BB_FEATURE_HUMAN_READABLE
Mark Whitleyae5612c2001-03-07 17:42:07 +0000102 case 'h':
103 df_disp_hr = 0;
104 strcpy(disp_units_hdr, " Size");
105 break;
106 case 'm':
Eric Andersenf429bac2001-06-13 08:02:45 +0000107 df_disp_hr = KILOBYTE;
Mark Whitleyae5612c2001-03-07 17:42:07 +0000108 strcpy(disp_units_hdr, "1M-blocks");
109 break;
Richard June6d0921c2001-01-22 22:35:38 +0000110#endif
Mark Whitleyae5612c2001-03-07 17:42:07 +0000111 case 'k':
112 /* default display is kilobytes */
113 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
Mark Whitleyae5612c2001-03-07 17:42:07 +0000119 printf("%-20s %-14s %s %s %s %s\n", "Filesystem", disp_units_hdr,
Richard June6d0921c2001-01-22 22:35:38 +0000120 "Used", "Available", "Use%", "Mounted on");
Eric Andersenc4996011999-10-20 22:08:37 +0000121
Richard June6d0921c2001-01-22 22:35:38 +0000122 if(optind < argc) {
Mark Whitleyae5612c2001-03-07 17:42:07 +0000123 struct mntent *mount_entry;
Richard June6d0921c2001-01-22 22:35:38 +0000124 for(i = optind; i < argc; i++)
125 {
Mark Whitleyae5612c2001-03-07 17:42:07 +0000126 if ((mount_entry = find_mount_point(argv[i], mtab_file)) == 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000127 error_msg("%s: can't find mount point.", argv[i]);
Matt Kraai92ed8a32000-12-06 15:55:23 +0000128 status = EXIT_FAILURE;
Mark Whitleyae5612c2001-03-07 17:42:07 +0000129 } else if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir))
Matt Kraai92ed8a32000-12-06 15:55:23 +0000130 status = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 } else {
Mark Whitleyae5612c2001-03-07 17:42:07 +0000133 FILE *mount_table;
134 struct mntent *mount_entry;
Eric Andersenc4996011999-10-20 22:08:37 +0000135
Mark Whitleyae5612c2001-03-07 17:42:07 +0000136 mount_table = setmntent(mtab_file, "r");
137 if (mount_table == 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000138 perror_msg("%s", mtab_file);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000139 return EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 }
141
Mark Whitleyae5612c2001-03-07 17:42:07 +0000142 while ((mount_entry = getmntent(mount_table))) {
143 if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir))
Matt Kraai92ed8a32000-12-06 15:55:23 +0000144 status = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000145 }
Mark Whitleyae5612c2001-03-07 17:42:07 +0000146 endmntent(mount_table);
Eric Andersenc4996011999-10-20 22:08:37 +0000147 }
148
Matt Kraai92ed8a32000-12-06 15:55:23 +0000149 return status;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000150}
Erik Andersen029011b2000-03-04 21:19:32 +0000151
152/*
153Local Variables:
154c-file-style: "linux"
155c-basic-offset: 4
156tab-width: 4
157End:
158*/