Changeset View
Changeset View
Standalone View
Standalone View
head/usr.bin/du/du.c
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | |||||
#include <sys/param.h> | #include <sys/param.h> | ||||
#include <sys/queue.h> | #include <sys/queue.h> | ||||
#include <sys/stat.h> | #include <sys/stat.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <fnmatch.h> | #include <fnmatch.h> | ||||
#include <fts.h> | #include <fts.h> | ||||
#include <getopt.h> | |||||
#include <libutil.h> | #include <libutil.h> | ||||
#include <locale.h> | #include <locale.h> | ||||
#include <stdint.h> | #include <stdint.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <sysexits.h> | #include <sysexits.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#define SI_OPT (CHAR_MAX + 1) | |||||
#define UNITS_2 1 | |||||
#define UNITS_SI 2 | |||||
static SLIST_HEAD(ignhead, ignentry) ignores; | static SLIST_HEAD(ignhead, ignentry) ignores; | ||||
struct ignentry { | struct ignentry { | ||||
char *mask; | char *mask; | ||||
SLIST_ENTRY(ignentry) next; | SLIST_ENTRY(ignentry) next; | ||||
}; | }; | ||||
static int linkchk(FTSENT *); | static int linkchk(FTSENT *); | ||||
static void usage(void); | static void usage(void); | ||||
static void prthumanval(int64_t); | static void prthumanval(int64_t); | ||||
static void ignoreadd(const char *); | static void ignoreadd(const char *); | ||||
static void ignoreclean(void); | static void ignoreclean(void); | ||||
static int ignorep(FTSENT *); | static int ignorep(FTSENT *); | ||||
static void siginfo(int __unused); | static void siginfo(int __unused); | ||||
static int nodumpflag = 0; | static int nodumpflag = 0; | ||||
static int Aflag; | static int Aflag, hflag; | ||||
static long blocksize, cblocksize; | static long blocksize, cblocksize; | ||||
static volatile sig_atomic_t info; | static volatile sig_atomic_t info; | ||||
static const struct option long_options[] = | |||||
{ | |||||
{ "si", no_argument, NULL, SI_OPT }, | |||||
{ NULL, no_argument, NULL, 0 }, | |||||
}; | |||||
int | int | ||||
main(int argc, char *argv[]) | main(int argc, char *argv[]) | ||||
{ | { | ||||
FTS *fts; | FTS *fts; | ||||
FTSENT *p; | FTSENT *p; | ||||
off_t savednumber, curblocks; | off_t savednumber, curblocks; | ||||
off_t threshold, threshold_sign; | off_t threshold, threshold_sign; | ||||
int ftsoptions; | int ftsoptions; | ||||
int depth; | int depth; | ||||
int Hflag, Lflag, aflag, sflag, dflag, cflag; | int Hflag, Lflag, aflag, sflag, dflag, cflag; | ||||
int hflag, lflag, ch, notused, rval; | int lflag, ch, notused, rval; | ||||
char **save; | char **save; | ||||
static char dot[] = "."; | static char dot[] = "."; | ||||
setlocale(LC_ALL, ""); | setlocale(LC_ALL, ""); | ||||
Hflag = Lflag = aflag = sflag = dflag = cflag = hflag = | Hflag = Lflag = aflag = sflag = dflag = cflag = lflag = Aflag = 0; | ||||
lflag = Aflag = 0; | |||||
save = argv; | save = argv; | ||||
ftsoptions = FTS_PHYSICAL; | ftsoptions = FTS_PHYSICAL; | ||||
savednumber = 0; | savednumber = 0; | ||||
threshold = 0; | threshold = 0; | ||||
threshold_sign = 1; | threshold_sign = 1; | ||||
cblocksize = DEV_BSIZE; | cblocksize = DEV_BSIZE; | ||||
blocksize = 0; | blocksize = 0; | ||||
depth = INT_MAX; | depth = INT_MAX; | ||||
SLIST_INIT(&ignores); | 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) { | switch (ch) { | ||||
case 'A': | case 'A': | ||||
Aflag = 1; | Aflag = 1; | ||||
break; | break; | ||||
case 'B': | case 'B': | ||||
errno = 0; | errno = 0; | ||||
cblocksize = atoi(optarg); | cblocksize = atoi(optarg); | ||||
if (errno == ERANGE || cblocksize <= 0) { | if (errno == ERANGE || cblocksize <= 0) { | ||||
Show All 35 Lines | while ((ch = getopt_long(argc, argv, "+AB:HI:LPasd:cghklmnrt:x", | ||||
case 'c': | case 'c': | ||||
cflag = 1; | cflag = 1; | ||||
break; | break; | ||||
case 'g': | case 'g': | ||||
hflag = 0; | hflag = 0; | ||||
blocksize = 1073741824; | blocksize = 1073741824; | ||||
break; | break; | ||||
case 'h': | case 'h': | ||||
hflag = 1; | hflag = UNITS_2; | ||||
break; | break; | ||||
case 'k': | case 'k': | ||||
hflag = 0; | hflag = 0; | ||||
blocksize = 1024; | blocksize = 1024; | ||||
break; | break; | ||||
case 'l': | case 'l': | ||||
lflag = 1; | lflag = 1; | ||||
break; | break; | ||||
Show All 12 Lines | case 't' : | ||||
warnx("invalid threshold: %s", optarg); | warnx("invalid threshold: %s", optarg); | ||||
usage(); | usage(); | ||||
} else if (threshold < 0) | } else if (threshold < 0) | ||||
threshold_sign = -1; | threshold_sign = -1; | ||||
break; | break; | ||||
case 'x': | case 'x': | ||||
ftsoptions |= FTS_XDEV; | ftsoptions |= FTS_XDEV; | ||||
break; | break; | ||||
case SI_OPT: | |||||
hflag = UNITS_SI; | |||||
break; | |||||
case '?': | case '?': | ||||
default: | default: | ||||
usage(); | usage(); | ||||
/* NOTREACHED */ | /* NOTREACHED */ | ||||
} | } | ||||
argc -= optind; | argc -= optind; | ||||
argv += optind; | argv += optind; | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | case FTS_DP: | ||||
howmany(p->fts_statp->st_size, cblocksize) : | howmany(p->fts_statp->st_size, cblocksize) : | ||||
howmany(p->fts_statp->st_blocks, cblocksize); | howmany(p->fts_statp->st_blocks, cblocksize); | ||||
p->fts_parent->fts_bignum += p->fts_bignum += | p->fts_parent->fts_bignum += p->fts_bignum += | ||||
curblocks; | curblocks; | ||||
if (p->fts_level <= depth && threshold <= | if (p->fts_level <= depth && threshold <= | ||||
threshold_sign * howmany(p->fts_bignum * | threshold_sign * howmany(p->fts_bignum * | ||||
cblocksize, blocksize)) { | cblocksize, blocksize)) { | ||||
if (hflag) { | if (hflag > 0) { | ||||
prthumanval(p->fts_bignum); | prthumanval(p->fts_bignum); | ||||
(void)printf("\t%s\n", p->fts_path); | (void)printf("\t%s\n", p->fts_path); | ||||
} else { | } else { | ||||
(void)printf("%jd\t%s\n", | (void)printf("%jd\t%s\n", | ||||
(intmax_t)howmany(p->fts_bignum * | (intmax_t)howmany(p->fts_bignum * | ||||
cblocksize, blocksize), | cblocksize, blocksize), | ||||
p->fts_path); | p->fts_path); | ||||
} | } | ||||
Show All 19 Lines | default: | ||||
linkchk(p)) | linkchk(p)) | ||||
break; | break; | ||||
curblocks = Aflag ? | curblocks = Aflag ? | ||||
howmany(p->fts_statp->st_size, cblocksize) : | howmany(p->fts_statp->st_size, cblocksize) : | ||||
howmany(p->fts_statp->st_blocks, cblocksize); | howmany(p->fts_statp->st_blocks, cblocksize); | ||||
if (aflag || p->fts_level == 0) { | if (aflag || p->fts_level == 0) { | ||||
if (hflag) { | if (hflag > 0) { | ||||
prthumanval(curblocks); | prthumanval(curblocks); | ||||
(void)printf("\t%s\n", p->fts_path); | (void)printf("\t%s\n", p->fts_path); | ||||
} else { | } else { | ||||
(void)printf("%jd\t%s\n", | (void)printf("%jd\t%s\n", | ||||
(intmax_t)howmany(curblocks * | (intmax_t)howmany(curblocks * | ||||
cblocksize, blocksize), | cblocksize, blocksize), | ||||
p->fts_path); | p->fts_path); | ||||
} | } | ||||
} | } | ||||
p->fts_parent->fts_bignum += curblocks; | p->fts_parent->fts_bignum += curblocks; | ||||
} | } | ||||
savednumber = p->fts_parent->fts_bignum; | savednumber = p->fts_parent->fts_bignum; | ||||
} | } | ||||
if (errno) | if (errno) | ||||
err(1, "fts_read"); | err(1, "fts_read"); | ||||
if (cflag) { | if (cflag) { | ||||
if (hflag) { | if (hflag > 0) { | ||||
prthumanval(savednumber); | prthumanval(savednumber); | ||||
(void)printf("\ttotal\n"); | (void)printf("\ttotal\n"); | ||||
} else { | } else { | ||||
(void)printf("%jd\ttotal\n", (intmax_t)howmany( | (void)printf("%jd\ttotal\n", (intmax_t)howmany( | ||||
savednumber * cblocksize, blocksize)); | savednumber * cblocksize, blocksize)); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 131 Lines • ▼ Show 20 Lines | linkchk(FTSENT *p) | ||||
buckets[hash] = le; | buckets[hash] = le; | ||||
return (0); | return (0); | ||||
} | } | ||||
static void | static void | ||||
prthumanval(int64_t bytes) | prthumanval(int64_t bytes) | ||||
{ | { | ||||
char buf[5]; | char buf[5]; | ||||
int flags; | |||||
bytes *= cblocksize; | bytes *= cblocksize; | ||||
flags = HN_B | HN_NOSPACE | HN_DECIMAL; | |||||
if (!Aflag) | if (!Aflag) | ||||
bytes *= DEV_BSIZE; | bytes *= DEV_BSIZE; | ||||
if (hflag == UNITS_SI) | |||||
flags |= HN_DIVISOR_1000; | |||||
humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE, | humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE, flags); | ||||
HN_B | HN_NOSPACE | HN_DECIMAL); | |||||
(void)printf("%4s", buf); | (void)printf("%4s", buf); | ||||
} | } | ||||
static void | static void | ||||
usage(void) | usage(void) | ||||
{ | { | ||||
(void)fprintf(stderr, | (void)fprintf(stderr, | ||||
▲ Show 20 Lines • Show All 52 Lines • Show Last 20 Lines |