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>
diff --git a/coreutils/du.c b/coreutils/du.c
index 9c6ff88..1889c16 100644
--- a/coreutils/du.c
+++ b/coreutils/du.c
@@ -75,7 +75,7 @@
struct globals {
#if ENABLE_FEATURE_HUMAN_READABLE
- unsigned long disp_hr;
+ unsigned long disp_unit;
#else
unsigned disp_k;
#endif
@@ -89,18 +89,27 @@
#define INIT_G() do { } while (0)
-/* FIXME? coreutils' du rounds sizes up:
- * for example, 1025k file is shown as "2" by du -m.
- * We round to nearest.
- */
static void print(unsigned long long size, const char *filename)
{
/* TODO - May not want to defer error checking here. */
#if ENABLE_FEATURE_HUMAN_READABLE
+# if ENABLE_DESKTOP
+ /* ~30 bytes of code for extra comtat:
+ * coreutils' du rounds sizes up:
+ * for example, 1025k file is shown as "2" by du -m.
+ * We round to nearest if human-readable [too hard to fix],
+ * else (fixed scale such as -m), we round up. To that end,
+ * add yet another half of the unit before displaying:
+ */
+ if (G.disp_unit)
+ size += (G.disp_unit-1) / (unsigned)(512 * 2);
+# endif
printf("%s\t%s\n",
- /* size x 512 / G.disp_hr, show one fractional,
- * use suffixes if G.disp_hr == 0 */
- make_human_readable_str(size, 512, G.disp_hr),
+ /* size x 512 / G.disp_unit.
+ * If G.disp_unit == 0, show one fractional
+ * and use suffixes
+ */
+ make_human_readable_str(size, 512, G.disp_unit),
filename);
#else
if (G.disp_k) {
@@ -199,10 +208,10 @@
INIT_G();
#if ENABLE_FEATURE_HUMAN_READABLE
- IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 1024;)
- IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_hr = 512;)
+ IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 1024;)
+ IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_unit = 512;)
if (getenv("POSIXLY_CORRECT")) /* TODO - a new libbb function? */
- G.disp_hr = 512;
+ G.disp_unit = 512;
#else
IF_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 1;)
/* IF_NOT_FEATURE_DU_DEFAULT_BLOCKSIZE_1K(G.disp_k = 0;) - G is pre-zeroed */
@@ -220,13 +229,13 @@
opt = getopt32(argv, "aHkLsx" "d:" "lc" "hm", &G.max_print_depth);
argv += optind;
if (opt & OPT_h_for_humans) {
- G.disp_hr = 0;
+ G.disp_unit = 0;
}
if (opt & OPT_m_mbytes) {
- G.disp_hr = 1024*1024;
+ G.disp_unit = 1024*1024;
}
if (opt & OPT_k_kbytes) {
- G.disp_hr = 1024;
+ G.disp_unit = 1024;
}
#else
opt_complementary = "H-L:L-H:s-d:d-s:d+";
diff --git a/libbb/human_readable.c b/libbb/human_readable.c
index 5c7fc07..b4e0ef1 100644
--- a/libbb/human_readable.c
+++ b/libbb/human_readable.c
@@ -14,16 +14,11 @@
* representations (say, powers of 1024) and manipulating coefficients.
* The base ten "bytes" output could be handled similarly.
*
- * 2) This routine always outputs a decimal point and a tenths digit when
- * display_unit != 0. Hence, it isn't uncommon for the returned string
+ * 2) This routine outputs a decimal point and a tenths digit when
+ * display_unit == 0. Hence, it isn't uncommon for the returned string
* to have a length of 5 or 6.
*
- * It might be nice to add a flag to indicate no decimal digits in
- * that case. This could be either an additional parameter, or a
- * special value of display_unit. Such a flag would also be nice for du.
- *
- * Some code to omit the decimal point and tenths digit is sketched out
- * and "#if 0"'d below.
+ * If block_size is also 0, no decimal digits are printed.
*
* Licensed under GPLv2, see file LICENSE in this source tree.
*/