From 598420ee63bcd384feb9d3bfb8825f534d22d9b3 Mon Sep 17 00:00:00 2001
From: Dima Dorfman
Date: Fri, 18 Oct 2002 04:06:59 +0000
Subject: [PATCH] Output "human-readable" values with a non-0 precision where
appropriate. Before this, a 2.9 GB file was misleadingly reported as "2G".
This mostly brings unit_adjust() in line with what is in du(1).
Reviewed by: jmallett
Approved by: nik
---
bin/ls/print.c | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/bin/ls/print.c b/bin/ls/print.c
index c6f15d3f743..4f2907affe9 100644
--- a/bin/ls/print.c
+++ b/bin/ls/print.c
@@ -93,7 +93,7 @@ static unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TE
typedef enum {
NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX
} unit_t;
-static unit_t unit_adjust(off_t *);
+static unit_t unit_adjust(double *);
static int unitp[] = {NONE, KILO, MEGA, GIGA, TERA, PETA};
@@ -602,16 +602,18 @@ printlink(FTSENT *p)
static void
printsize(size_t width, off_t bytes)
{
+ double dbytes;
unit_t unit;
if (f_humanval) {
- unit = unit_adjust(&bytes);
+ dbytes = bytes;
+ unit = unit_adjust(&dbytes);
- if (bytes == 0)
+ if (dbytes == 0)
(void)printf("%*s ", width, "0B");
else
- (void)printf("%*lld%c ", width - 1, bytes,
- "BKMGTPE"[unit]);
+ (void)printf("%*.*f%c ", width - 1, dbytes > 10 ? 0 : 1,
+ dbytes, "BKMGTPE"[unit]);
} else
(void)printf("%*lld ", width, bytes);
}
@@ -623,13 +625,13 @@ printsize(size_t width, off_t bytes)
*
*/
unit_t
-unit_adjust(off_t *val)
+unit_adjust(double *val)
{
double abval;
unit_t unit;
unsigned int unit_sz;
- abval = fabs((double)*val);
+ abval = fabs(*val);
unit_sz = abval ? ilogb(abval) / 10 : 0;