diff options
author | pjd <pjd@FreeBSD.org> | 2004-05-25 14:53:47 +0000 |
---|---|---|
committer | pjd <pjd@FreeBSD.org> | 2004-05-25 14:53:47 +0000 |
commit | b9afd62a76cd169a61b5a7f64a87d54193678755 (patch) | |
tree | bfb8e981e5e1989483c76400d694f2dce7a90532 | |
parent | e12c97ac070c61c56e9e4b8e850ae984120726c9 (diff) | |
download | FreeBSD-src-b9afd62a76cd169a61b5a7f64a87d54193678755.zip FreeBSD-src-b9afd62a76cd169a61b5a7f64a87d54193678755.tar.gz |
Use humanize_number(3).
Reminded by: jhb
-rw-r--r-- | bin/ls/Makefile | 4 | ||||
-rw-r--r-- | bin/ls/print.c | 63 |
2 files changed, 7 insertions, 60 deletions
diff --git a/bin/ls/Makefile b/bin/ls/Makefile index 12a130d..2af1203 100644 --- a/bin/ls/Makefile +++ b/bin/ls/Makefile @@ -3,8 +3,8 @@ PROG= ls SRCS= cmp.c ls.c print.c util.c -DPADD= ${LIBM} -LDADD= -lm +DPADD= ${LIBUTIL} +LDADD= -lutil .if !defined(RELEASE_CRUNCH) CFLAGS+= -DCOLORLS diff --git a/bin/ls/print.c b/bin/ls/print.c index eb0258e..3e57b37 100644 --- a/bin/ls/print.c +++ b/bin/ls/print.c @@ -45,8 +45,8 @@ __FBSDID("$FreeBSD$"); #include <err.h> #include <errno.h> #include <fts.h> -#include <math.h> #include <langinfo.h> +#include <libutil.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -74,27 +74,6 @@ static void aclmode(char *, const FTSENT *, int *); #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) -#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)) - -static unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ}; - -typedef enum { - NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX -} unit_t; -static unit_t unit_adjust(double *); - -static unit_t unitp[] = {NONE, KILO, MEGA, GIGA, TERA, PETA}; - #ifdef COLORLS /* Most of these are taken from <sys/stat.h> */ typedef enum Colors { @@ -613,49 +592,17 @@ printlink(const FTSENT *p) static void printsize(size_t width, off_t bytes) { - double dbytes; - unit_t unit; if (f_humanval) { - dbytes = bytes; - unit = unit_adjust(&dbytes); + char buf[5]; - if (unit == 0) - (void)printf("%*d%c ", 4, (int)bytes, 'B'); - else - (void)printf("%*.*f%c ", 4, - dbytes >= 99.95 ? 0 : 1, dbytes, "BKMGTPE"[unit]); + humanize_number(buf, sizeof(buf), (int64_t)bytes, "", + HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); + (void)printf("%5s ", buf); } else (void)printf("%*jd ", (u_int)width, bytes); } -/* - * 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. - * - */ -static unit_t -unit_adjust(double *val) -{ - double abval; - unit_t unit; - u_int unit_sz; - - abval = fabs(*val); - - unit_sz = abval ? (u_int)ilogb(abval) / 10 : 0; - - if (unit_sz >= (u_int)UNIT_MAX) { - unit = NONE; - } else { - unit = unitp[unit_sz]; - *val /= (double)vals_base2[unit_sz]; - } - - return (unit); -} - static void aclmode(char *buf, const FTSENT *p, int *haveacls) { |