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>
63 static SLIST_HEAD(ignhead, ignentry) ignores;
66 SLIST_ENTRY(ignentry) next;
69 static int linkchk(FTSENT *);
70 static void usage(void);
71 static void prthumanval(int64_t);
72 static void ignoreadd(const char *);
73 static void ignoreclean(void);
74 static int ignorep(FTSENT *);
75 static void siginfo(int __unused);
77 static int nodumpflag = 0;
79 static long blocksize, cblocksize;
80 static volatile sig_atomic_t info;
83 main(int argc, char *argv[])
87 off_t savednumber, curblocks;
88 off_t threshold, threshold_sign;
91 int Hflag, Lflag, aflag, sflag, dflag, cflag;
92 int hflag, lflag, ch, notused, rval;
94 static char dot[] = ".";
96 setlocale(LC_ALL, "");
98 Hflag = Lflag = aflag = sflag = dflag = cflag = hflag =
102 ftsoptions = FTS_PHYSICAL;
106 cblocksize = DEV_BSIZE;
109 SLIST_INIT(&ignores);
111 while ((ch = getopt(argc, argv, "AB:HI:LPasd:cghklmnrt:x")) != -1)
118 cblocksize = atoi(optarg);
119 if (errno == ERANGE || cblocksize <= 0) {
120 warnx("invalid argument to option B: %s",
148 depth = atoi(optarg);
149 if (errno == ERANGE || depth < 0) {
150 warnx("invalid argument to option d: %s",
160 blocksize = 1073741824;
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.
215 ftsoptions |= FTS_COMFOLLOW;
217 ftsoptions &= ~FTS_PHYSICAL;
218 ftsoptions |= FTS_LOGICAL;
221 if (!Aflag && (cblocksize % DEV_BSIZE) != 0)
222 cblocksize = howmany(cblocksize, DEV_BSIZE) * DEV_BSIZE;
224 if (aflag + dflag + sflag > 1)
236 (void)getbsize(¬used, &blocksize);
239 cblocksize /= DEV_BSIZE;
240 blocksize /= DEV_BSIZE;
244 threshold = howmany(threshold / DEV_BSIZE * cblocksize,
249 (void)signal(SIGINFO, siginfo);
251 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
254 while ((p = fts_read(fts)) != NULL) {
255 switch (p->fts_info) {
256 case FTS_D: /* Ignore. */
258 fts_set(fts, p, FTS_SKIP);
265 howmany(p->fts_statp->st_size, cblocksize) :
266 howmany(p->fts_statp->st_blocks, cblocksize);
267 p->fts_parent->fts_bignum += p->fts_bignum +=
270 if (p->fts_level <= depth && threshold <=
271 threshold_sign * howmany(p->fts_bignum *
272 cblocksize, blocksize)) {
274 prthumanval(p->fts_bignum);
275 (void)printf("\t%s\n", p->fts_path);
277 (void)printf("%jd\t%s\n",
278 (intmax_t)howmany(p->fts_bignum *
279 cblocksize, blocksize),
285 (void)printf("\t%s\n", p->fts_path);
288 case FTS_DC: /* Ignore. */
290 case FTS_DNR: /* Warn, continue. */
293 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
300 if (lflag == 0 && p->fts_statp->st_nlink > 1 &&
305 howmany(p->fts_statp->st_size, cblocksize) :
306 howmany(p->fts_statp->st_blocks, cblocksize);
308 if (aflag || p->fts_level == 0) {
310 prthumanval(curblocks);
311 (void)printf("\t%s\n", p->fts_path);
313 (void)printf("%jd\t%s\n",
314 (intmax_t)howmany(curblocks *
315 cblocksize, blocksize),
320 p->fts_parent->fts_bignum += curblocks;
322 savednumber = p->fts_parent->fts_bignum;
330 prthumanval(savednumber);
331 (void)printf("\ttotal\n");
333 (void)printf("%jd\ttotal\n", (intmax_t)howmany(
334 savednumber * cblocksize, blocksize));
346 struct links_entry *next;
347 struct links_entry *previous;
352 static const size_t links_hash_initial_size = 8192;
353 static struct links_entry **buckets;
354 static struct links_entry *free_list;
355 static size_t number_buckets;
356 static unsigned long number_entries;
357 static char stop_allocating;
358 struct links_entry *le, **new_buckets;
365 /* If necessary, initialize the hash table. */
366 if (buckets == NULL) {
367 number_buckets = links_hash_initial_size;
368 buckets = malloc(number_buckets * sizeof(buckets[0]));
370 errx(1, "No memory for hardlink detection");
371 for (i = 0; i < number_buckets; i++)
375 /* If the hash table is getting too full, enlarge it. */
376 if (number_entries > number_buckets * 10 && !stop_allocating) {
377 new_size = number_buckets * 2;
378 new_buckets = malloc(new_size * sizeof(struct links_entry *));
380 /* Try releasing the free list to see if that helps. */
381 if (new_buckets == NULL && free_list != NULL) {
382 while (free_list != NULL) {
384 free_list = le->next;
387 new_buckets = malloc(new_size *
388 sizeof(new_buckets[0]));
391 if (new_buckets == NULL) {
393 warnx("No more memory for tracking hard links");
395 memset(new_buckets, 0,
396 new_size * sizeof(struct links_entry *));
397 for (i = 0; i < number_buckets; i++) {
398 while (buckets[i] != NULL) {
399 /* Remove entry from old bucket. */
401 buckets[i] = le->next;
403 /* Add entry to new bucket. */
404 hash = (le->dev ^ le->ino) % new_size;
406 if (new_buckets[hash] != NULL)
407 new_buckets[hash]->previous =
409 le->next = new_buckets[hash];
411 new_buckets[hash] = le;
415 buckets = new_buckets;
416 number_buckets = new_size;
420 /* Try to locate this entry in the hash table. */
421 hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
422 for (le = buckets[hash]; le != NULL; le = le->next) {
423 if (le->dev == st->st_dev && le->ino == st->st_ino) {
425 * Save memory by releasing an entry when we've seen
428 if (--le->links <= 0) {
429 if (le->previous != NULL)
430 le->previous->next = le->next;
431 if (le->next != NULL)
432 le->next->previous = le->previous;
433 if (buckets[hash] == le)
434 buckets[hash] = le->next;
436 /* Recycle this node through the free list */
437 if (stop_allocating) {
440 le->next = free_list;
451 /* Add this entry to the links cache. */
452 if (free_list != NULL) {
453 /* Pull a node from the free list if we can. */
455 free_list = le->next;
457 /* Malloc one if we have to. */
458 le = malloc(sizeof(struct links_entry));
461 warnx("No more memory for tracking hard links");
464 le->dev = st->st_dev;
465 le->ino = st->st_ino;
466 le->links = st->st_nlink - 1;
468 le->next = buckets[hash];
470 if (buckets[hash] != NULL)
471 buckets[hash]->previous = le;
477 prthumanval(int64_t bytes)
485 humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
486 HN_B | HN_NOSPACE | HN_DECIMAL);
488 (void)printf("%4s", buf);
494 (void)fprintf(stderr,
495 "usage: du [-Aclnx] [-H | -L | -P] [-g | -h | -k | -m] "
496 "[-a | -s | -d depth] [-B blocksize] [-I mask] "
497 "[-t threshold] [file ...]\n");
502 ignoreadd(const char *mask)
504 struct ignentry *ign;
506 ign = calloc(1, sizeof(*ign));
508 errx(1, "cannot allocate memory");
509 ign->mask = strdup(mask);
510 if (ign->mask == NULL)
511 errx(1, "cannot allocate memory");
512 SLIST_INSERT_HEAD(&ignores, ign, next);
518 struct ignentry *ign;
520 while (!SLIST_EMPTY(&ignores)) {
521 ign = SLIST_FIRST(&ignores);
522 SLIST_REMOVE_HEAD(&ignores, next);
531 struct ignentry *ign;
533 if (nodumpflag && (ent->fts_statp->st_flags & UF_NODUMP))
535 SLIST_FOREACH(ign, &ignores, next)
536 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
542 siginfo(int sig __unused)