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 void prthumanval(int64_t);
77 void ignoreadd(const char *);
78 void ignoreclean(void);
79 int ignorep(FTSENT *);
84 main(int argc, char *argv[])
88 off_t savednumber = 0;
93 int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval;
95 static char dot[] = ".";
97 setlocale(LC_ALL, "");
99 Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0;
104 SLIST_INIT(&ignores);
106 while ((ch = getopt(argc, argv, "HI:LPasd:chkmnrx")) != -1)
133 depth = atoi(optarg);
134 if (errno == ERANGE || depth < 0) {
135 warnx("invalid argument to option d: %s", optarg);
143 setenv("BLOCKSIZE", "512", 1);
148 setenv("BLOCKSIZE", "1024", 1);
152 setenv("BLOCKSIZE", "1048576", 1);
157 case 'r': /* Compatibility. */
160 ftsoptions |= FTS_XDEV;
172 * Because of the way that fts(3) works, logical walks will not count
173 * the blocks actually used by symbolic links. We rationalize this by
174 * noting that users computing logical sizes are likely to do logical
175 * copies, so not counting the links is correct. The real reason is
176 * that we'd have to re-implement the kernel's symbolic link traversing
177 * algorithm to get this right. If, for example, you have relative
178 * symbolic links referencing other relative symbolic links, it gets
179 * very nasty, very fast. The bottom line is that it's documented in
180 * the man page, so it's a feature.
183 if (Hflag + Lflag + Pflag > 1)
186 if (Hflag + Lflag + Pflag == 0)
187 Pflag = 1; /* -P (physical) is default */
190 ftsoptions |= FTS_COMFOLLOW;
193 ftsoptions |= FTS_LOGICAL;
196 ftsoptions |= FTS_PHYSICAL;
216 (void) getbsize(¬used, &blocksize);
221 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
224 while ((p = fts_read(fts)) != NULL) {
225 switch (p->fts_info) {
226 case FTS_D: /* Ignore. */
228 fts_set(fts, p, FTS_SKIP);
234 p->fts_parent->fts_bignum +=
235 p->fts_bignum += p->fts_statp->st_blocks;
237 if (p->fts_level <= depth) {
239 (void) prthumanval(howmany(p->fts_bignum, blocksize));
240 (void) printf("\t%s\n", p->fts_path);
242 (void) printf("%jd\t%s\n",
243 (intmax_t)howmany(p->fts_bignum, blocksize),
248 case FTS_DC: /* Ignore. */
250 case FTS_DNR: /* Warn, continue. */
253 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
260 if (p->fts_statp->st_nlink > 1 && linkchk(p))
263 if (listall || p->fts_level == 0) {
265 (void) prthumanval(howmany(p->fts_statp->st_blocks,
267 (void) printf("\t%s\n", p->fts_path);
269 (void) printf("%jd\t%s\n",
270 (intmax_t)howmany(p->fts_statp->st_blocks, blocksize),
275 p->fts_parent->fts_bignum += p->fts_statp->st_blocks;
277 savednumber = p->fts_parent->fts_bignum;
285 (void) prthumanval(howmany(savednumber, blocksize));
286 (void) printf("\ttotal\n");
288 (void) printf("%jd\ttotal\n", (intmax_t)howmany(savednumber, blocksize));
300 struct links_entry *next;
301 struct links_entry *previous;
306 static const size_t links_hash_initial_size = 8192;
307 static struct links_entry **buckets;
308 static struct links_entry *free_list;
309 static size_t number_buckets;
310 static unsigned long number_entries;
311 static char stop_allocating;
312 struct links_entry *le, **new_buckets;
319 /* If necessary, initialize the hash table. */
320 if (buckets == NULL) {
321 number_buckets = links_hash_initial_size;
322 buckets = malloc(number_buckets * sizeof(buckets[0]));
324 errx(1, "No memory for hardlink detection");
325 for (i = 0; i < number_buckets; i++)
329 /* If the hash table is getting too full, enlarge it. */
330 if (number_entries > number_buckets * 10 && !stop_allocating) {
331 new_size = number_buckets * 2;
332 new_buckets = malloc(new_size * sizeof(struct links_entry *));
334 /* Try releasing the free list to see if that helps. */
335 if (new_buckets == NULL && free_list != NULL) {
336 while (free_list != NULL) {
338 free_list = le->next;
341 new_buckets = malloc(new_size * sizeof(new_buckets[0]));
344 if (new_buckets == NULL) {
346 warnx("No more memory for tracking hard links");
348 memset(new_buckets, 0,
349 new_size * sizeof(struct links_entry *));
350 for (i = 0; i < number_buckets; i++) {
351 while (buckets[i] != NULL) {
352 /* Remove entry from old bucket. */
354 buckets[i] = le->next;
356 /* Add entry to new bucket. */
357 hash = (le->dev ^ le->ino) % new_size;
359 if (new_buckets[hash] != NULL)
360 new_buckets[hash]->previous =
362 le->next = new_buckets[hash];
364 new_buckets[hash] = le;
368 buckets = new_buckets;
369 number_buckets = new_size;
373 /* Try to locate this entry in the hash table. */
374 hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
375 for (le = buckets[hash]; le != NULL; le = le->next) {
376 if (le->dev == st->st_dev && le->ino == st->st_ino) {
378 * Save memory by releasing an entry when we've seen
381 if (--le->links <= 0) {
382 if (le->previous != NULL)
383 le->previous->next = le->next;
384 if (le->next != NULL)
385 le->next->previous = le->previous;
386 if (buckets[hash] == le)
387 buckets[hash] = le->next;
389 /* Recycle this node through the free list */
390 if (stop_allocating) {
393 le->next = free_list;
404 /* Add this entry to the links cache. */
405 if (free_list != NULL) {
406 /* Pull a node from the free list if we can. */
408 free_list = le->next;
410 /* Malloc one if we have to. */
411 le = malloc(sizeof(struct links_entry));
414 warnx("No more memory for tracking hard links");
417 le->dev = st->st_dev;
418 le->ino = st->st_ino;
419 le->links = st->st_nlink - 1;
421 le->next = buckets[hash];
423 if (buckets[hash] != NULL)
424 buckets[hash]->previous = le;
430 prthumanval(int64_t bytes)
436 humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
437 HN_B | HN_NOSPACE | HN_DECIMAL);
439 (void)printf("%4s", buf);
445 (void)fprintf(stderr,
446 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k | -m] [-n] [-x] [-I mask] [file ...]\n");
451 ignoreadd(const char *mask)
453 struct ignentry *ign;
455 ign = calloc(1, sizeof(*ign));
457 errx(1, "cannot allocate memory");
458 ign->mask = strdup(mask);
459 if (ign->mask == NULL)
460 errx(1, "cannot allocate memory");
461 SLIST_INSERT_HEAD(&ignores, ign, next);
467 struct ignentry *ign;
469 while (!SLIST_EMPTY(&ignores)) {
470 ign = SLIST_FIRST(&ignores);
471 SLIST_REMOVE_HEAD(&ignores, next);
480 struct ignentry *ign;
482 if (nodumpflag && (ent->fts_statp->st_flags & UF_NODUMP))
484 SLIST_FOREACH(ign, &ignores, next)
485 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)