summaryrefslogtreecommitdiffstats
path: root/bin/df
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 /bin/df
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 'bin/df')
-rw-r--r--bin/df/Makefile4
-rw-r--r--bin/df/df.c103
2 files changed, 19 insertions, 88 deletions
diff --git a/bin/df/Makefile b/bin/df/Makefile
index 63991d4..df6574c 100644
--- a/bin/df/Makefile
+++ b/bin/df/Makefile
@@ -9,7 +9,7 @@ SRCS= df.c vfslist.c
CFLAGS+= -I${MOUNT}
-DPADD= ${LIBM}
-LDADD= -lm
+DPADD= ${LIBUTIL}
+LDADD= -lutil
.include <bsd.prog.mk>
diff --git a/bin/df/df.c b/bin/df/df.c
index 32b4c73..96022b3 100644
--- a/bin/df/df.c
+++ b/bin/df/df.c
@@ -52,7 +52,7 @@ __FBSDID("$FreeBSD$");
#include <sys/sysctl.h>
#include <ufs/ufs/ufsmount.h>
#include <err.h>
-#include <math.h>
+#include <libutil.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -62,26 +62,8 @@ __FBSDID("$FreeBSD$");
#include "extern.h"
-#define UNITS_SI 1
-#define UNITS_2 2
-
-#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 UNITS_SI 1
+#define UNITS_2 2
/* Maximum widths of various fields. */
struct maxwidths {
@@ -93,37 +75,14 @@ struct maxwidths {
int ifree;
};
-static uintmax_t vals_si [] = {
- 1,
- KILO_SI_SZ,
- MEGA_SI_SZ,
- GIGA_SI_SZ,
- TERA_SI_SZ,
- PETA_SI_SZ
-};
-static uintmax_t vals_base2[] = {
- 1,
- KILO_2_SZ,
- MEGA_2_SZ,
- GIGA_2_SZ,
- TERA_2_SZ,
- PETA_2_SZ
-};
-static uintmax_t *valp;
-
-typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
-
-static unit_t unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
-
static void addstat(struct statfs *, struct statfs *);
static char *getmntpt(const char *);
static int int64width(int64_t);
static char *makenetvfslist(void);
static void prthuman(const struct statfs *, int64_t);
-static void prthumanval(double);
+static void prthumanval(int64_t);
static void prtstat(struct statfs *, struct maxwidths *);
static size_t regetmntinfo(struct statfs **, long, const char **);
-static unit_t unit_adjust(double *);
static void update_maxwidths(struct maxwidths *, const struct statfs *);
static void usage(void);
@@ -175,11 +134,9 @@ main(int argc, char *argv[])
break;
case 'H':
hflag = UNITS_SI;
- valp = vals_si;
break;
case 'h':
hflag = UNITS_2;
- valp = vals_base2;
break;
case 'i':
iflag = 1;
@@ -359,55 +316,29 @@ regetmntinfo(struct statfs **mntbufp, long mntsize, const char **vfslist)
return (j);
}
-/*
- * 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;
- int unit_sz;
-
- abval = fabs(*val);
-
- unit_sz = abval ? ilogb(abval) / 10 : 0;
-
- if (unit_sz >= (int)UNIT_MAX) {
- unit = NONE;
- } else {
- unit = unitp[unit_sz];
- *val /= (double)valp[unit_sz];
- }
-
- return (unit);
-}
-
static void
prthuman(const struct statfs *sfsp, int64_t used)
{
- prthumanval((double)sfsp->f_blocks * (double)sfsp->f_bsize);
- prthumanval((double)used * (double)sfsp->f_bsize);
- prthumanval((double)sfsp->f_bavail * (double)sfsp->f_bsize);
+ prthumanval(sfsp->f_blocks * sfsp->f_bsize);
+ prthumanval(used * sfsp->f_bsize);
+ prthumanval(sfsp->f_bavail * sfsp->f_bsize);
}
static void
-prthumanval(double bytes)
+prthumanval(int64_t bytes)
{
+ char buf[6];
+ int flags;
+
+ flags = HN_B | HN_NOSPACE | HN_DECIMAL;
+ if (hflag == UNITS_SI)
+ flags |= HN_DIVISOR_1000;
- unit_t unit;
- unit = unit_adjust(&bytes);
+ humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1),
+ bytes, "", HN_AUTOSCALE, flags);
- if (bytes == 0)
- (void)printf(" 0B");
- else if (bytes > 10)
- (void)printf(" % 6.0f%c", bytes, "BKMGTPE"[unit]);
- else
- (void)printf(" % 6.1f%c", bytes, "BKMGTPE"[unit]);
+ (void)printf(" %6s", buf);
}
/*
OpenPOWER on IntegriCloud