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 <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 23 | #include <string.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 24 | #include <unistd.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 25 | #include <mntent.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 26 | #include <sys/vfs.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 27 | #include "busybox.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 28 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 29 | #ifndef CONFIG_FEATURE_HUMAN_READABLE |
| 30 | static long kscale(long b, long bs) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 32 | return ( b * (long long) bs + KILOBYTE/2 ) / KILOBYTE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 33 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 34 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 35 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 36 | int df_main(int argc, char **argv) |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 37 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | long blocks_used; |
| 39 | long blocks_percent_used; |
| 40 | #ifdef CONFIG_FEATURE_HUMAN_READABLE |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 41 | unsigned long df_disp_hr = KILOBYTE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 42 | #endif |
Matt Kraai | 92ed8a3 | 2000-12-06 15:55:23 +0000 | [diff] [blame] | 43 | int status = EXIT_SUCCESS; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 44 | unsigned long opt; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | FILE *mount_table; |
| 46 | struct mntent *mount_entry; |
| 47 | struct statfs s; |
Manuel Novoa III | 4eff181 | 2003-03-19 09:42:02 +0000 | [diff] [blame] | 48 | static const char hdr_1k[] = "1k-blocks"; /* default display is kilobytes */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | const char *disp_units_hdr = hdr_1k; |
Richard June | 6d0921c | 2001-01-22 22:35:38 +0000 | [diff] [blame] | 50 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 51 | #ifdef CONFIG_FEATURE_HUMAN_READABLE |
"Vladimir N. Oleynik" | 27421a1 | 2005-09-05 14:46:07 +0000 | [diff] [blame] | 52 | bb_opt_complementally = "h-km:k-hm:m-hk"; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 53 | opt = bb_getopt_ulflags(argc, argv, "hmk"); |
| 54 | if(opt & 1) { |
Mark Whitley | ae5612c | 2001-03-07 17:42:07 +0000 | [diff] [blame] | 55 | df_disp_hr = 0; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | disp_units_hdr = " Size"; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 57 | } |
| 58 | if(opt & 2) { |
Manuel Novoa III | 8f01839 | 2001-06-30 07:48:01 +0000 | [diff] [blame] | 59 | df_disp_hr = MEGABYTE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | disp_units_hdr = "1M-blocks"; |
Richard June | 6d0921c | 2001-01-22 22:35:38 +0000 | [diff] [blame] | 61 | } |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 62 | #else |
| 63 | opt = bb_getopt_ulflags(argc, argv, "k"); |
| 64 | #endif |
Matt Kraai | 92ed8a3 | 2000-12-06 15:55:23 +0000 | [diff] [blame] | 65 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 66 | bb_printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n", |
| 67 | "", disp_units_hdr); |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 68 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 69 | mount_table = NULL; |
| 70 | argv += optind; |
| 71 | if (optind >= argc) { |
| 72 | if (!(mount_table = setmntent(bb_path_mtab_file, "r"))) { |
| 73 | bb_perror_msg_and_die(bb_path_mtab_file); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | } |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 77 | do { |
| 78 | const char *device; |
| 79 | const char *mount_point; |
| 80 | |
| 81 | if (mount_table) { |
| 82 | if (!(mount_entry = getmntent(mount_table))) { |
| 83 | endmntent(mount_table); |
| 84 | break; |
| 85 | } |
| 86 | } else { |
| 87 | if (!(mount_point = *argv++)) { |
| 88 | break; |
| 89 | } |
| 90 | if (!(mount_entry = find_mount_point(mount_point, bb_path_mtab_file))) { |
| 91 | bb_error_msg("%s: can't find mount point.", mount_point); |
| 92 | SET_ERROR: |
| 93 | status = EXIT_FAILURE; |
| 94 | continue; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | device = mount_entry->mnt_fsname; |
| 99 | mount_point = mount_entry->mnt_dir; |
| 100 | |
| 101 | if (statfs(mount_point, &s) != 0) { |
| 102 | bb_perror_msg("%s", mount_point); |
| 103 | goto SET_ERROR; |
| 104 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 105 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 106 | if ((s.f_blocks > 0) || !mount_table){ |
| 107 | blocks_used = s.f_blocks - s.f_bfree; |
| 108 | blocks_percent_used = 0; |
| 109 | if (blocks_used + s.f_bavail) { |
| 110 | blocks_percent_used = (((long long) blocks_used) * 100 |
| 111 | + (blocks_used + s.f_bavail)/2 |
| 112 | ) / (blocks_used + s.f_bavail); |
| 113 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 114 | |
Eric Andersen | 9d7f0f0 | 2003-06-20 09:36:49 +0000 | [diff] [blame] | 115 | if (strcmp(device, "rootfs") == 0) { |
| 116 | continue; |
| 117 | } else if (strcmp(device, "/dev/root") == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 118 | /* Adjusts device to be the real root device, |
| 119 | * or leaves device alone if it can't find it */ |
Rob Landley | 6a6798b | 2005-08-10 20:35:54 +0000 | [diff] [blame] | 120 | if ((device = find_block_device("/")) == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 121 | goto SET_ERROR; |
| 122 | } |
| 123 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 124 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 125 | #ifdef CONFIG_FEATURE_HUMAN_READABLE |
Rob Landley | 98ea849 | 2005-09-10 02:59:35 +0000 | [diff] [blame] | 126 | bb_printf("%-20s %9s ", device, |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 127 | 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] | 128 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 129 | bb_printf("%9s ", |
| 130 | make_human_readable_str( (s.f_blocks - s.f_bfree), |
| 131 | s.f_bsize, df_disp_hr)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 132 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 133 | bb_printf("%9s %3ld%% %s\n", |
| 134 | make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr), |
| 135 | blocks_percent_used, mount_point); |
| 136 | #else |
Rob Landley | 98ea849 | 2005-09-10 02:59:35 +0000 | [diff] [blame] | 137 | bb_printf("%-20s %9ld %9ld %9ld %3ld%% %s\n", |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 138 | device, |
| 139 | kscale(s.f_blocks, s.f_bsize), |
| 140 | kscale(s.f_blocks-s.f_bfree, s.f_bsize), |
| 141 | kscale(s.f_bavail, s.f_bsize), |
| 142 | blocks_percent_used, mount_point); |
| 143 | #endif |
| 144 | } |
| 145 | |
| 146 | } while (1); |
| 147 | |
| 148 | bb_fflush_stdout_and_exit(status); |
Eric Andersen | c6cb79d | 1999-10-13 18:01:10 +0000 | [diff] [blame] | 149 | } |