diff options
author | Kyle Evans <kevans@FreeBSD.org> | 2017-08-09 01:19:19 +0000 |
---|---|---|
committer | Kyle Evans <kevans@FreeBSD.org> | 2017-08-09 01:19:19 +0000 |
commit | 476c601b7a6817ba60d74e633666dc7f8d3d11e2 (patch) | |
tree | f5657a52623addaa71fdbc58521e9b75b6ce89e8 /usr.bin/du/du.c | |
parent | 1070a9141ca6b9ce3ba2d31fc7da64850da825e6 (diff) | |
download | src-476c601b7a6817ba60d74e633666dc7f8d3d11e2.tar.gz src-476c601b7a6817ba60d74e633666dc7f8d3d11e2.zip |
du(1): Add --si option to display in terms of powers of 1000
Reviewed by: cem (earlier version), emaste
Approved by: emaste (mentor)
Differential Revision: https://reviews.freebsd.org/D11748
Notes
Notes:
svn path=/head/; revision=322291
Diffstat (limited to 'usr.bin/du/du.c')
-rw-r--r-- | usr.bin/du/du.c | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/usr.bin/du/du.c b/usr.bin/du/du.c index 6affc12ad240..689120837ffb 100644 --- a/usr.bin/du/du.c +++ b/usr.bin/du/du.c @@ -51,6 +51,7 @@ __FBSDID("$FreeBSD$"); #include <errno.h> #include <fnmatch.h> #include <fts.h> +#include <getopt.h> #include <libutil.h> #include <locale.h> #include <stdint.h> @@ -60,6 +61,11 @@ __FBSDID("$FreeBSD$"); #include <sysexits.h> #include <unistd.h> +#define SI_OPT (CHAR_MAX + 1) + +#define UNITS_2 1 +#define UNITS_SI 2 + static SLIST_HEAD(ignhead, ignentry) ignores; struct ignentry { char *mask; @@ -75,10 +81,16 @@ static int ignorep(FTSENT *); static void siginfo(int __unused); static int nodumpflag = 0; -static int Aflag; +static int Aflag, hflag; static long blocksize, cblocksize; static volatile sig_atomic_t info; +static const struct option long_options[] = +{ + { "si", no_argument, NULL, SI_OPT }, + { NULL, no_argument, NULL, 0 }, +}; + int main(int argc, char *argv[]) { @@ -89,14 +101,13 @@ main(int argc, char *argv[]) int ftsoptions; int depth; int Hflag, Lflag, aflag, sflag, dflag, cflag; - int hflag, lflag, ch, notused, rval; + int lflag, ch, notused, rval; char **save; static char dot[] = "."; setlocale(LC_ALL, ""); - Hflag = Lflag = aflag = sflag = dflag = cflag = hflag = - lflag = Aflag = 0; + Hflag = Lflag = aflag = sflag = dflag = cflag = lflag = Aflag = 0; save = argv; ftsoptions = FTS_PHYSICAL; @@ -108,7 +119,8 @@ main(int argc, char *argv[]) depth = INT_MAX; SLIST_INIT(&ignores); - while ((ch = getopt(argc, argv, "AB:HI:LPasd:cghklmnrt:x")) != -1) + while ((ch = getopt_long(argc, argv, "+AB:HI:LPasd:cghklmnrt:x", + long_options, NULL)) != -1) switch (ch) { case 'A': Aflag = 1; @@ -160,7 +172,7 @@ main(int argc, char *argv[]) blocksize = 1073741824; break; case 'h': - hflag = 1; + hflag = UNITS_2; break; case 'k': hflag = 0; @@ -189,6 +201,9 @@ main(int argc, char *argv[]) case 'x': ftsoptions |= FTS_XDEV; break; + case SI_OPT: + hflag = UNITS_SI; + break; case '?': default: usage(); @@ -270,7 +285,7 @@ main(int argc, char *argv[]) if (p->fts_level <= depth && threshold <= threshold_sign * howmany(p->fts_bignum * cblocksize, blocksize)) { - if (hflag) { + if (hflag > 0) { prthumanval(p->fts_bignum); (void)printf("\t%s\n", p->fts_path); } else { @@ -306,7 +321,7 @@ main(int argc, char *argv[]) howmany(p->fts_statp->st_blocks, cblocksize); if (aflag || p->fts_level == 0) { - if (hflag) { + if (hflag > 0) { prthumanval(curblocks); (void)printf("\t%s\n", p->fts_path); } else { @@ -326,7 +341,7 @@ main(int argc, char *argv[]) err(1, "fts_read"); if (cflag) { - if (hflag) { + if (hflag > 0) { prthumanval(savednumber); (void)printf("\ttotal\n"); } else { @@ -474,13 +489,16 @@ static void prthumanval(int64_t bytes) { char buf[5]; + int flags; bytes *= cblocksize; + flags = HN_B | HN_NOSPACE | HN_DECIMAL; if (!Aflag) bytes *= DEV_BSIZE; + if (hflag == UNITS_SI) + flags |= HN_DIVISOR_1000; - humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE, - HN_B | HN_NOSPACE | HN_DECIMAL); + humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE, flags); (void)printf("%4s", buf); } |