Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini du implementation for busybox |
| 4 | * |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000,2001 by Lineo, inc. and John Beppu |
| 6 | * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org> |
Eric Andersen | 265d229 | 2002-04-06 23:16:44 +0000 | [diff] [blame] | 7 | * Copyright (C) 2002 Edward Betts <edward@debian.org> |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 12 | /* BB_AUDIT SUSv3 compliant (unless default blocksize set to 1k) */ |
| 13 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/du.html */ |
| 14 | |
| 15 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 16 | * |
| 17 | * Mostly rewritten for SUSv3 compliance and to fix bugs/defects. |
| 18 | * 1) Added support for SUSv3 -a, -H, -L, gnu -c, and (busybox) -d options. |
| 19 | * The -d option allows setting of max depth (similar to gnu --max-depth). |
| 20 | * 2) Fixed incorrect size calculations for links and directories, especially |
| 21 | * when errors occurred. Calculates sizes should now match gnu du output. |
| 22 | * 3) Added error checking of output. |
| 23 | * 4) Fixed busybox bug #1284 involving long overflow with human_readable. |
| 24 | */ |
| 25 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 26 | //usage:#define du_trivial_usage |
| 27 | //usage: "[-aHLdclsx" IF_FEATURE_HUMAN_READABLE("hm") "k] [FILE]..." |
| 28 | //usage:#define du_full_usage "\n\n" |
Denys Vlasenko | bb9254a | 2012-03-05 09:59:56 +0100 | [diff] [blame] | 29 | //usage: "Summarize disk space used for each FILE and/or directory\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 30 | //usage: "\n -a Show file sizes too" |
| 31 | //usage: "\n -L Follow all symlinks" |
| 32 | //usage: "\n -H Follow symlinks on command line" |
| 33 | //usage: "\n -d N Limit output to directories (and files with -a) of depth < N" |
| 34 | //usage: "\n -c Show grand total" |
| 35 | //usage: "\n -l Count sizes many times if hard linked" |
| 36 | //usage: "\n -s Display only a total for each argument" |
| 37 | //usage: "\n -x Skip directories on different filesystems" |
| 38 | //usage: IF_FEATURE_HUMAN_READABLE( |
Denys Vlasenko | 81b6bf1 | 2012-03-05 09:52:19 +0100 | [diff] [blame] | 39 | //usage: "\n -h Sizes in human readable format (e.g., 1K 243M 2G)" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 40 | //usage: "\n -m Sizes in megabytes" |
| 41 | //usage: ) |
Denys Vlasenko | bb9254a | 2012-03-05 09:59:56 +0100 | [diff] [blame] | 42 | //usage: "\n -k Sizes in kilobytes" IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(" (default)") |
| 43 | //usage: IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K( |
| 44 | //usage: "\n Default unit is 512 bytes" |
| 45 | //usage: ) |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 46 | //usage: |
| 47 | //usage:#define du_example_usage |
| 48 | //usage: "$ du\n" |
| 49 | //usage: "16 ./CVS\n" |
| 50 | //usage: "12 ./kernel-patches/CVS\n" |
| 51 | //usage: "80 ./kernel-patches\n" |
| 52 | //usage: "12 ./tests/CVS\n" |
| 53 | //usage: "36 ./tests\n" |
| 54 | //usage: "12 ./scripts/CVS\n" |
| 55 | //usage: "16 ./scripts\n" |
| 56 | //usage: "12 ./docs/CVS\n" |
| 57 | //usage: "104 ./docs\n" |
| 58 | //usage: "2417 .\n" |
| 59 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 60 | #include "libbb.h" |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 62 | enum { |
| 63 | OPT_a_files_too = (1 << 0), |
| 64 | OPT_H_follow_links = (1 << 1), |
| 65 | OPT_k_kbytes = (1 << 2), |
| 66 | OPT_L_follow_links = (1 << 3), |
| 67 | OPT_s_total_norecurse = (1 << 4), |
| 68 | OPT_x_one_FS = (1 << 5), |
| 69 | OPT_d_maxdepth = (1 << 6), |
| 70 | OPT_l_hardlinks = (1 << 7), |
| 71 | OPT_c_total = (1 << 8), |
| 72 | OPT_h_for_humans = (1 << 9), |
| 73 | OPT_m_mbytes = (1 << 10), |
| 74 | }; |
| 75 | |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 76 | struct globals { |
Denis Vlasenko | f42ff90 | 2006-12-18 21:22:16 +0000 | [diff] [blame] | 77 | #if ENABLE_FEATURE_HUMAN_READABLE |
Denys Vlasenko | 93dd9fd | 2015-10-15 21:33:34 +0200 | [diff] [blame] | 78 | unsigned long disp_unit; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 79 | #else |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 80 | unsigned disp_k; |
Richard June | 6d0921c | 2001-01-22 22:35:38 +0000 | [diff] [blame] | 81 | #endif |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 82 | int max_print_depth; |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 83 | bool status; |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 84 | int slink_depth; |
| 85 | int du_depth; |
| 86 | dev_t dir_dev; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 87 | } FIX_ALIASING; |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 88 | #define G (*(struct globals*)&bb_common_bufsiz1) |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 89 | #define INIT_G() do { } while (0) |
John Beppu | e1618e4 | 1999-12-15 18:52:17 +0000 | [diff] [blame] | 90 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 91 | |
Ian Wienand | 694738f | 2012-02-28 03:10:31 +0100 | [diff] [blame] | 92 | static void print(unsigned long long size, const char *filename) |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 93 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 94 | /* TODO - May not want to defer error checking here. */ |
Denis Vlasenko | f42ff90 | 2006-12-18 21:22:16 +0000 | [diff] [blame] | 95 | #if ENABLE_FEATURE_HUMAN_READABLE |
Denys Vlasenko | 93dd9fd | 2015-10-15 21:33:34 +0200 | [diff] [blame] | 96 | # if ENABLE_DESKTOP |
| 97 | /* ~30 bytes of code for extra comtat: |
| 98 | * coreutils' du rounds sizes up: |
| 99 | * for example, 1025k file is shown as "2" by du -m. |
| 100 | * We round to nearest if human-readable [too hard to fix], |
| 101 | * else (fixed scale such as -m), we round up. To that end, |
| 102 | * add yet another half of the unit before displaying: |
| 103 | */ |
| 104 | if (G.disp_unit) |
| 105 | size += (G.disp_unit-1) / (unsigned)(512 * 2); |
| 106 | # endif |
Denys Vlasenko | 0bf44d0 | 2009-10-13 01:25:09 +0200 | [diff] [blame] | 107 | printf("%s\t%s\n", |
Denys Vlasenko | 93dd9fd | 2015-10-15 21:33:34 +0200 | [diff] [blame] | 108 | /* size x 512 / G.disp_unit. |
| 109 | * If G.disp_unit == 0, show one fractional |
| 110 | * and use suffixes |
| 111 | */ |
| 112 | make_human_readable_str(size, 512, G.disp_unit), |
Denis Vlasenko | f42ff90 | 2006-12-18 21:22:16 +0000 | [diff] [blame] | 113 | filename); |
Richard June | 6d0921c | 2001-01-22 22:35:38 +0000 | [diff] [blame] | 114 | #else |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 115 | if (G.disp_k) { |
Glenn L McGrath | c66ebe4 | 2004-03-10 09:58:51 +0000 | [diff] [blame] | 116 | size++; |
| 117 | size >>= 1; |
| 118 | } |
Ian Wienand | 694738f | 2012-02-28 03:10:31 +0100 | [diff] [blame] | 119 | printf("%llu\t%s\n", size, filename); |
Richard June | 6d0921c | 2001-01-22 22:35:38 +0000 | [diff] [blame] | 120 | #endif |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* tiny recursive du */ |
Ian Wienand | 694738f | 2012-02-28 03:10:31 +0100 | [diff] [blame] | 124 | static unsigned long long du(const char *filename) |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 125 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 126 | struct stat statbuf; |
Ian Wienand | 694738f | 2012-02-28 03:10:31 +0100 | [diff] [blame] | 127 | unsigned long long sum; |
John Beppu | 14c82b6 | 1999-12-10 06:15:27 +0000 | [diff] [blame] | 128 | |
Denis Vlasenko | f42ff90 | 2006-12-18 21:22:16 +0000 | [diff] [blame] | 129 | if (lstat(filename, &statbuf) != 0) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 130 | bb_simple_perror_msg(filename); |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 131 | G.status = EXIT_FAILURE; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 132 | return 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 135 | if (option_mask32 & OPT_x_one_FS) { |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 136 | if (G.du_depth == 0) { |
| 137 | G.dir_dev = statbuf.st_dev; |
| 138 | } else if (G.dir_dev != statbuf.st_dev) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 139 | return 0; |
Eric Andersen | 8fa1bf7 | 2001-06-30 17:54:20 +0000 | [diff] [blame] | 140 | } |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 141 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 142 | |
| 143 | sum = statbuf.st_blocks; |
| 144 | |
| 145 | if (S_ISLNK(statbuf.st_mode)) { |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 146 | if (G.slink_depth > G.du_depth) { /* -H or -L */ |
Denis Vlasenko | f42ff90 | 2006-12-18 21:22:16 +0000 | [diff] [blame] | 147 | if (stat(filename, &statbuf) != 0) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 148 | bb_simple_perror_msg(filename); |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 149 | G.status = EXIT_FAILURE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | sum = statbuf.st_blocks; |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 153 | if (G.slink_depth == 1) { |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 154 | /* Convert -H to -L */ |
| 155 | G.slink_depth = INT_MAX; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 160 | if (!(option_mask32 & OPT_l_hardlinks) |
| 161 | && statbuf.st_nlink > 1 |
| 162 | ) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 163 | /* Add files/directories with links only once */ |
Denis Vlasenko | e1e93c1 | 2007-03-14 22:06:57 +0000 | [diff] [blame] | 164 | if (is_in_ino_dev_hashtable(&statbuf)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 165 | return 0; |
| 166 | } |
| 167 | add_to_ino_dev_hashtable(&statbuf, NULL); |
| 168 | } |
| 169 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 170 | if (S_ISDIR(statbuf.st_mode)) { |
| 171 | DIR *dir; |
| 172 | struct dirent *entry; |
Eric Andersen | 04b0354 | 2001-05-07 22:49:43 +0000 | [diff] [blame] | 173 | char *newfile; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 174 | |
Rob Landley | 86b4d64 | 2006-08-03 17:58:17 +0000 | [diff] [blame] | 175 | dir = warn_opendir(filename); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 176 | if (!dir) { |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 177 | G.status = EXIT_FAILURE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 178 | return sum; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 179 | } |
Erik Andersen | 42387e4 | 2000-02-21 17:27:17 +0000 | [diff] [blame] | 180 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 181 | while ((entry = readdir(dir))) { |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 182 | newfile = concat_subpath_file(filename, entry->d_name); |
Denis Vlasenko | f42ff90 | 2006-12-18 21:22:16 +0000 | [diff] [blame] | 183 | if (newfile == NULL) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 184 | continue; |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 185 | ++G.du_depth; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 186 | sum += du(newfile); |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 187 | --G.du_depth; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 188 | free(newfile); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 189 | } |
| 190 | closedir(dir); |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 191 | } else { |
| 192 | if (!(option_mask32 & OPT_a_files_too) && G.du_depth != 0) |
| 193 | return sum; |
Erik Andersen | 27fdd08 | 2000-02-19 18:16:49 +0000 | [diff] [blame] | 194 | } |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 195 | if (G.du_depth <= G.max_print_depth) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 196 | print(sum, filename); |
| 197 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 198 | return sum; |
John Beppu | 0f5e1ab | 1999-12-09 18:23:54 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 201 | int du_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 202 | int du_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 203 | { |
Ian Wienand | 694738f | 2012-02-28 03:10:31 +0100 | [diff] [blame] | 204 | unsigned long long total; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 205 | int slink_depth_save; |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 206 | unsigned opt; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 207 | |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 208 | INIT_G(); |
| 209 | |
Denis Vlasenko | f42ff90 | 2006-12-18 21:22:16 +0000 | [diff] [blame] | 210 | #if ENABLE_FEATURE_HUMAN_READABLE |
Denys Vlasenko | 93dd9fd | 2015-10-15 21:33:34 +0200 | [diff] [blame] | 211 | IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 1024;) |
| 212 | IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 512;) |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 213 | if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */ |
Denys Vlasenko | 93dd9fd | 2015-10-15 21:33:34 +0200 | [diff] [blame] | 214 | G.disp_unit = 512; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 215 | #else |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 216 | IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;) |
| 217 | /* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 218 | #endif |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 219 | G.max_print_depth = INT_MAX; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 220 | |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 221 | /* Note: SUSv3 specifies that -a and -s options cannot be used together |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 222 | * in strictly conforming applications. However, it also says that some |
| 223 | * du implementations may produce output when -a and -s are used together. |
| 224 | * gnu du exits with an error code in this case. We choose to simply |
| 225 | * ignore -a. This is consistent with -s being equivalent to -d 0. |
| 226 | */ |
Denis Vlasenko | f42ff90 | 2006-12-18 21:22:16 +0000 | [diff] [blame] | 227 | #if ENABLE_FEATURE_HUMAN_READABLE |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 228 | opt_complementary = "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s:d+"; |
| 229 | opt = getopt32(argv, "aHkLsx" "d:" "lc" "hm", &G.max_print_depth); |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 230 | argv += optind; |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 231 | if (opt & OPT_h_for_humans) { |
Denys Vlasenko | 93dd9fd | 2015-10-15 21:33:34 +0200 | [diff] [blame] | 232 | G.disp_unit = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 233 | } |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 234 | if (opt & OPT_m_mbytes) { |
Denys Vlasenko | 93dd9fd | 2015-10-15 21:33:34 +0200 | [diff] [blame] | 235 | G.disp_unit = 1024*1024; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 236 | } |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 237 | if (opt & OPT_k_kbytes) { |
Denys Vlasenko | 93dd9fd | 2015-10-15 21:33:34 +0200 | [diff] [blame] | 238 | G.disp_unit = 1024; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 239 | } |
| 240 | #else |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 241 | opt_complementary = "H-L:L-H:s-d:d-s:d+"; |
| 242 | opt = getopt32(argv, "aHkLsx" "d:" "lc", &G.max_print_depth); |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 243 | argv += optind; |
Denis Vlasenko | f42ff90 | 2006-12-18 21:22:16 +0000 | [diff] [blame] | 244 | #if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 245 | if (opt & OPT_k_kbytes) { |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 246 | G.disp_k = 1; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 247 | } |
| 248 | #endif |
| 249 | #endif |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 250 | if (opt & OPT_H_follow_links) { |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 251 | G.slink_depth = 1; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 252 | } |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 253 | if (opt & OPT_L_follow_links) { |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 254 | G.slink_depth = INT_MAX; |
Eric Andersen | 8876fb2 | 2003-06-20 09:01:58 +0000 | [diff] [blame] | 255 | } |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 256 | if (opt & OPT_s_total_norecurse) { |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 257 | G.max_print_depth = 0; |
Denis Vlasenko | f42ff90 | 2006-12-18 21:22:16 +0000 | [diff] [blame] | 258 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 259 | |
| 260 | /* go through remaining args (if any) */ |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 261 | if (!*argv) { |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 262 | *--argv = (char*)"."; |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 263 | if (G.slink_depth == 1) { |
| 264 | G.slink_depth = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 268 | slink_depth_save = G.slink_depth; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 269 | total = 0; |
| 270 | do { |
| 271 | total += du(*argv); |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 272 | /* otherwise du /dir /dir won't show /dir twice: */ |
| 273 | reset_ino_dev_hashtable(); |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 274 | G.slink_depth = slink_depth_save; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 275 | } while (*++argv); |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 276 | |
Denis Vlasenko | 618a302 | 2008-11-11 21:15:56 +0000 | [diff] [blame] | 277 | if (opt & OPT_c_total) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 278 | print(total, "total"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 279 | |
Denis Vlasenko | 21b83cf | 2007-09-03 20:05:58 +0000 | [diff] [blame] | 280 | fflush_stdout_and_exit(G.status); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 281 | } |