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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 #include <sys/param.h>
52 #include <sys/queue.h>
68 SLIST_HEAD(ignhead, ignentry) ignores;
71 SLIST_ENTRY(ignentry) next;
74 static int linkchk(FTSENT *);
75 static void usage(void);
76 static void prthumanval(int64_t);
77 static void ignoreadd(const char *);
78 static void ignoreclean(void);
79 static int ignorep(FTSENT *);
80 static void siginfo(int __unused);
82 static int nodumpflag = 0;
84 static long blocksize, cblocksize;
85 static volatile sig_atomic_t info;
88 main(int argc, char *argv[])
92 off_t savednumber, curblocks;
96 int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag;
97 int hflag, lflag, ch, notused, rval;
99 static char dot[] = ".";
101 setlocale(LC_ALL, "");
103 Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag =
109 cblocksize = DEV_BSIZE;
112 SLIST_INIT(&ignores);
114 while ((ch = getopt(argc, argv, "AB:HI:LPasd:chklmnrx")) != -1)
121 cblocksize = atoi(optarg);
122 if (errno == ERANGE || cblocksize <= 0) {
123 warnx("invalid argument to option B: %s",
153 depth = atoi(optarg);
154 if (errno == ERANGE || depth < 0) {
155 warnx("invalid argument to option d: %s",
180 case 'r': /* Compatibility. */
183 ftsoptions |= FTS_XDEV;
196 * Because of the way that fts(3) works, logical walks will not count
197 * the blocks actually used by symbolic links. We rationalize this by
198 * noting that users computing logical sizes are likely to do logical
199 * copies, so not counting the links is correct. The real reason is
200 * that we'd have to re-implement the kernel's symbolic link traversing
201 * algorithm to get this right. If, for example, you have relative
202 * symbolic links referencing other relative symbolic links, it gets
203 * very nasty, very fast. The bottom line is that it's documented in
204 * the man page, so it's a feature.
207 if (Hflag + Lflag + Pflag > 1)
210 if (Hflag + Lflag + Pflag == 0)
211 Pflag = 1; /* -P (physical) is default */
214 ftsoptions |= FTS_COMFOLLOW;
217 ftsoptions |= FTS_LOGICAL;
220 ftsoptions |= FTS_PHYSICAL;
222 if (!Aflag && (cblocksize % DEV_BSIZE) != 0)
223 cblocksize = howmany(cblocksize, DEV_BSIZE) * DEV_BSIZE;
244 (void)getbsize(¬used, &blocksize);
247 cblocksize /= DEV_BSIZE;
248 blocksize /= DEV_BSIZE;
253 (void)signal(SIGINFO, siginfo);
255 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
258 while ((p = fts_read(fts)) != NULL) {
259 switch (p->fts_info) {
260 case FTS_D: /* Ignore. */
262 fts_set(fts, p, FTS_SKIP);
269 howmany(p->fts_statp->st_size, cblocksize) :
270 howmany(p->fts_statp->st_blocks, cblocksize);
271 p->fts_parent->fts_bignum += p->fts_bignum +=
274 if (p->fts_level <= depth) {
276 prthumanval(p->fts_bignum);
277 (void)printf("\t%s\n", p->fts_path);
279 (void)printf("%jd\t%s\n",
280 (intmax_t)howmany(p->fts_bignum *
281 cblocksize, blocksize),
287 (void)printf("\t%s\n", p->fts_path);
290 case FTS_DC: /* Ignore. */
292 case FTS_DNR: /* Warn, continue. */
295 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
302 if (lflag == 0 && p->fts_statp->st_nlink > 1 &&
307 howmany(p->fts_statp->st_size, cblocksize) :
308 howmany(p->fts_statp->st_blocks, cblocksize);
310 if (listall || p->fts_level == 0) {
312 prthumanval(curblocks);
313 (void)printf("\t%s\n", p->fts_path);
315 (void)printf("%jd\t%s\n",
316 (intmax_t)howmany(curblocks *
317 cblocksize, blocksize),
322 p->fts_parent->fts_bignum += curblocks;
324 savednumber = p->fts_parent->fts_bignum;
332 prthumanval(savednumber);
333 (void)printf("\ttotal\n");
335 (void)printf("%jd\ttotal\n", (intmax_t)howmany(
336 savednumber * cblocksize, blocksize));
348 struct links_entry *next;
349 struct links_entry *previous;
354 static const size_t links_hash_initial_size = 8192;
355 static struct links_entry **buckets;
356 static struct links_entry *free_list;
357 static size_t number_buckets;
358 static unsigned long number_entries;
359 static char stop_allocating;
360 struct links_entry *le, **new_buckets;
367 /* If necessary, initialize the hash table. */
368 if (buckets == NULL) {
369 number_buckets = links_hash_initial_size;
370 buckets = malloc(number_buckets * sizeof(buckets[0]));
372 errx(1, "No memory for hardlink detection");
373 for (i = 0; i < number_buckets; i++)
377 /* If the hash table is getting too full, enlarge it. */
378 if (number_entries > number_buckets * 10 && !stop_allocating) {
379 new_size = number_buckets * 2;
380 new_buckets = malloc(new_size * sizeof(struct links_entry *));
382 /* Try releasing the free list to see if that helps. */
383 if (new_buckets == NULL && free_list != NULL) {
384 while (free_list != NULL) {
386 free_list = le->next;
389 new_buckets = malloc(new_size *
390 sizeof(new_buckets[0]));
393 if (new_buckets == NULL) {
395 warnx("No more memory for tracking hard links");
397 memset(new_buckets, 0,
398 new_size * sizeof(struct links_entry *));
399 for (i = 0; i < number_buckets; i++) {
400 while (buckets[i] != NULL) {
401 /* Remove entry from old bucket. */
403 buckets[i] = le->next;
405 /* Add entry to new bucket. */
406 hash = (le->dev ^ le->ino) % new_size;
408 if (new_buckets[hash] != NULL)
409 new_buckets[hash]->previous =
411 le->next = new_buckets[hash];
413 new_buckets[hash] = le;
417 buckets = new_buckets;
418 number_buckets = new_size;
422 /* Try to locate this entry in the hash table. */
423 hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
424 for (le = buckets[hash]; le != NULL; le = le->next) {
425 if (le->dev == st->st_dev && le->ino == st->st_ino) {
427 * Save memory by releasing an entry when we've seen
430 if (--le->links <= 0) {
431 if (le->previous != NULL)
432 le->previous->next = le->next;
433 if (le->next != NULL)
434 le->next->previous = le->previous;
435 if (buckets[hash] == le)
436 buckets[hash] = le->next;
438 /* Recycle this node through the free list */
439 if (stop_allocating) {
442 le->next = free_list;
453 /* Add this entry to the links cache. */
454 if (free_list != NULL) {
455 /* Pull a node from the free list if we can. */
457 free_list = le->next;
459 /* Malloc one if we have to. */
460 le = malloc(sizeof(struct links_entry));
463 warnx("No more memory for tracking hard links");
466 le->dev = st->st_dev;
467 le->ino = st->st_ino;
468 le->links = st->st_nlink - 1;
470 le->next = buckets[hash];
472 if (buckets[hash] != NULL)
473 buckets[hash]->previous = le;
479 prthumanval(int64_t bytes)
487 humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
488 HN_B | HN_NOSPACE | HN_DECIMAL);
490 (void)printf("%4s", buf);
496 (void)fprintf(stderr,
497 "usage: du [-A] [-H | -L | -P] [-a | -s | -d depth] [-c] "
498 "[-l] [-h | -k | -m | -B bsize] [-n] [-x] [-I mask] "
504 ignoreadd(const char *mask)
506 struct ignentry *ign;
508 ign = calloc(1, sizeof(*ign));
510 errx(1, "cannot allocate memory");
511 ign->mask = strdup(mask);
512 if (ign->mask == NULL)
513 errx(1, "cannot allocate memory");
514 SLIST_INSERT_HEAD(&ignores, ign, next);
520 struct ignentry *ign;
522 while (!SLIST_EMPTY(&ignores)) {
523 ign = SLIST_FIRST(&ignores);
524 SLIST_REMOVE_HEAD(&ignores, next);
533 struct ignentry *ign;
535 if (nodumpflag && (ent->fts_statp->st_flags & UF_NODUMP))
537 SLIST_FOREACH(ign, &ignores, next)
538 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
544 siginfo(int sig __unused)