#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
The Regents of the University of California. All rights reserved.");
#endif
#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
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fts.h>
#include <inttypes.h>
#include <util.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#define COUNT (iflag ? 1 : p->fts_statp->st_blocks)
static int linkchk(dev_t, ino_t);
static void prstat(const char *, int64_t);
__dead static void usage(void);
static int hflag, iflag;
static long blocksize;
int
main(int argc, char *argv[])
{
FTS *fts;
FTSENT *p;
int64_t totalblocks;
int ftsoptions, listfiles;
int depth;
int Hflag, Lflag, aflag, ch, cflag, dflag, gkmflag, nflag, rval, sflag;
const 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)
switch (ch) {
case 'H':
Hflag = 1;
Lflag = 0;
break;
case 'L':
Lflag = 1;
Hflag = 0;
break;
case 'P':
Hflag = Lflag = 0;
break;
case 'a':
aflag = 1;
break;
case 'c':
cflag = 1;
break;
case 'd':
dflag = 1;
depth = atoi(optarg);
if (depth < 0 || depth > SHRT_MAX) {
warnx("invalid argument to option d: %s",
optarg);
usage();
}
break;
case 'g':
blocksize = 1024 * 1024 * 1024;
gkmflag = 1;
break;
case 'h':
hflag = 1;
break;
case 'i':
iflag = 1;
break;
case 'k':
blocksize = 1024;
gkmflag = 1;
break;
case 'm':
blocksize = 1024 * 1024;
gkmflag = 1;
break;
case 'n':
nflag = 1;
break;
case 'r':
break;
case 's':
sflag = 1;
break;
case 'x':
ftsoptions |= FTS_XDEV;
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (Hflag)
ftsoptions |= FTS_COMFOLLOW;
if (Lflag) {
ftsoptions &= ~FTS_PHYSICAL;
ftsoptions |= FTS_LOGICAL;
}
listfiles = 0;
if (aflag) {
if (sflag || dflag)
usage();
listfiles = 1;
} else if (sflag) {
if (dflag)
usage();
depth = 0;
}
if (!*argv) {
noargv[0] = ".";
noargv[1] = NULL;
argv = __UNCONST(noargv);
}
if (!gkmflag)
(void)getbsize(NULL, &blocksize);
blocksize /= 512;
if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
err(1, "fts_open `%s'", *argv);
for (rval = 0; (p = fts_read(fts)) != NULL;) {
if (nflag) {
switch (p->fts_info) {
case FTS_NS:
case FTS_SLNONE:
break;
default:
if (p->fts_statp->st_flags & UF_NODUMP) {
fts_set(fts, p, FTS_SKIP);
continue;
}
}
}
switch (p->fts_info) {
case FTS_D:
break;
case FTS_DP:
p->fts_parent->fts_number +=
p->fts_number += COUNT;
if (cflag)
totalblocks += COUNT;
if (p->fts_level <= depth
|| (!listfiles && !p->fts_level))
prstat(p->fts_path, p->fts_number);
break;
case FTS_DC:
break;
case FTS_DNR:
case FTS_ERR:
case FTS_NS:
warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
rval = 1;
break;
default:
if (p->fts_statp->st_nlink > 1 &&
linkchk(p->fts_statp->st_dev, p->fts_statp->st_ino))
break;
if (listfiles || !p->fts_level)
prstat(p->fts_path, COUNT);
p->fts_parent->fts_number += COUNT;
if (cflag)
totalblocks += COUNT;
}
}
if (errno)
err(1, "fts_read");
if (cflag)
prstat("total", totalblocks);
exit(rval);
}
static void
prstat(const char *fname, int64_t blocks)
{
if (iflag) {
(void)printf("%" PRId64 "\t%s\n", blocks, fname);
return;
}
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);
} else
(void)printf("%" PRId64 "\t%s\n",
howmany(blocks, (int64_t)blocksize),
fname);
}
static int
linkchk(dev_t dev, ino_t ino)
{
static struct entry {
dev_t dev;
ino_t ino;
} *htable;
static int htshift;
static int htmask;
static int htused;
static int sawzero;
int h, h2;
uint64_t tmp;
const uint64_t HTCONST = 11400714819323198485ULL;
const int HTBITS = CHAR_BIT * sizeof(tmp);
if (dev == 0 && ino == 0) {
h = sawzero;
sawzero = 1;
return h;
}
if (htused<<1 >= htmask) {
struct entry *ohtable;
if (!htable)
htshift = 10;
else
htshift++;
htmask = (1 << htshift) - 1;
htused = 0;
ohtable = htable;
htable = calloc(htmask+1, sizeof(*htable));
if (!htable)
err(1, "calloc");
if (ohtable) {
int i;
for (i = 0; i <= htmask>>1; i++)
if (ohtable[i].ino || ohtable[i].dev)
linkchk(ohtable[i].dev, ohtable[i].ino);
free(ohtable);
}
}
tmp = dev;
tmp <<= HTBITS>>1;
tmp |= ino;
tmp *= HTCONST;
h = tmp >> (HTBITS - htshift);
h2 = 1 | ( tmp >> (HTBITS - (htshift<<1) - 1));
while (htable[h].ino || htable[h].dev) {
if ((htable[h].ino == ino) && (htable[h].dev == dev))
return 1;
h = (h + h2) & htmask;
}
htable[h].dev = dev;
htable[h].ino = ino;
htused++;
return 0;
}
static void
usage(void)
{
(void)fprintf(stderr,
"usage: du [-H | -L | -P] [-a | -d depth | -s] [-cghikmnrx] [file ...]\n");
exit(1);
}