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 | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 6 | * based on original code by (I think) Bruce Perens <bruce@pixar.com>. |
| 7 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 11 | /* BB_AUDIT SUSv3 _NOT_ compliant -- options -P and -t missing. Also blocksize. */ |
| 12 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */ |
| 13 | |
| 14 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 15 | * |
| 16 | * Size reduction. Removed floating point dependency. Added error checking |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 17 | * on output. Output stats on 0-sized filesystems if specifically listed on |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 18 | * the command line. Properly round *-blocks, Used, and Available quantities. |
| 19 | */ |
| 20 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 21 | #include <mntent.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 22 | #include <sys/vfs.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 23 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 25 | #if !ENABLE_FEATURE_HUMAN_READABLE |
Denis Vlasenko | 08294db | 2007-08-13 12:27:49 +0000 | [diff] [blame] | 26 | static unsigned long kscale(unsigned long b, unsigned long bs) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 27 | { |
Denis Vlasenko | 08294db | 2007-08-13 12:27:49 +0000 | [diff] [blame] | 28 | return (b * (unsigned long long) bs + 1024/2) / 1024; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 29 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 30 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame^] | 32 | int df_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 33 | int df_main(int argc, char **argv) |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 34 | { |
Denis Vlasenko | 08294db | 2007-08-13 12:27:49 +0000 | [diff] [blame] | 35 | unsigned long blocks_used; |
| 36 | unsigned blocks_percent_used; |
| 37 | #if ENABLE_FEATURE_HUMAN_READABLE |
| 38 | unsigned df_disp_hr = 1024; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 39 | #endif |
Matt Kraai | 92ed8a3 | 2000-12-06 15:55:23 +0000 | [diff] [blame] | 40 | int status = EXIT_SUCCESS; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 41 | unsigned opt; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 42 | FILE *mount_table; |
| 43 | struct mntent *mount_entry; |
| 44 | struct statfs s; |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 45 | /* default display is kilobytes */ |
Denis Vlasenko | 08294db | 2007-08-13 12:27:49 +0000 | [diff] [blame] | 46 | const char *disp_units_hdr = "1k-blocks"; |
Richard June | 6d0921c | 2001-01-22 22:35:38 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | 08294db | 2007-08-13 12:27:49 +0000 | [diff] [blame] | 48 | #if ENABLE_FEATURE_HUMAN_READABLE |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 49 | opt_complementary = "h-km:k-hm:m-hk"; |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 50 | opt = getopt32(argv, "hmk"); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 51 | if (opt & 1) { |
| 52 | df_disp_hr = 0; |
| 53 | disp_units_hdr = " Size"; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 54 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 55 | if (opt & 2) { |
Denis Vlasenko | dca0b70 | 2006-10-27 09:05:02 +0000 | [diff] [blame] | 56 | df_disp_hr = 1024*1024; |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 57 | disp_units_hdr = "1M-blocks"; |
Richard June | 6d0921c | 2001-01-22 22:35:38 +0000 | [diff] [blame] | 58 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 59 | #else |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 60 | opt = getopt32(argv, "k"); |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 61 | #endif |
Matt Kraai | 92ed8a3 | 2000-12-06 15:55:23 +0000 | [diff] [blame] | 62 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 63 | printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n", |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 64 | "", disp_units_hdr); |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 65 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 66 | mount_table = NULL; |
| 67 | argv += optind; |
| 68 | if (optind >= argc) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 69 | mount_table = setmntent(bb_path_mtab_file, "r"); |
| 70 | if (!mount_table) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 71 | bb_perror_msg_and_die(bb_path_mtab_file); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | } |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Denis Vlasenko | 08294db | 2007-08-13 12:27:49 +0000 | [diff] [blame] | 75 | while (1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 76 | const char *device; |
| 77 | const char *mount_point; |
| 78 | |
| 79 | if (mount_table) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 80 | mount_entry = getmntent(mount_table); |
| 81 | if (!mount_entry) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 82 | endmntent(mount_table); |
| 83 | break; |
| 84 | } |
| 85 | } else { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 86 | mount_point = *argv++; |
| 87 | if (!mount_point) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 88 | break; |
| 89 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 90 | mount_entry = find_mount_point(mount_point, bb_path_mtab_file); |
| 91 | if (!mount_entry) { |
| 92 | bb_error_msg("%s: can't find mount point", mount_point); |
Denis Vlasenko | 98ee06d | 2006-12-31 18:57:37 +0000 | [diff] [blame] | 93 | SET_ERROR: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 94 | status = EXIT_FAILURE; |
| 95 | continue; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | device = mount_entry->mnt_fsname; |
| 100 | mount_point = mount_entry->mnt_dir; |
| 101 | |
| 102 | if (statfs(mount_point, &s) != 0) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 103 | bb_simple_perror_msg(mount_point); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 104 | goto SET_ERROR; |
| 105 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 106 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 107 | if ((s.f_blocks > 0) || !mount_table){ |
| 108 | blocks_used = s.f_blocks - s.f_bfree; |
| 109 | blocks_percent_used = 0; |
| 110 | if (blocks_used + s.f_bavail) { |
Denis Vlasenko | 08294db | 2007-08-13 12:27:49 +0000 | [diff] [blame] | 111 | blocks_percent_used = (blocks_used * 100ULL |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 112 | + (blocks_used + s.f_bavail)/2 |
| 113 | ) / (blocks_used + s.f_bavail); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 114 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 115 | |
Eric Andersen | 9d7f0f0 | 2003-06-20 09:36:49 +0000 | [diff] [blame] | 116 | if (strcmp(device, "rootfs") == 0) { |
| 117 | continue; |
| 118 | } else if (strcmp(device, "/dev/root") == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 119 | /* Adjusts device to be the real root device, |
| 120 | * or leaves device alone if it can't find it */ |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 121 | device = find_block_device("/"); |
| 122 | if (!device) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 123 | goto SET_ERROR; |
| 124 | } |
| 125 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 126 | |
Denis Vlasenko | 08294db | 2007-08-13 12:27:49 +0000 | [diff] [blame] | 127 | if (printf("\n%-20s" + 1, device) > 20) |
| 128 | printf("\n%-20s", ""); |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 129 | #if ENABLE_FEATURE_HUMAN_READABLE |
Denis Vlasenko | 08294db | 2007-08-13 12:27:49 +0000 | [diff] [blame] | 130 | printf(" %9s ", |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 131 | make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 132 | |
Denis Vlasenko | 08294db | 2007-08-13 12:27:49 +0000 | [diff] [blame] | 133 | printf(" %9s " + 1, |
| 134 | make_human_readable_str((s.f_blocks - s.f_bfree), |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 135 | s.f_bsize, df_disp_hr)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | 08294db | 2007-08-13 12:27:49 +0000 | [diff] [blame] | 137 | printf("%9s %3u%% %s\n", |
| 138 | make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr), |
| 139 | blocks_percent_used, mount_point); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 140 | #else |
Denis Vlasenko | 08294db | 2007-08-13 12:27:49 +0000 | [diff] [blame] | 141 | printf(" %9lu %9lu %9lu %3u%% %s\n", |
| 142 | kscale(s.f_blocks, s.f_bsize), |
| 143 | kscale(s.f_blocks-s.f_bfree, s.f_bsize), |
| 144 | kscale(s.f_bavail, s.f_bsize), |
| 145 | blocks_percent_used, mount_point); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 146 | #endif |
| 147 | } |
Denis Vlasenko | 08294db | 2007-08-13 12:27:49 +0000 | [diff] [blame] | 148 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 149 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 150 | fflush_stdout_and_exit(status); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 151 | } |