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 SLIST_HEAD(ignhead, ignentry) ignores;
67 SLIST_ENTRY(ignentry) next;
70 static int linkchk(FTSENT *);
71 static void usage(void);
72 static void prthumanval(int64_t);
73 static void ignoreadd(const char *);
74 static void ignoreclean(void);
75 static int ignorep(FTSENT *);
76 static void siginfo(int __unused);
78 static int nodumpflag = 0;
80 static long blocksize, cblocksize;
81 static volatile sig_atomic_t info;
84 main(int argc, char *argv[])
88 off_t savednumber, curblocks;
89 off_t threshold, threshold_sign;
93 int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag;
94 int hflag, lflag, ch, notused, rval;
96 static char dot[] = ".";
98 setlocale(LC_ALL, "");
100 Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag =
108 cblocksize = DEV_BSIZE;
111 SLIST_INIT(&ignores);
113 while ((ch = getopt(argc, argv, "AB:HI:LPasd:chklmnrt:x")) != -1)
120 cblocksize = atoi(optarg);
121 if (errno == ERANGE || cblocksize <= 0) {
122 warnx("invalid argument to option B: %s",
152 depth = atoi(optarg);
153 if (errno == ERANGE || depth < 0) {
154 warnx("invalid argument to option d: %s",
179 case 'r': /* Compatibility. */
182 if (expand_number(optarg, &threshold) != 0 ||
184 warnx("invalid threshold: %s", optarg);
186 } else if (threshold < 0)
190 ftsoptions |= FTS_XDEV;
203 * Because of the way that fts(3) works, logical walks will not count
204 * the blocks actually used by symbolic links. We rationalize this by
205 * noting that users computing logical sizes are likely to do logical
206 * copies, so not counting the links is correct. The real reason is
207 * that we'd have to re-implement the kernel's symbolic link traversing
208 * algorithm to get this right. If, for example, you have relative
209 * symbolic links referencing other relative symbolic links, it gets
210 * very nasty, very fast. The bottom line is that it's documented in
211 * the man page, so it's a feature.
214 if (Hflag + Lflag + Pflag > 1)
217 if (Hflag + Lflag + Pflag == 0)
218 Pflag = 1; /* -P (physical) is default */
221 ftsoptions |= FTS_COMFOLLOW;
224 ftsoptions |= FTS_LOGICAL;
227 ftsoptions |= FTS_PHYSICAL;
229 if (!Aflag && (cblocksize % DEV_BSIZE) != 0)
230 cblocksize = howmany(cblocksize, DEV_BSIZE) * DEV_BSIZE;
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 (listall || 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 = malloc(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 = malloc(new_size *
403 sizeof(new_buckets[0]));
406 if (new_buckets == NULL) {
408 warnx("No more memory for tracking hard links");
410 memset(new_buckets, 0,
411 new_size * sizeof(struct links_entry *));
412 for (i = 0; i < number_buckets; i++) {
413 while (buckets[i] != NULL) {
414 /* Remove entry from old bucket. */
416 buckets[i] = le->next;
418 /* Add entry to new bucket. */
419 hash = (le->dev ^ le->ino) % new_size;
421 if (new_buckets[hash] != NULL)
422 new_buckets[hash]->previous =
424 le->next = new_buckets[hash];
426 new_buckets[hash] = le;
430 buckets = new_buckets;
431 number_buckets = new_size;
435 /* Try to locate this entry in the hash table. */
436 hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
437 for (le = buckets[hash]; le != NULL; le = le->next) {
438 if (le->dev == st->st_dev && le->ino == st->st_ino) {
440 * Save memory by releasing an entry when we've seen
443 if (--le->links <= 0) {
444 if (le->previous != NULL)
445 le->previous->next = le->next;
446 if (le->next != NULL)
447 le->next->previous = le->previous;
448 if (buckets[hash] == le)
449 buckets[hash] = le->next;
451 /* Recycle this node through the free list */
452 if (stop_allocating) {
455 le->next = free_list;
466 /* Add this entry to the links cache. */
467 if (free_list != NULL) {
468 /* Pull a node from the free list if we can. */
470 free_list = le->next;
472 /* Malloc one if we have to. */
473 le = malloc(sizeof(struct links_entry));
476 warnx("No more memory for tracking hard links");
479 le->dev = st->st_dev;
480 le->ino = st->st_ino;
481 le->links = st->st_nlink - 1;
483 le->next = buckets[hash];
485 if (buckets[hash] != NULL)
486 buckets[hash]->previous = le;
492 prthumanval(int64_t bytes)
500 humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
501 HN_B | HN_NOSPACE | HN_DECIMAL);
503 (void)printf("%4s", buf);
509 (void)fprintf(stderr,
510 "usage: du [-A] [-H | -L | -P] [-a | -s | -d depth] [-c] "
511 "[-l] [-h | -k | -m | -B bsize] [-n] [-x] [-I mask] "
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)