summaryrefslogtreecommitdiffstats
path: root/usr.bin/du
diff options
context:
space:
mode:
authorpjd <pjd@FreeBSD.org>2004-05-24 22:22:29 +0000
committerpjd <pjd@FreeBSD.org>2004-05-24 22:22:29 +0000
commit72e7aa908c82a0eef8761e6345938417a59b2ebb (patch)
treee2268ad30fce7d66c6f868759ab24a815498aea4 /usr.bin/du
parentd207894810cf9fa574c3f60f611210946e00a166 (diff)
downloadFreeBSD-src-72e7aa908c82a0eef8761e6345938417a59b2ebb.zip
FreeBSD-src-72e7aa908c82a0eef8761e6345938417a59b2ebb.tar.gz
Use humanize_number(3) to format sizes into a human readable form.
Diffstat (limited to 'usr.bin/du')
-rw-r--r--usr.bin/du/Makefile4
-rw-r--r--usr.bin/du/du.c76
2 files changed, 11 insertions, 69 deletions
diff --git a/usr.bin/du/Makefile b/usr.bin/du/Makefile
index 7db32c0..12e80b9 100644
--- a/usr.bin/du/Makefile
+++ b/usr.bin/du/Makefile
@@ -3,7 +3,7 @@
PROG= du
WARNS?= 6
-DPADD= ${LIBM}
-LDADD= -lm
+DPADD= ${LIBUTIL}
+LDADD= -lutil
.include <bsd.prog.mk>
diff --git a/usr.bin/du/du.c b/usr.bin/du/du.c
index 24f2179..f2010f1 100644
--- a/usr.bin/du/du.c
+++ b/usr.bin/du/du.c
@@ -56,39 +56,13 @@ __FBSDID("$FreeBSD$");
#include <errno.h>
#include <fnmatch.h>
#include <fts.h>
-#include <math.h>
+#include <libutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
-#define KILO_SZ(n) (n)
-#define MEGA_SZ(n) ((n) * (n))
-#define GIGA_SZ(n) ((n) * (n) * (n))
-#define TERA_SZ(n) ((n) * (n) * (n) * (n))
-#define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
-
-#define KILO_2_SZ (KILO_SZ(1024ULL))
-#define MEGA_2_SZ (MEGA_SZ(1024ULL))
-#define GIGA_2_SZ (GIGA_SZ(1024ULL))
-#define TERA_2_SZ (TERA_SZ(1024ULL))
-#define PETA_2_SZ (PETA_SZ(1024ULL))
-
-#define KILO_SI_SZ (KILO_SZ(1000ULL))
-#define MEGA_SI_SZ (MEGA_SZ(1000ULL))
-#define GIGA_SI_SZ (GIGA_SZ(1000ULL))
-#define TERA_SI_SZ (TERA_SZ(1000ULL))
-#define PETA_SI_SZ (PETA_SZ(1000ULL))
-
-unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ};
-unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
-unsigned long long *valp;
-
-typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
-
-int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
-
SLIST_HEAD(ignhead, ignentry) ignores;
struct ignentry {
char *mask;
@@ -97,8 +71,7 @@ struct ignentry {
static int linkchk(FTSENT *);
static void usage(void);
-void prthumanval(double);
-unit_t unit_adjust(double *);
+void prthumanval(int64_t);
void ignoreadd(const char *);
void ignoreclean(void);
int ignorep(FTSENT *);
@@ -162,7 +135,6 @@ main(int argc, char *argv[])
case 'h':
putenv("BLOCKSIZE=512");
hflag = 1;
- valp = vals_base2;
break;
case 'k':
hflag = 0;
@@ -441,47 +413,17 @@ linkchk(FTSENT *p)
return (0);
}
-/*
- * Output in "human-readable" format. Uses 3 digits max and puts
- * unit suffixes at the end. Makes output compact and easy to read,
- * especially on huge disks.
- *
- */
-unit_t
-unit_adjust(double *val)
-{
- double abval;
- unit_t unit;
- unsigned int unit_sz;
-
- abval = fabs(*val);
-
- unit_sz = abval ? ilogb(abval) / 10 : 0;
-
- if (unit_sz >= UNIT_MAX) {
- unit = NONE;
- } else {
- unit = unitp[unit_sz];
- *val /= (double)valp[unit_sz];
- }
-
- return (unit);
-}
-
void
-prthumanval(double bytes)
+prthumanval(int64_t bytes)
{
- unit_t unit;
+ char buf[5];
+
+ bytes *= DEV_BSIZE;
- bytes *= 512;
- unit = unit_adjust(&bytes);
+ humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
+ HN_B | HN_NOSPACE | HN_DECIMAL);
- if (bytes == 0)
- (void)printf(" 0B");
- else if (bytes > 10)
- (void)printf("%3.0f%c", bytes, "BKMGTPE"[unit]);
- else
- (void)printf("%3.1f%c", bytes, "BKMGTPE"[unit]);
+ (void)printf("%4s", buf);
}
static void
OpenPOWER on IntegriCloud