Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini df implementation for busybox |
| 4 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen |
| 6 | * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 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 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | /* BB_AUDIT SUSv3 _NOT_ compliant -- options -P and -t missing. Also blocksize. */ |
| 26 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */ |
| 27 | |
| 28 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 29 | * |
| 30 | * Size reduction. Removed floating point dependency. Added error checking |
| 31 | * on output. Output stats on 0-sized filesystems if specificly listed on |
| 32 | * the command line. Properly round *-blocks, Used, and Available quantities. |
| 33 | */ |
| 34 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 36 | #include <stdlib.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 37 | #include <string.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | #include <unistd.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | #include <mntent.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 40 | #include <sys/vfs.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 41 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 42 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 43 | #ifndef CONFIG_FEATURE_HUMAN_READABLE |
| 44 | static long kscale(long b, long bs) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 45 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | return ( b * (long long) bs + KILOBYTE/2 ) / KILOBYTE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 47 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 48 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 49 | |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 50 | extern int df_main(int argc, char **argv) |
| 51 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 52 | long blocks_used; |
| 53 | long blocks_percent_used; |
| 54 | #ifdef CONFIG_FEATURE_HUMAN_READABLE |
| 55 | unsigned long df_disp_hr = KILOBYTE; |
| 56 | #endif |
Matt Kraai | 92ed8a3 | 2000-12-06 15:55:23 +0000 | [diff] [blame] | 57 | int status = EXIT_SUCCESS; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 58 | unsigned long opt; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 59 | FILE *mount_table; |
| 60 | struct mntent *mount_entry; |
| 61 | struct statfs s; |
Manuel Novoa III | 4eff181 | 2003-03-19 09:42:02 +0000 | [diff] [blame] | 62 | static const char hdr_1k[] = "1k-blocks"; /* default display is kilobytes */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | const char *disp_units_hdr = hdr_1k; |
Richard June | 6d0921c | 2001-01-22 22:35:38 +0000 | [diff] [blame] | 64 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 65 | #ifdef CONFIG_FEATURE_HUMAN_READABLE |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 66 | bb_opt_complementaly = "h-km:k-hm:m-hk"; |
| 67 | opt = bb_getopt_ulflags(argc, argv, "hmk"); |
| 68 | if(opt & 1) { |
Mark Whitley | ae5612c | 2001-03-07 17:42:07 +0000 | [diff] [blame] | 69 | df_disp_hr = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | disp_units_hdr = " Size"; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 71 | } |
| 72 | if(opt & 2) { |
Manuel Novoa III | 8f01839 | 2001-06-30 07:48:01 +0000 | [diff] [blame] | 73 | df_disp_hr = MEGABYTE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | disp_units_hdr = "1M-blocks"; |
Richard June | 6d0921c | 2001-01-22 22:35:38 +0000 | [diff] [blame] | 75 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 76 | #else |
| 77 | opt = bb_getopt_ulflags(argc, argv, "k"); |
| 78 | #endif |
Matt Kraai | 92ed8a3 | 2000-12-06 15:55:23 +0000 | [diff] [blame] | 79 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | bb_printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n", |
| 81 | "", disp_units_hdr); |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 82 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | mount_table = NULL; |
| 84 | argv += optind; |
| 85 | if (optind >= argc) { |
| 86 | if (!(mount_table = setmntent(bb_path_mtab_file, "r"))) { |
| 87 | bb_perror_msg_and_die(bb_path_mtab_file); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | } |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 91 | do { |
| 92 | const char *device; |
| 93 | const char *mount_point; |
| 94 | |
| 95 | if (mount_table) { |
| 96 | if (!(mount_entry = getmntent(mount_table))) { |
| 97 | endmntent(mount_table); |
| 98 | break; |
| 99 | } |
| 100 | } else { |
| 101 | if (!(mount_point = *argv++)) { |
| 102 | break; |
| 103 | } |
| 104 | if (!(mount_entry = find_mount_point(mount_point, bb_path_mtab_file))) { |
| 105 | bb_error_msg("%s: can't find mount point.", mount_point); |
| 106 | SET_ERROR: |
| 107 | status = EXIT_FAILURE; |
| 108 | continue; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | device = mount_entry->mnt_fsname; |
| 113 | mount_point = mount_entry->mnt_dir; |
| 114 | |
| 115 | if (statfs(mount_point, &s) != 0) { |
| 116 | bb_perror_msg("%s", mount_point); |
| 117 | goto SET_ERROR; |
| 118 | } |
| 119 | |
| 120 | if ((s.f_blocks > 0) || !mount_table){ |
| 121 | blocks_used = s.f_blocks - s.f_bfree; |
| 122 | blocks_percent_used = 0; |
| 123 | if (blocks_used + s.f_bavail) { |
| 124 | blocks_percent_used = (((long long) blocks_used) * 100 |
| 125 | + (blocks_used + s.f_bavail)/2 |
| 126 | ) / (blocks_used + s.f_bavail); |
| 127 | } |
| 128 | |
Eric Andersen | 9d7f0f0 | 2003-06-20 09:36:49 +0000 | [diff] [blame] | 129 | if (strcmp(device, "rootfs") == 0) { |
| 130 | continue; |
| 131 | } else if (strcmp(device, "/dev/root") == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 132 | /* Adjusts device to be the real root device, |
| 133 | * or leaves device alone if it can't find it */ |
Manuel Novoa III | 3a9b0bf | 2003-03-23 20:27:33 +0000 | [diff] [blame] | 134 | if ((device = find_real_root_device_name(device)) == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 135 | goto SET_ERROR; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | #ifdef CONFIG_FEATURE_HUMAN_READABLE |
| 140 | bb_printf("%-21s%9s ", device, |
| 141 | make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr)); |
| 142 | |
| 143 | bb_printf("%9s ", |
| 144 | make_human_readable_str( (s.f_blocks - s.f_bfree), |
| 145 | s.f_bsize, df_disp_hr)); |
| 146 | |
| 147 | bb_printf("%9s %3ld%% %s\n", |
| 148 | make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr), |
| 149 | blocks_percent_used, mount_point); |
| 150 | #else |
| 151 | bb_printf("%-21s%9ld %9ld %9ld %3ld%% %s\n", |
| 152 | device, |
| 153 | kscale(s.f_blocks, s.f_bsize), |
| 154 | kscale(s.f_blocks-s.f_bfree, s.f_bsize), |
| 155 | kscale(s.f_bavail, s.f_bsize), |
| 156 | blocks_percent_used, mount_point); |
| 157 | #endif |
| 158 | } |
| 159 | |
| 160 | } while (1); |
| 161 | |
| 162 | bb_fflush_stdout_and_exit(status); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 163 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | Local Variables: |
| 167 | c-file-style: "linux" |
| 168 | c-basic-offset: 4 |
| 169 | tab-width: 4 |
| 170 | End: |
| 171 | */ |