From 49985d6dab23172573beb37984a45eb61282395f Mon Sep 17 00:00:00 2001 From: Kiyoshi Aman Date: Wed, 29 May 2019 06:00:00 -0500 Subject: usr.bin/du: make buildable --- usr.bin/du/du.1 | 2 +- usr.bin/du/du.c | 94 +++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 59 insertions(+), 37 deletions(-) (limited to 'usr.bin') diff --git a/usr.bin/du/du.1 b/usr.bin/du/du.1 index 14bb436..eb23f94 100644 --- a/usr.bin/du/du.1 +++ b/usr.bin/du/du.1 @@ -39,7 +39,7 @@ .Nm .Op Fl H | Fl L | Fl P .Op Fl a | Fl d Ar depth | Fl s -.Op Fl cghikmnrx +.Op Fl chiknrx .Op Ar file ... .Sh DESCRIPTION The diff --git a/usr.bin/du/du.c b/usr.bin/du/du.c index 5b13232..213a5ef 100644 --- a/usr.bin/du/du.c +++ b/usr.bin/du/du.c @@ -32,32 +32,23 @@ * SUCH DAMAGE. */ -#include -#ifndef lint -__COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\ - The Regents of the University of California. All rights reserved."); -#endif /* not lint */ - -#ifndef lint -#if 0 -static char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95"; -#else -__RCSID("$NetBSD: du.c,v 1.36 2012/03/11 11:23:20 shattered Exp $"); -#endif -#endif /* not lint */ - +#include #include #include #include +#include + #include #include #include #include #include -#include +#include +#include #include #include +#include #include #include #include @@ -67,7 +58,7 @@ __RCSID("$NetBSD: du.c,v 1.36 2012/03/11 11:23:20 shattered Exp $"); static int linkchk(dev_t, ino_t); static void prstat(const char *, int64_t); -__dead static void usage(void); +static void usage(void); static int hflag, iflag; static long blocksize; @@ -81,13 +72,13 @@ main(int argc, char *argv[]) int ftsoptions, listfiles; int depth; int Hflag, Lflag, aflag, ch, cflag, dflag, gkmflag, nflag, rval, sflag; - const char *noargv[2]; + char *noargv[2]; Hflag = Lflag = aflag = cflag = dflag = gkmflag = nflag = sflag = 0; totalblocks = 0; ftsoptions = FTS_PHYSICAL; depth = INT_MAX; - while ((ch = getopt(argc, argv, "HLPacd:ghikmnrsx")) != -1) + while ((ch = getopt(argc, argv, "HLPacd:hiknrsx")) != -1) switch (ch) { case 'H': Hflag = 1; @@ -115,10 +106,6 @@ main(int argc, char *argv[]) usage(); } break; - case 'g': - blocksize = 1024 * 1024 * 1024; - gkmflag = 1; - break; case 'h': hflag = 1; break; @@ -129,10 +116,6 @@ main(int argc, char *argv[]) blocksize = 1024; gkmflag = 1; break; - case 'm': - blocksize = 1024 * 1024; - gkmflag = 1; - break; case 'n': nflag = 1; break; @@ -184,11 +167,11 @@ main(int argc, char *argv[]) if (!*argv) { noargv[0] = "."; noargv[1] = NULL; - argv = __UNCONST(noargv); + argv = noargv; } if (!gkmflag) - (void)getbsize(NULL, &blocksize); + blocksize = 512; blocksize /= 512; if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL) @@ -196,13 +179,28 @@ main(int argc, char *argv[]) for (rval = 0; (p = fts_read(fts)) != NULL;) { if (nflag) { + FILE *file; + int flags; switch (p->fts_info) { case FTS_NS: case FTS_SLNONE: /* nothing */ break; default: - if (p->fts_statp->st_flags & UF_NODUMP) { + file = fopen(p->fts_name, "r"); + if (file == NULL) { + fprintf(stderr, "%s: couldn't check %s: %s\n", + getprogname(), p->fts_name, strerror(errno) + ); + continue; + } + if (ioctl(fileno(file), FS_IOC_GETFLAGS, &flags) == -1) { + fprintf(stderr, "%s: couldn't check %s: %s\n", + getprogname(), p->fts_name, strerror(errno) + ); + continue; + } + if (flags & FS_NODUMP_FL) { fts_set(fts, p, FTS_SKIP); continue; } @@ -255,6 +253,36 @@ main(int argc, char *argv[]) exit(rval); } +char *scale[] = { + "", + "k", + "M", + "G", + "T", + "P", + "E", + "Z", + "Y" +}; + +char *humanize(uint64_t size, bool si) { + uint16_t unit = si ? 1000 : 1024; + int8_t radix = (int8_t)(log(size)/log(unit)); + char suffix[2], ret[10]; + double val; + uint64_t temp = size; + + if (radix > 9) { + radix = 9; + } + + val = size / (double)pow(unit, radix); + + snprintf(ret, 10, "%.0f%s%sB", val, scale[radix], (si || (radix == 0)) ? "" : "i"); + + return strndup(ret, 10); +} + static void prstat(const char *fname, int64_t blocks) { @@ -264,13 +292,7 @@ prstat(const char *fname, int64_t blocks) } if (hflag) { - char buf[5]; - int64_t sz = blocks * 512; - - humanize_number(buf, sizeof(buf), sz, "", HN_AUTOSCALE, - HN_B | HN_NOSPACE | HN_DECIMAL); - - (void)printf("%s\t%s\n", buf, fname); + (void)printf("%s\t%s\n", humanize(blocks * 512, false), fname); } else (void)printf("%" PRId64 "\t%s\n", howmany(blocks, (int64_t)blocksize), -- cgit v1.2.3-70-g09d2