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 if (setenv("BLOCKSIZE", "512", 1) == -1)
145 "setenv: cannot set BLOCKSIZE=512");
150 if (setenv("BLOCKSIZE", "1024", 1) == -1)
151 warnx("setenv: cannot set BLOCKSIZE=1024");
155 if (setenv("BLOCKSIZE", "1048576", 1) == -1)
156 warnx("setenv: cannot set BLOCKSIZE=1048576");
161 case 'r': /* Compatibility. */
164 ftsoptions |= FTS_XDEV;
176 * Because of the way that fts(3) works, logical walks will not count
177 * the blocks actually used by symbolic links. We rationalize this by
178 * noting that users computing logical sizes are likely to do logical
179 * copies, so not counting the links is correct. The real reason is
180 * that we'd have to re-implement the kernel's symbolic link traversing
181 * algorithm to get this right. If, for example, you have relative
182 * symbolic links referencing other relative symbolic links, it gets
183 * very nasty, very fast. The bottom line is that it's documented in
184 * the man page, so it's a feature.
187 if (Hflag + Lflag + Pflag > 1)
190 if (Hflag + Lflag + Pflag == 0)
191 Pflag = 1; /* -P (physical) is default */
194 ftsoptions |= FTS_COMFOLLOW;
197 ftsoptions |= FTS_LOGICAL;
200 ftsoptions |= FTS_PHYSICAL;
220 (void) getbsize(¬used, &blocksize);
225 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
228 while ((p = fts_read(fts)) != NULL) {
229 switch (p->fts_info) {
230 case FTS_D: /* Ignore. */
232 fts_set(fts, p, FTS_SKIP);
238 p->fts_parent->fts_bignum +=
239 p->fts_bignum += p->fts_statp->st_blocks;
241 if (p->fts_level <= depth) {
243 (void) prthumanval(howmany(p->fts_bignum, blocksize));
244 (void) printf("\t%s\n", p->fts_path);
246 (void) printf("%jd\t%s\n",
247 (intmax_t)howmany(p->fts_bignum, blocksize),
252 case FTS_DC: /* Ignore. */
254 case FTS_DNR: /* Warn, continue. */
257 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
264 if (p->fts_statp->st_nlink > 1 && linkchk(p))
267 if (listall || p->fts_level == 0) {
269 (void) prthumanval(howmany(p->fts_statp->st_blocks,
271 (void) printf("\t%s\n", p->fts_path);
273 (void) printf("%jd\t%s\n",
274 (intmax_t)howmany(p->fts_statp->st_blocks, blocksize),
279 p->fts_parent->fts_bignum += p->fts_statp->st_blocks;
281 savednumber = p->fts_parent->fts_bignum;
289 (void) prthumanval(howmany(savednumber, blocksize));
290 (void) printf("\ttotal\n");
292 (void) printf("%jd\ttotal\n", (intmax_t)howmany(savednumber, blocksize));
304 struct links_entry *next;
305 struct links_entry *previous;
310 static const size_t links_hash_initial_size = 8192;
311 static struct links_entry **buckets;
312 static struct links_entry *free_list;
313 static size_t number_buckets;
314 static unsigned long number_entries;
315 static char stop_allocating;
316 struct links_entry *le, **new_buckets;
323 /* If necessary, initialize the hash table. */
324 if (buckets == NULL) {
325 number_buckets = links_hash_initial_size;
326 buckets = malloc(number_buckets * sizeof(buckets[0]));
328 errx(1, "No memory for hardlink detection");
329 for (i = 0; i < number_buckets; i++)
333 /* If the hash table is getting too full, enlarge it. */
334 if (number_entries > number_buckets * 10 && !stop_allocating) {
335 new_size = number_buckets * 2;
336 new_buckets = malloc(new_size * sizeof(struct links_entry *));
338 /* Try releasing the free list to see if that helps. */
339 if (new_buckets == NULL && free_list != NULL) {
340 while (free_list != NULL) {
342 free_list = le->next;
345 new_buckets = malloc(new_size * sizeof(new_buckets[0]));
348 if (new_buckets == NULL) {
350 warnx("No more memory for tracking hard links");
352 memset(new_buckets, 0,
353 new_size * sizeof(struct links_entry *));
354 for (i = 0; i < number_buckets; i++) {
355 while (buckets[i] != NULL) {
356 /* Remove entry from old bucket. */
358 buckets[i] = le->next;
360 /* Add entry to new bucket. */
361 hash = (le->dev ^ le->ino) % new_size;
363 if (new_buckets[hash] != NULL)
364 new_buckets[hash]->previous =
366 le->next = new_buckets[hash];
368 new_buckets[hash] = le;
372 buckets = new_buckets;
373 number_buckets = new_size;
377 /* Try to locate this entry in the hash table. */
378 hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
379 for (le = buckets[hash]; le != NULL; le = le->next) {
380 if (le->dev == st->st_dev && le->ino == st->st_ino) {
382 * Save memory by releasing an entry when we've seen
385 if (--le->links <= 0) {
386 if (le->previous != NULL)
387 le->previous->next = le->next;
388 if (le->next != NULL)
389 le->next->previous = le->previous;
390 if (buckets[hash] == le)
391 buckets[hash] = le->next;
393 /* Recycle this node through the free list */
394 if (stop_allocating) {
397 le->next = free_list;
408 /* Add this entry to the links cache. */
409 if (free_list != NULL) {
410 /* Pull a node from the free list if we can. */
412 free_list = le->next;
414 /* Malloc one if we have to. */
415 le = malloc(sizeof(struct links_entry));
418 warnx("No more memory for tracking hard links");
421 le->dev = st->st_dev;
422 le->ino = st->st_ino;
423 le->links = st->st_nlink - 1;
425 le->next = buckets[hash];
427 if (buckets[hash] != NULL)
428 buckets[hash]->previous = le;
434 prthumanval(int64_t bytes)
440 humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
441 HN_B | HN_NOSPACE | HN_DECIMAL);
443 (void)printf("%4s", buf);
449 (void)fprintf(stderr,
450 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k | -m] [-n] [-x] [-I mask] [file ...]\n");
455 ignoreadd(const char *mask)
457 struct ignentry *ign;
459 ign = calloc(1, sizeof(*ign));
461 errx(1, "cannot allocate memory");
462 ign->mask = strdup(mask);
463 if (ign->mask == NULL)
464 errx(1, "cannot allocate memory");
465 SLIST_INSERT_HEAD(&ignores, ign, next);
471 struct ignentry *ign;
473 while (!SLIST_EMPTY(&ignores)) {
474 ign = SLIST_FIRST(&ignores);
475 SLIST_REMOVE_HEAD(&ignores, next);
484 struct ignentry *ign;
486 if (nodumpflag && (ent->fts_statp->st_flags & UF_NODUMP))
488 SLIST_FOREACH(ign, &ignores, next)
489 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)