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>
This commit is contained in:
Denys Vlasenko 2021-06-16 17:28:02 +02:00
parent 46d315ae44
commit e7a8e8e30c

View File

@ -42,6 +42,7 @@
//usage:#define du_full_usage "\n\n" //usage:#define du_full_usage "\n\n"
//usage: "Summarize disk space used for FILEs (or directories)\n" //usage: "Summarize disk space used for FILEs (or directories)\n"
//usage: "\n -a Show file sizes too" //usage: "\n -a Show file sizes too"
//usage: "\n -b Apparent size (including holes)"
//usage: "\n -L Follow all symlinks" //usage: "\n -L Follow all symlinks"
//usage: "\n -H Follow symlinks on command line" //usage: "\n -H Follow symlinks on command line"
//usage: "\n -d N Limit output to directories (and files with -a) of depth < N" //usage: "\n -d N Limit output to directories (and files with -a) of depth < N"
@ -84,8 +85,9 @@ enum {
OPT_d_maxdepth = (1 << 6), OPT_d_maxdepth = (1 << 6),
OPT_l_hardlinks = (1 << 7), OPT_l_hardlinks = (1 << 7),
OPT_c_total = (1 << 8), OPT_c_total = (1 << 8),
OPT_h_for_humans = (1 << 9), OPT_b = (1 << 9),
OPT_m_mbytes = (1 << 10), OPT_h_for_humans = (1 << 10),
OPT_m_mbytes = (1 << 11),
}; };
struct globals { struct globals {
@ -109,7 +111,7 @@ static void print(unsigned long long size, const char *filename)
/* TODO - May not want to defer error checking here. */ /* TODO - May not want to defer error checking here. */
#if ENABLE_FEATURE_HUMAN_READABLE #if ENABLE_FEATURE_HUMAN_READABLE
# if ENABLE_DESKTOP # if ENABLE_DESKTOP
/* ~30 bytes of code for extra comtat: /* ~30 bytes of code for extra compat:
* coreutils' du rounds sizes up: * coreutils' du rounds sizes up:
* for example, 1025k file is shown as "2" by du -m. * for example, 1025k file is shown as "2" by du -m.
* We round to nearest if human-readable [too hard to fix], * 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)
* If G.disp_unit == 0, show one fractional * If G.disp_unit == 0, show one fractional
* and use suffixes * and use suffixes
*/ */
make_human_readable_str(size, 512, G.disp_unit), make_human_readable_str(size, (option_mask32 & OPT_b) ? 1 : 512, G.disp_unit),
filename); filename);
#else #else
if (G.disp_k) { if (G.disp_k) {
size++; if (!(option_mask32 & OPT_b)) {
size >>= 1; size++;
size >>= 1;
} else {
size >>= 10;
}
} }
printf("%llu\t%s\n", size, filename); printf("%llu\t%s\n", size, filename);
#endif #endif
@ -155,7 +161,7 @@ static unsigned long long du(const char *filename)
} }
} }
sum = statbuf.st_blocks; sum = ((option_mask32 & OPT_b) ? statbuf.st_size : statbuf.st_blocks);
if (S_ISLNK(statbuf.st_mode)) { if (S_ISLNK(statbuf.st_mode)) {
if (G.slink_depth > G.du_depth) { /* -H or -L */ if (G.slink_depth > G.du_depth) { /* -H or -L */
@ -164,7 +170,7 @@ static unsigned long long du(const char *filename)
G.status = EXIT_FAILURE; G.status = EXIT_FAILURE;
return 0; return 0;
} }
sum = statbuf.st_blocks; sum = ((option_mask32 & OPT_b) ? statbuf.st_size : statbuf.st_blocks);
if (G.slink_depth == 1) { if (G.slink_depth == 1) {
/* Convert -H to -L */ /* Convert -H to -L */
G.slink_depth = INT_MAX; G.slink_depth = INT_MAX;
@ -241,11 +247,14 @@ int du_main(int argc UNUSED_PARAM, char **argv)
*/ */
#if ENABLE_FEATURE_HUMAN_READABLE #if ENABLE_FEATURE_HUMAN_READABLE
opt = getopt32(argv, "^" opt = getopt32(argv, "^"
"aHkLsxd:+lchm" "aHkLsxd:+lcbhm"
"\0" "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s", "\0" "h-km:k-hm:m-hk:H-L:L-H:s-d:d-s",
&G.max_print_depth &G.max_print_depth
); );
argv += optind; argv += optind;
if (opt & OPT_b) {
G.disp_unit = 1;
}
if (opt & OPT_h_for_humans) { if (opt & OPT_h_for_humans) {
G.disp_unit = 0; G.disp_unit = 0;
} }
@ -257,16 +266,16 @@ int du_main(int argc UNUSED_PARAM, char **argv)
} }
#else #else
opt = getopt32(argv, "^" opt = getopt32(argv, "^"
"aHkLsxd:+lc" "aHkLsxd:+lcb"
"\0" "H-L:L-H:s-d:d-s", "\0" "H-L:L-H:s-d:d-s",
&G.max_print_depth &G.max_print_depth
); );
argv += optind; argv += optind;
#if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K # if !ENABLE_FEATURE_DU_DEFAULT_BLOCKSIZE_1K
if (opt & OPT_k_kbytes) { if (opt & OPT_k_kbytes) {
G.disp_k = 1; G.disp_k = 1;
} }
#endif # endif
#endif #endif
if (opt & OPT_H_follow_links) { if (opt & OPT_H_follow_links) {
G.slink_depth = 1; G.slink_depth = 1;