diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-15 21:33:34 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2015-10-15 21:33:34 +0200 |
commit | 93dd9fd90ae284e7878767fe14bcb17e3edd9cf8 (patch) | |
tree | ce345561860ba2894f0793d846dd54200b6069d5 /coreutils/du.c | |
parent | 5251135bc184bdcb8cbcb964e8c44c6c301bffdc (diff) | |
download | busybox-w32-93dd9fd90ae284e7878767fe14bcb17e3edd9cf8.tar.gz busybox-w32-93dd9fd90ae284e7878767fe14bcb17e3edd9cf8.tar.bz2 busybox-w32-93dd9fd90ae284e7878767fe14bcb17e3edd9cf8.zip |
du: extra compat: with -k and -m, round sizes up
function old new delta
print 36 65 +29
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils/du.c')
-rw-r--r-- | coreutils/du.c | 37 |
1 files changed, 23 insertions, 14 deletions
diff --git a/coreutils/du.c b/coreutils/du.c index 9c6ff8800..1889c16bb 100644 --- a/coreutils/du.c +++ b/coreutils/du.c | |||
@@ -75,7 +75,7 @@ enum { | |||
75 | 75 | ||
76 | struct globals { | 76 | struct globals { |
77 | #if ENABLE_FEATURE_HUMAN_READABLE | 77 | #if ENABLE_FEATURE_HUMAN_READABLE |
78 | unsigned long disp_hr; | 78 | unsigned long disp_unit; |
79 | #else | 79 | #else |
80 | unsigned disp_k; | 80 | unsigned disp_k; |
81 | #endif | 81 | #endif |
@@ -89,18 +89,27 @@ struct globals { | |||
89 | #define INIT_G() do { } while (0) | 89 | #define INIT_G() do { } while (0) |
90 | 90 | ||
91 | 91 | ||
92 | /* FIXME? coreutils' du rounds sizes up: | ||
93 | * for example, 1025k file is shown as "2" by du -m. | ||
94 | * We round to nearest. | ||
95 | */ | ||
96 | static void print(unsigned long long size, const char *filename) | 92 | static void print(unsigned long long size, const char *filename) |
97 | { | 93 | { |
98 | /* TODO - May not want to defer error checking here. */ | 94 | /* TODO - May not want to defer error checking here. */ |
99 | #if ENABLE_FEATURE_HUMAN_READABLE | 95 | #if ENABLE_FEATURE_HUMAN_READABLE |
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 | ||
100 | printf("%s\t%s\n", | 107 | printf("%s\t%s\n", |
101 | /* size x 512 / G.disp_hr, show one fractional, | 108 | /* size x 512 / G.disp_unit. |
102 | * use suffixes if G.disp_hr == 0 */ | 109 | * If G.disp_unit == 0, show one fractional |
103 | make_human_readable_str(size, 512, G.disp_hr), | 110 | * and use suffixes |
111 | */ | ||
112 | make_human_readable_str(size, 512, G.disp_unit), | ||
104 | filename); | 113 | filename); |
105 | #else | 114 | #else |
106 | if (G.disp_k) { | 115 | if (G.disp_k) { |
@@ -199,10 +208,10 @@ int du_main(int argc UNUSED_PARAM, char **argv) | |||
199 | INIT_G(); | 208 | INIT_G(); |
200 | 209 | ||
201 | #if ENABLE_FEATURE_HUMAN_READABLE | 210 | #if ENABLE_FEATURE_HUMAN_READABLE |
202 | IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 1024;) | 211 | IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 1024;) |
203 | IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 512;) | 212 | IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 512;) |
204 | if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */ | 213 | if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */ |
205 | G.disp_hr = 512; | 214 | G.disp_unit = 512; |
206 | #else | 215 | #else |
207 | IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;) | 216 | IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;) |
208 | /* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */ | 217 | /* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */ |
@@ -220,13 +229,13 @@ int du_main(int argc UNUSED_PARAM, char **argv) | |||
220 | opt = getopt32(argv, "aHkLsx" "d:" "lc" "hm", &G.max_print_depth); | 229 | opt = getopt32(argv, "aHkLsx" "d:" "lc" "hm", &G.max_print_depth); |
221 | argv += optind; | 230 | argv += optind; |
222 | if (opt & OPT_h_for_humans) { | 231 | if (opt & OPT_h_for_humans) { |
223 | G.disp_hr = 0; | 232 | G.disp_unit = 0; |
224 | } | 233 | } |
225 | if (opt & OPT_m_mbytes) { | 234 | if (opt & OPT_m_mbytes) { |
226 | G.disp_hr = 1024*1024; | 235 | G.disp_unit = 1024*1024; |
227 | } | 236 | } |
228 | if (opt & OPT_k_kbytes) { | 237 | if (opt & OPT_k_kbytes) { |
229 | G.disp_hr = 1024; | 238 | G.disp_unit = 1024; |
230 | } | 239 | } |
231 | #else | 240 | #else |
232 | opt_complementary = "H-L:L-H:s-d:d-s:d+"; | 241 | opt_complementary = "H-L:L-H:s-d:d-s:d+"; |