diff options
Diffstat (limited to 'usr/src/cmd/du/du.c')
-rw-r--r-- | usr/src/cmd/du/du.c | 82 |
1 files changed, 5 insertions, 77 deletions
diff --git a/usr/src/cmd/du/du.c b/usr/src/cmd/du/du.c index 3911135b50..24ca7aca50 100644 --- a/usr/src/cmd/du/du.c +++ b/usr/src/cmd/du/du.c @@ -22,6 +22,7 @@ * Copyright 2017 OmniTI Computer Consulting, Inc. All rights reserved. * Copyright 2007 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. + * Copyright 2017 Jason King */ /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ @@ -66,9 +67,6 @@ static char *name; static size_t base_len = PATH_MAX + 1; /* # of chars for base */ static size_t name_len = PATH_MAX + 1; /* # of chars for name */ -#define NUMBER_WIDTH 64 -typedef char numbuf_t[NUMBER_WIDTH]; - /* * Output formats. illumos uses a tab as separator, XPG4 a space. */ @@ -528,75 +526,6 @@ descend(char *curname, int curfd, int *retcode, dev_t device) return (blocks); } -/* - * Convert an unsigned long long to a string representation and place the - * result in the caller-supplied buffer. - * The given number is in units of "unit_from" size, - * this will first be converted to a number in 1024 or 1000 byte size, - * depending on the scaling factor. - * Then the number is scaled down until it is small enough to be in a good - * human readable format i.e. in the range 0 thru scale-1. - * If it's smaller than 10 there's room enough to provide one decimal place. - * The value "(unsigned long long)-1" is a special case and is always - * converted to "-1". - * Returns a pointer to the caller-supplied buffer. - */ -static char * -number_to_scaled_string( - numbuf_t buf, /* put the result here */ - unsigned long long number, /* convert this number */ - unsigned long long unit_from, /* number of bytes per input unit */ - unsigned long long scale) /* 1024 (-h) or 1000 (-H) */ -{ - unsigned long long save = 0; - char *M = "KMGTPE"; /* Measurement: kilo, mega, giga, tera, peta, exa */ - char *uom = M; /* unit of measurement, initially 'K' (=M[0]) */ - - if ((long long)number == (long long)-1) { - (void) strcpy(buf, "-1"); - return (buf); - } - - /* - * Convert number from unit_from to given scale (1024 or 1000) - * This means multiply number with unit_from and divide by scale. - * if number is large enough, we first divide and then multiply - * to avoid an overflow - * (large enough here means 100 (rather arbitrary value) - * times scale in order to reduce rounding errors) - * otherwise, we first multiply and then divide - * to avoid an underflow - */ - if (number >= 100L * scale) { - number = number / scale; - number = number * unit_from; - } else { - number = number * unit_from; - number = number / scale; - } - - /* - * Now we have number as a count of scale units. - * Stop scaling when we reached exa bytes, then something is - * probably wrong with our number. - */ - while ((number >= scale) && (*uom != 'E')) { - uom++; /* next unit of measurement */ - save = number; - number = (number + (scale / 2)) / scale; - } - - /* check if we should output a decimal place after the point */ - if (save && ((save / scale) < 10)) { - /* sprintf() will round for us */ - float fnum = (float)save / scale; - (void) sprintf(buf, "%4.1f%c", fnum, *uom); - } else { - (void) sprintf(buf, "%4llu%c", number, *uom); - } - return (buf); -} - static void printsize(blkcnt_t blocks, char *path) { @@ -605,11 +534,10 @@ printsize(blkcnt_t blocks, char *path) bsize = Aflg ? 1 : DEV_BSIZE; if (hflg) { - numbuf_t numbuf; - unsigned long long scale = 1024L; - (void) printf(FORMAT1, - number_to_scaled_string(numbuf, blocks, bsize, scale), - path); + char buf[NN_NUMBUF_SZ] = { 0 }; + + nicenum_scale(blocks, bsize, buf, sizeof (buf), 0); + (void) printf(FORMAT1, buf, path); } else if (kflg) { (void) printf(FORMAT2, (long long)kb(blocks), path); } else if (mflg) { |