From 4dd884eaf13087370ad284cfb435dff5c6b8e85a Mon Sep 17 00:00:00 2001 From: Sascha Wildner Date: Mon, 4 Aug 2008 19:22:44 +0000 Subject: [PATCH] Use libutil's humanize_number(3) for -h. Submitted-by: Samuel J. Greear Taken-from: FreeBSD --- usr.bin/du/Makefile | 6 ++-- usr.bin/du/du.c | 78 ++++++--------------------------------------- 2 files changed, 13 insertions(+), 71 deletions(-) diff --git a/usr.bin/du/Makefile b/usr.bin/du/Makefile index e6cdbb2c02..9da6f46c97 100644 --- a/usr.bin/du/Makefile +++ b/usr.bin/du/Makefile @@ -1,9 +1,9 @@ # @(#)Makefile 8.1 (Berkeley) 6/6/93 # $FreeBSD: src/usr.bin/du/Makefile,v 1.4.2.1 2000/07/02 10:45:29 ps Exp $ -# $DragonFly: src/usr.bin/du/Makefile,v 1.4 2007/08/27 16:50:53 pavalos Exp $ +# $DragonFly: src/usr.bin/du/Makefile,v 1.5 2008/08/04 19:22:44 swildner Exp $ PROG= du -DPADD= ${LIBM} -LDADD= -lm +DPADD= ${LIBUTIL} +LDADD= -lutil .include diff --git a/usr.bin/du/du.c b/usr.bin/du/du.c index fa484c35aa..253e3d7ee2 100644 --- a/usr.bin/du/du.c +++ b/usr.bin/du/du.c @@ -36,7 +36,7 @@ * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California. All rights reserved. * @(#)du.c 8.5 (Berkeley) 5/4/95 * $FreeBSD: src/usr.bin/du/du.c,v 1.17.2.4 2002/12/12 16:29:39 trhodes Exp $ - * $DragonFly: src/usr.bin/du/du.c,v 1.10 2008/07/13 03:37:43 dillon Exp $ + * $DragonFly: src/usr.bin/du/du.c,v 1.11 2008/08/04 19:22:44 swildner Exp $ */ #include @@ -47,42 +47,16 @@ #include #include #include -#include +#include #include #include #include #include #include -#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)) - #define HASHSIZE 256 /* power of 2 only */ #define HASHMASK (HASHSIZE - 1) -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; @@ -91,8 +65,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 *); @@ -161,7 +134,6 @@ main(int argc, char **argv) if (putenv("BLOCKSIZE=512") == -1) warn("putenv: cannot set BLOCKSIZE=512"); hflag = 1; - valp = vals_base2; break; case 'k': hflag = 0; @@ -456,47 +428,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[4]; + + 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 -- 2.41.0