aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2021-06-16 17:28:02 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2021-06-16 17:45:28 +0200
commite7a8e8e30c31977476ab3be2fadb38c2bcb92482 (patch)
tree8df62a0d5c6eacc4a1c498d06e967b3133dc975b
parent46d315ae445b3653f6e8a49003011c5bcf0eed9d (diff)
downloadbusybox-w32-e7a8e8e30c31977476ab3be2fadb38c2bcb92482.tar.gz
busybox-w32-e7a8e8e30c31977476ab3be2fadb38c2bcb92482.tar.bz2
busybox-w32-e7a8e8e30c31977476ab3be2fadb38c2bcb92482.zip
du: support -b "apparent size"
function old new delta du 434 470 +36 packed_usage 33542 33570 +28 print 57 78 +21 du_main 286 302 +16 .rodata 103187 103188 +1 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 5/0 up/down: 102/0) Total: 102 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/du.c33
1 files changed, 21 insertions, 12 deletions
diff --git a/coreutils/du.c b/coreutils/du.c
index c8aedb6ef..832dd7594 100644
--- a/coreutils/du.c
+++ b/coreutils/du.c
@@ -42,6 +42,7 @@
42//usage:#define du_full_usage "\n\n" 42//usage:#define du_full_usage "\n\n"
43//usage: "Summarize disk space used for FILEs (or directories)\n" 43//usage: "Summarize disk space used for FILEs (or directories)\n"
44//usage: "\n -a Show file sizes too" 44//usage: "\n -a Show file sizes too"
45//usage: "\n -b Apparent size (including holes)"
45//usage: "\n -L Follow all symlinks" 46//usage: "\n -L Follow all symlinks"
46//usage: "\n -H Follow symlinks on command line" 47//usage: "\n -H Follow symlinks on command line"
47//usage: "\n -d N Limit output to directories (and files with -a) of depth < N" 48//usage: "\n -d N Limit output to directories (and files with -a) of depth < N"
@@ -84,8 +85,9 @@ enum {
84 OPT_d_maxdepth = (1 << 6), 85 OPT_d_maxdepth = (1 << 6),
85 OPT_l_hardlinks = (1 << 7), 86 OPT_l_hardlinks = (1 << 7),
86 OPT_c_total = (1 << 8), 87 OPT_c_total = (1 << 8),
87 OPT_h_for_humans = (1 << 9), 88 OPT_b = (1 << 9),
88 OPT_m_mbytes = (1 << 10), 89 OPT_h_for_humans = (1 << 10),
90 OPT_m_mbytes = (1 << 11),
89}; 91};
90 92
91struct globals { 93struct globals {
@@ -109,7 +111,7 @@ static void print(unsigned long long size, const char *filename)
109 /* TODO - May not want to defer error checking here. */ 111 /* TODO - May not want to defer error checking here. */
110#if ENABLE_FEATURE_HUMAN_READABLE 112#if ENABLE_FEATURE_HUMAN_READABLE
111# if ENABLE_DESKTOP 113# if ENABLE_DESKTOP
112 /* ~30 bytes of code for extra comtat: 114 /* ~30 bytes of code for extra compat:
113 * coreutils' du rounds sizes up: 115 * coreutils' du rounds sizes up:
114 * for example, 1025k file is shown as "2" by du -m. 116 * for example, 1025k file is shown as "2" by du -m.
115 * We round to nearest if human-readable [too hard to fix], 117 * We round to nearest if human-readable [too hard to fix],
@@ -124,12 +126,16 @@ static void print(unsigned long long size, const char *filename)
124 * If G.disp_unit == 0, show one fractional 126 * If G.disp_unit == 0, show one fractional
125 * and use suffixes 127 * and use suffixes
126 */ 128 */
127 make_human_readable_str(size, 512, G.disp_unit), 129 make_human_readable_str(size, (option_mask32 & OPT_b) ? 1 : 512, G.disp_unit),
128 filename); 130 filename);
129#else 131#else
130 if (G.disp_k) { 132 if (G.disp_k) {
131 size++; 133 if (!(option_mask32 & OPT_b)) {
132 size >>= 1; 134 size++;
135 size >>= 1;
136 } else {
137 size >>= 10;
138 }
133 } 139 }
134 printf("%llu\t%s\n", size, filename); 140 printf("%llu\t%s\n", size, filename);
135#endif 141#endif
@@ -155,7 +161,7 @@ static unsigned long long du(const char *filename)
155 } 161 }
156 } 162 }
157 163
158 sum = statbuf.st_blocks; 164 sum = ((option_mask32 & OPT_b) ? statbuf.st_size : statbuf.st_blocks);
159 165
160 if (S_ISLNK(statbuf.st_mode)) { 166 if (S_ISLNK(statbuf.st_mode)) {
161 if (G.slink_depth > G.du_depth) { /* -H or -L */ 167 if (G.slink_depth > G.du_depth) { /* -H or -L */
@@ -164,7 +170,7 @@ static unsigned long long du(const char *filename)
164 G.status = EXIT_FAILURE; 170 G.status = EXIT_FAILURE;
165 return 0; 171 return 0;
166 } 172 }
167 sum = statbuf.st_blocks; 173 sum = ((option_mask32 & OPT_b) ? statbuf.st_size : statbuf.st_blocks);
168 if (G.slink_depth == 1) { 174 if (G.slink_depth == 1) {
169 /* Convert -H to -L */ 175 /* Convert -H to -L */
170 G.slink_depth = INT_MAX; 176 G.slink_depth = INT_MAX;
@@ -241,11 +247,14 @@ int du_main(int argc UNUSED_PARAM, char **argv)
241 */ 247 */
242#if ENABLE_FEATURE_HUMAN_READABLE 248#if ENABLE_FEATURE_HUMAN_READABLE
243 opt = getopt32(argv, "^" 249 opt = getopt32(argv, "^"
244 "aHkLsxd:+lchm" 250 "aHkLsxd:+lcbhm"
245 "\0" "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s", 251 "\0" "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s",
246 &G.max_print_depth 252 &G.max_print_depth
247 ); 253 );
248 argv += optind; 254 argv += optind;
255 if (opt & OPT_b) {
256 G.disp_unit = 1;
257 }
249 if (opt & OPT_h_for_humans) { 258 if (opt & OPT_h_for_humans) {
250 G.disp_unit = 0; 259 G.disp_unit = 0;
251 } 260 }
@@ -257,16 +266,16 @@ int du_main(int argc UNUSED_PARAM, char **argv)
257 } 266 }
258#else 267#else
259 opt = getopt32(argv, "^" 268 opt = getopt32(argv, "^"
260 "aHkLsxd:+lc" 269 "aHkLsxd:+lcb"
261 "\0" "H-L:L-H:s-d:d-s", 270 "\0" "H-L:L-H:s-d:d-s",
262 &G.max_print_depth 271 &G.max_print_depth
263 ); 272 );
264 argv += optind; 273 argv += optind;
265#if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K 274# if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
266 if (opt & OPT_k_kbytes) { 275 if (opt & OPT_k_kbytes) {
267 G.disp_k = 1; 276 G.disp_k = 1;
268 } 277 }
269#endif 278# endif
270#endif 279#endif
271 if (opt & OPT_H_follow_links) { 280 if (opt & OPT_H_follow_links) {
272 G.slink_depth = 1; 281 G.slink_depth = 1;