2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1989, 1993, 1994\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/queue.h>
64 #define SI_OPT (CHAR_MAX + 1)
69 static SLIST_HEAD(ignhead, ignentry) ignores;
72 SLIST_ENTRY(ignentry) next;
75 static int linkchk(FTSENT *);
76 static void usage(void);
77 static void prthumanval(int64_t);
78 static void ignoreadd(const char *);
79 static void ignoreclean(void);
80 static int ignorep(FTSENT *);
81 static void siginfo(int __unused);
83 static int nodumpflag = 0;
84 static int Aflag, hflag;
85 static long blocksize, cblocksize;
86 static volatile sig_atomic_t info;
88 static const struct option long_options[] =
90 { "si", no_argument, NULL, SI_OPT },
91 { NULL, no_argument, NULL, 0 },
95 main(int argc, char *argv[])
99 off_t savednumber, curblocks;
100 off_t threshold, threshold_sign;
103 int Hflag, Lflag, aflag, sflag, dflag, cflag;
104 int lflag, ch, notused, rval;
106 static char dot[] = ".";
108 setlocale(LC_ALL, "");
110 Hflag = Lflag = aflag = sflag = dflag = cflag = lflag = Aflag = 0;
113 ftsoptions = FTS_PHYSICAL;
117 cblocksize = DEV_BSIZE;
120 SLIST_INIT(&ignores);
122 while ((ch = getopt_long(argc, argv, "+AB:HI:LPasd:cghklmnrt:x",
123 long_options, NULL)) != -1)
130 cblocksize = atoi(optarg);
131 if (errno == ERANGE || cblocksize <= 0) {
132 warnx("invalid argument to option B: %s",
160 depth = atoi(optarg);
161 if (errno == ERANGE || depth < 0) {
162 warnx("invalid argument to option d: %s",
172 blocksize = 1073741824;
191 case 'r': /* Compatibility. */
194 if (expand_number(optarg, &threshold) != 0 ||
196 warnx("invalid threshold: %s", optarg);
198 } else if (threshold < 0)
202 ftsoptions |= FTS_XDEV;
218 * Because of the way that fts(3) works, logical walks will not count
219 * the blocks actually used by symbolic links. We rationalize this by
220 * noting that users computing logical sizes are likely to do logical
221 * copies, so not counting the links is correct. The real reason is
222 * that we'd have to re-implement the kernel's symbolic link traversing
223 * algorithm to get this right. If, for example, you have relative
224 * symbolic links referencing other relative symbolic links, it gets
225 * very nasty, very fast. The bottom line is that it's documented in
226 * the man page, so it's a feature.
230 ftsoptions |= FTS_COMFOLLOW;
232 ftsoptions &= ~FTS_PHYSICAL;
233 ftsoptions |= FTS_LOGICAL;
236 if (!Aflag && (cblocksize % DEV_BSIZE) != 0)
237 cblocksize = howmany(cblocksize, DEV_BSIZE) * DEV_BSIZE;
239 if (aflag + dflag + sflag > 1)
251 (void)getbsize(¬used, &blocksize);
254 cblocksize /= DEV_BSIZE;
255 blocksize /= DEV_BSIZE;
259 threshold = howmany(threshold / DEV_BSIZE * cblocksize,
264 (void)signal(SIGINFO, siginfo);
266 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
269 while ((p = fts_read(fts)) != NULL) {
270 switch (p->fts_info) {
271 case FTS_D: /* Ignore. */
273 fts_set(fts, p, FTS_SKIP);
280 howmany(p->fts_statp->st_size, cblocksize) :
281 howmany(p->fts_statp->st_blocks, cblocksize);
282 p->fts_parent->fts_bignum += p->fts_bignum +=
285 if (p->fts_level <= depth && threshold <=
286 threshold_sign * howmany(p->fts_bignum *
287 cblocksize, blocksize)) {
289 prthumanval(p->fts_bignum);
290 (void)printf("\t%s\n", p->fts_path);
292 (void)printf("%jd\t%s\n",
293 (intmax_t)howmany(p->fts_bignum *
294 cblocksize, blocksize),
300 (void)printf("\t%s\n", p->fts_path);
303 case FTS_DC: /* Ignore. */
305 case FTS_DNR: /* Warn, continue. */
308 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
315 if (lflag == 0 && p->fts_statp->st_nlink > 1 &&
320 howmany(p->fts_statp->st_size, cblocksize) :
321 howmany(p->fts_statp->st_blocks, cblocksize);
323 if (aflag || p->fts_level == 0) {
325 prthumanval(curblocks);
326 (void)printf("\t%s\n", p->fts_path);
328 (void)printf("%jd\t%s\n",
329 (intmax_t)howmany(curblocks *
330 cblocksize, blocksize),
335 p->fts_parent->fts_bignum += curblocks;
337 savednumber = p->fts_parent->fts_bignum;
345 prthumanval(savednumber);
346 (void)printf("\ttotal\n");
348 (void)printf("%jd\ttotal\n", (intmax_t)howmany(
349 savednumber * cblocksize, blocksize));
361 struct links_entry *next;
362 struct links_entry *previous;
367 static const size_t links_hash_initial_size = 8192;
368 static struct links_entry **buckets;
369 static struct links_entry *free_list;
370 static size_t number_buckets;
371 static unsigned long number_entries;
372 static char stop_allocating;
373 struct links_entry *le, **new_buckets;
380 /* If necessary, initialize the hash table. */
381 if (buckets == NULL) {
382 number_buckets = links_hash_initial_size;
383 buckets = malloc(number_buckets * sizeof(buckets[0]));
385 errx(1, "No memory for hardlink detection");
386 for (i = 0; i < number_buckets; i++)
390 /* If the hash table is getting too full, enlarge it. */
391 if (number_entries > number_buckets * 10 && !stop_allocating) {
392 new_size = number_buckets * 2;
393 new_buckets = calloc(new_size, sizeof(struct links_entry *));
395 /* Try releasing the free list to see if that helps. */
396 if (new_buckets == NULL && free_list != NULL) {
397 while (free_list != NULL) {
399 free_list = le->next;
402 new_buckets = calloc(new_size, sizeof(new_buckets[0]));
405 if (new_buckets == NULL) {
407 warnx("No more memory for tracking hard links");
409 for (i = 0; i < number_buckets; i++) {
410 while (buckets[i] != NULL) {
411 /* Remove entry from old bucket. */
413 buckets[i] = le->next;
415 /* Add entry to new bucket. */
416 hash = (le->dev ^ le->ino) % new_size;
418 if (new_buckets[hash] != NULL)
419 new_buckets[hash]->previous =
421 le->next = new_buckets[hash];
423 new_buckets[hash] = le;
427 buckets = new_buckets;
428 number_buckets = new_size;
432 /* Try to locate this entry in the hash table. */
433 hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
434 for (le = buckets[hash]; le != NULL; le = le->next) {
435 if (le->dev == st->st_dev && le->ino == st->st_ino) {
437 * Save memory by releasing an entry when we've seen
440 if (--le->links <= 0) {
441 if (le->previous != NULL)
442 le->previous->next = le->next;
443 if (le->next != NULL)
444 le->next->previous = le->previous;
445 if (buckets[hash] == le)
446 buckets[hash] = le->next;
448 /* Recycle this node through the free list */
449 if (stop_allocating) {
452 le->next = free_list;
463 /* Add this entry to the links cache. */
464 if (free_list != NULL) {
465 /* Pull a node from the free list if we can. */
467 free_list = le->next;
469 /* Malloc one if we have to. */
470 le = malloc(sizeof(struct links_entry));
473 warnx("No more memory for tracking hard links");
476 le->dev = st->st_dev;
477 le->ino = st->st_ino;
478 le->links = st->st_nlink - 1;
480 le->next = buckets[hash];
482 if (buckets[hash] != NULL)
483 buckets[hash]->previous = le;
489 prthumanval(int64_t bytes)
495 flags = HN_B | HN_NOSPACE | HN_DECIMAL;
498 if (hflag == UNITS_SI)
499 flags |= HN_DIVISOR_1000;
501 humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE, flags);
503 (void)printf("%4s", buf);
509 (void)fprintf(stderr,
510 "usage: du [-Aclnx] [-H | -L | -P] [-g | -h | -k | -m] "
511 "[-a | -s | -d depth] [-B blocksize] [-I mask] "
512 "[-t threshold] [file ...]\n");
517 ignoreadd(const char *mask)
519 struct ignentry *ign;
521 ign = calloc(1, sizeof(*ign));
523 errx(1, "cannot allocate memory");
524 ign->mask = strdup(mask);
525 if (ign->mask == NULL)
526 errx(1, "cannot allocate memory");
527 SLIST_INSERT_HEAD(&ignores, ign, next);
533 struct ignentry *ign;
535 while (!SLIST_EMPTY(&ignores)) {
536 ign = SLIST_FIRST(&ignores);
537 SLIST_REMOVE_HEAD(&ignores, next);
546 struct ignentry *ign;
548 if (nodumpflag && (ent->fts_statp->st_flags & UF_NODUMP))
550 SLIST_FOREACH(ign, &ignores, next)
551 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
557 siginfo(int sig __unused)