aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/du.c37
-rw-r--r--libbb/human_readable.c11
2 files changed, 26 insertions, 22 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
76struct globals { 76struct 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 */
96static void print(unsigned long long size, const char *filename) 92static 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+";
diff --git a/libbb/human_readable.c b/libbb/human_readable.c
index 5c7fc076f..b4e0ef181 100644
--- a/libbb/human_readable.c
+++ b/libbb/human_readable.c
@@ -14,16 +14,11 @@
14 * representations (say, powers of 1024) and manipulating coefficients. 14 * representations (say, powers of 1024) and manipulating coefficients.
15 * The base ten "bytes" output could be handled similarly. 15 * The base ten "bytes" output could be handled similarly.
16 * 16 *
17 * 2) This routine always outputs a decimal point and a tenths digit when 17 * 2) This routine outputs a decimal point and a tenths digit when
18 * display_unit != 0. Hence, it isn't uncommon for the returned string 18 * display_unit == 0. Hence, it isn't uncommon for the returned string
19 * to have a length of 5 or 6. 19 * to have a length of 5 or 6.
20 * 20 *
21 * It might be nice to add a flag to indicate no decimal digits in 21 * If block_size is also 0, no decimal digits are printed.
22 * that case. This could be either an additional parameter, or a
23 * special value of display_unit. Such a flag would also be nice for du.
24 *
25 * Some code to omit the decimal point and tenths digit is sketched out
26 * and "#if 0"'d below.
27 * 22 *
28 * Licensed under GPLv2, see file LICENSE in this source tree. 23 * Licensed under GPLv2, see file LICENSE in this source tree.
29 */ 24 */