summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorKiyoshi Aman <kiyoshi.aman+adelie@gmail.com>2019-05-29 06:00:00 -0500
committerKiyoshi Aman <kiyoshi.aman+adelie@gmail.com>2019-05-29 06:00:00 -0500
commit49985d6dab23172573beb37984a45eb61282395f (patch)
tree1b6414d046484769d143174a81cb9e308faf0975 /usr.bin
parenta1744be670f76aba50d5def240584770ccefe027 (diff)
downloaduserland-49985d6dab23172573beb37984a45eb61282395f.tar.gz
userland-49985d6dab23172573beb37984a45eb61282395f.tar.bz2
userland-49985d6dab23172573beb37984a45eb61282395f.tar.xz
userland-49985d6dab23172573beb37984a45eb61282395f.zip
usr.bin/du: make buildable
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/du/du.12
-rw-r--r--usr.bin/du/du.c94
2 files changed, 59 insertions, 37 deletions
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 <sys/cdefs.h>
-#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 <sys/ioctl.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <linux/fs.h>
+
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fts.h>
#include <inttypes.h>
-#include <util.h>
+#include <math.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
+#include <bsd/stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
@@ -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),