blob: 776fceb2801f8b9d3db8aab0bb628f11ae6704a0 [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,
Mark Whitleyae5612c2001-03-07 17:42:07 +000077 make_human_readable_str(s.f_blocks * (s.f_bsize/KILOBYTE), base));
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000078 printf("%9s ",
Mark Whitleyae5612c2001-03-07 17:42:07 +000079 make_human_readable_str((s.f_blocks - s.f_bfree) * (s.f_bsize/KILOBYTE), base));
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000080 printf("%9s %3ld%% %s\n",
Mark Whitleyae5612c2001-03-07 17:42:07 +000081 make_human_readable_str(s.f_bavail * (s.f_bsize/KILOBYTE), base),
82 blocks_percent_used, mount_point);
Richard June6d0921c2001-01-22 22:35:38 +000083#else
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
Eric Andersen7c3e7ac2001-02-21 00:24:51 +000085 device,
86 (long) (s.f_blocks * (s.f_bsize / KILOBYTE)),
87 (long) ((s.f_blocks - s.f_bfree) * (s.f_bsize / KILOBYTE)),
88 (long) (s.f_bavail * (s.f_bsize / KILOBYTE)),
Mark Whitleyae5612c2001-03-07 17:42:07 +000089 blocks_percent_used, mount_point);
Eric Andersen9e370072001-02-20 21:52:49 +000090#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 }
92
93 return TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +000094}
95
Eric Andersenc4996011999-10-20 22:08:37 +000096extern int df_main(int argc, char **argv)
97{
Matt Kraai92ed8a32000-12-06 15:55:23 +000098 int status = EXIT_SUCCESS;
Richard June6d0921c2001-01-22 22:35:38 +000099 int opt = 0;
100 int i = 0;
Mark Whitleyae5612c2001-03-07 17:42:07 +0000101 char disp_units_hdr[80] = "1k-blocks"; /* default display is kilobytes */
Richard June6d0921c2001-01-22 22:35:38 +0000102
Mark Whitleyae5612c2001-03-07 17:42:07 +0000103 while ((opt = getopt(argc, argv, "k"
Richard June6d0921c2001-01-22 22:35:38 +0000104#ifdef BB_FEATURE_HUMAN_READABLE
105 "hm"
106#endif
Richard June6d0921c2001-01-22 22:35:38 +0000107)) > 0)
108 {
109 switch (opt) {
110#ifdef BB_FEATURE_HUMAN_READABLE
Mark Whitleyae5612c2001-03-07 17:42:07 +0000111 case 'h':
112 df_disp_hr = 0;
113 strcpy(disp_units_hdr, " Size");
114 break;
115 case 'm':
116 df_disp_hr = MEGABYTE;
117 strcpy(disp_units_hdr, "1M-blocks");
118 break;
Richard June6d0921c2001-01-22 22:35:38 +0000119#endif
Mark Whitleyae5612c2001-03-07 17:42:07 +0000120 case 'k':
121 /* default display is kilobytes */
122 break;
Eric Andersenb50da532001-02-17 16:52:35 +0000123 default:
124 show_usage();
Richard June6d0921c2001-01-22 22:35:38 +0000125 }
126 }
Matt Kraai92ed8a32000-12-06 15:55:23 +0000127
Mark Whitleyae5612c2001-03-07 17:42:07 +0000128 printf("%-20s %-14s %s %s %s %s\n", "Filesystem", disp_units_hdr,
Richard June6d0921c2001-01-22 22:35:38 +0000129 "Used", "Available", "Use%", "Mounted on");
Eric Andersenc4996011999-10-20 22:08:37 +0000130
Richard June6d0921c2001-01-22 22:35:38 +0000131 if(optind < argc) {
Mark Whitleyae5612c2001-03-07 17:42:07 +0000132 struct mntent *mount_entry;
Richard June6d0921c2001-01-22 22:35:38 +0000133 for(i = optind; i < argc; i++)
134 {
Mark Whitleyae5612c2001-03-07 17:42:07 +0000135 if ((mount_entry = find_mount_point(argv[i], mtab_file)) == 0) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000136 error_msg("%s: can't find mount point.", argv[i]);
Matt Kraai92ed8a32000-12-06 15:55:23 +0000137 status = EXIT_FAILURE;
Mark Whitleyae5612c2001-03-07 17:42:07 +0000138 } else if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir))
Matt Kraai92ed8a32000-12-06 15:55:23 +0000139 status = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000141 } else {
Mark Whitleyae5612c2001-03-07 17:42:07 +0000142 FILE *mount_table;
143 struct mntent *mount_entry;
Eric Andersenc4996011999-10-20 22:08:37 +0000144
Mark Whitleyae5612c2001-03-07 17:42:07 +0000145 mount_table = setmntent(mtab_file, "r");
146 if (mount_table == 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000147 perror_msg("%s", mtab_file);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000148 return EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149 }
150
Mark Whitleyae5612c2001-03-07 17:42:07 +0000151 while ((mount_entry = getmntent(mount_table))) {
152 if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir))
Matt Kraai92ed8a32000-12-06 15:55:23 +0000153 status = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154 }
Mark Whitleyae5612c2001-03-07 17:42:07 +0000155 endmntent(mount_table);
Eric Andersenc4996011999-10-20 22:08:37 +0000156 }
157
Matt Kraai92ed8a32000-12-06 15:55:23 +0000158 return status;
Eric Andersenc6cb79d1999-10-13 18:01:10 +0000159}
Erik Andersen029011b2000-03-04 21:19:32 +0000160
161/*
162Local Variables:
163c-file-style: "linux"
164c-basic-offset: 4
165tab-width: 4
166End:
167*/