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
36 * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
37 * @(#)du.c 8.5 (Berkeley) 5/4/95
38 * $FreeBSD: src/usr.bin/du/du.c,v 1.17.2.4 2002/12/12 16:29:39 trhodes Exp $
39 * $DragonFly: src/usr.bin/du/du.c,v 1.7 2005/01/01 23:02:42 cpressey Exp $
42 #include <sys/param.h>
43 #include <sys/queue.h>
57 #define KILO_SZ(n) (n)
58 #define MEGA_SZ(n) ((n) * (n))
59 #define GIGA_SZ(n) ((n) * (n) * (n))
60 #define TERA_SZ(n) ((n) * (n) * (n) * (n))
61 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
63 #define KILO_2_SZ (KILO_SZ(1024ULL))
64 #define MEGA_2_SZ (MEGA_SZ(1024ULL))
65 #define GIGA_2_SZ (GIGA_SZ(1024ULL))
66 #define TERA_2_SZ (TERA_SZ(1024ULL))
67 #define PETA_2_SZ (PETA_SZ(1024ULL))
69 #define KILO_SI_SZ (KILO_SZ(1000ULL))
70 #define MEGA_SI_SZ (MEGA_SZ(1000ULL))
71 #define GIGA_SI_SZ (GIGA_SZ(1000ULL))
72 #define TERA_SI_SZ (TERA_SZ(1000ULL))
73 #define PETA_SI_SZ (PETA_SZ(1000ULL))
75 #define HASHSIZE 256 /* power of 2 only */
76 #define HASHMASK (HASHSIZE - 1)
78 unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ};
79 unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
80 unsigned long long *valp;
82 typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
84 int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
86 SLIST_HEAD(ignhead, ignentry) ignores;
89 SLIST_ENTRY(ignentry) next;
92 static int linkchk(FTSENT *);
93 static void usage(void);
94 void prthumanval(double);
95 unit_t unit_adjust(double *);
96 void ignoreadd(const char *);
97 void ignoreclean(void);
98 int ignorep(FTSENT *);
100 static char period[] = ".";
103 main(int argc, char **argv)
107 long blocksize, savednumber = 0;
111 int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval;
114 Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0;
119 SLIST_INIT(&ignores);
121 while ((ch = getopt(argc, argv, "HI:LPasd:chkrx")) != -1)
148 depth = atoi(optarg);
149 if (errno == ERANGE || depth < 0) {
150 warnx("invalid argument to option d: %s", optarg);
158 putenv("BLOCKSIZE=512");
164 putenv("BLOCKSIZE=1024");
166 case 'r': /* Compatibility. */
169 ftsoptions |= FTS_XDEV;
181 * Because of the way that fts(3) works, logical walks will not count
182 * the blocks actually used by symbolic links. We rationalize this by
183 * noting that users computing logical sizes are likely to do logical
184 * copies, so not counting the links is correct. The real reason is
185 * that we'd have to re-implement the kernel's symbolic link traversing
186 * algorithm to get this right. If, for example, you have relative
187 * symbolic links referencing other relative symbolic links, it gets
188 * very nasty, very fast. The bottom line is that it's documented in
189 * the man page, so it's a feature.
192 if (Hflag + Lflag + Pflag > 1)
195 if (Hflag + Lflag + Pflag == 0)
196 Pflag = 1; /* -P (physical) is default */
199 ftsoptions |= FTS_COMFOLLOW;
202 ftsoptions |= FTS_LOGICAL;
205 ftsoptions |= FTS_PHYSICAL;
225 (void) getbsize(¬used, &blocksize);
230 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
233 while ((p = fts_read(fts)) != NULL) {
234 switch (p->fts_info) {
235 case FTS_D: /* Ignore. */
237 fts_set(fts, p, FTS_SKIP);
243 p->fts_parent->fts_number +=
244 p->fts_number += p->fts_statp->st_blocks;
246 if (p->fts_level <= depth) {
248 (void) prthumanval(howmany(p->fts_number, blocksize));
249 (void) printf("\t%s\n", p->fts_path);
251 (void) printf("%ld\t%s\n",
252 howmany(p->fts_number, blocksize),
257 case FTS_DC: /* Ignore. */
259 case FTS_DNR: /* Warn, continue. */
262 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
269 if (p->fts_statp->st_nlink > 1 && linkchk(p))
272 if (listall || p->fts_level == 0) {
274 (void) prthumanval(howmany(p->fts_statp->st_blocks,
276 (void) printf("\t%s\n", p->fts_path);
278 (void) printf("%qd\t%s\n",
279 howmany(p->fts_statp->st_blocks, blocksize),
284 p->fts_parent->fts_number += p->fts_statp->st_blocks;
286 savednumber = p->fts_parent->fts_number;
294 (void) prthumanval(howmany(savednumber, blocksize));
295 (void) printf("\ttotal\n");
297 (void) printf("%ld\ttotal\n", howmany(savednumber, blocksize));
309 struct links_entry *next;
310 struct links_entry *previous;
316 static const size_t links_hash_initial_size = 8192;
317 static struct links_entry **buckets;
318 static struct links_entry *free_list;
319 static size_t number_buckets;
320 static unsigned long number_entries;
321 static char stop_allocating;
322 struct links_entry *le, **new_buckets;
329 /* If necessary, initialize the hash table. */
330 if (buckets == NULL) {
331 number_buckets = links_hash_initial_size;
332 buckets = malloc(number_buckets * sizeof(buckets[0]));
334 errx(1, "No memory for hardlink detection");
335 for (i = 0; i < number_buckets; i++)
339 /* If the hash table is getting too full, enlarge it. */
340 if (number_entries > number_buckets * 10 && !stop_allocating) {
341 new_size = number_buckets * 2;
342 new_buckets = malloc(new_size * sizeof(struct links_entry *));
345 /* Try releasing the free list to see if that helps. */
346 if (new_buckets == NULL && free_list != NULL) {
347 while (free_list != NULL) {
349 free_list = le->next;
352 new_buckets = malloc(new_size * sizeof(new_buckets[0]));
355 if (new_buckets == NULL) {
357 warnx("No more memory for tracking hard links");
359 memset(new_buckets, 0, new_size * sizeof(struct links_entry *));
360 for (i = 0; i < number_buckets; i++) {
361 while (buckets[i] != NULL) {
362 /* Remove entry from old bucket. */
364 buckets[i] = le->next;
366 /* Add entry to new bucket. */
367 hash = (le->dev ^ le->ino) % new_size;
369 if (new_buckets[hash] != NULL)
370 new_buckets[hash]->previous = le;
371 le->next = new_buckets[hash];
373 new_buckets[hash] = le;
377 buckets = new_buckets;
378 number_buckets = new_size;
382 /* Try to locate this entry in the hash table. */
383 hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
384 for (le = buckets[hash]; le != NULL; le = le->next) {
385 if (le->dev == st->st_dev && le->ino == st->st_ino) {
387 * Save memory by releasing an entry when we've seen
390 if (--le->links <= 0) {
391 if (le->previous != NULL)
392 le->previous->next = le->next;
393 if (le->next != NULL)
394 le->next->previous = le->previous;
395 if (buckets[hash] == le)
396 buckets[hash] = le->next;
398 /* Recycle this node through the free list */
399 if (stop_allocating) {
402 le->next = free_list;
413 /* Add this entry to the links cache. */
414 if (free_list != NULL) {
415 /* Pull a node from the free list if we can. */
417 free_list = le->next;
419 /* Malloc one if we have to. */
420 le = malloc(sizeof(struct links_entry));
423 warnx("No more memory for tracking hard links");
426 le->dev = st->st_dev;
427 le->ino = st->st_ino;
428 le->links = st->st_nlink - 1;
430 le->next = buckets[hash];
432 if (buckets[hash] != NULL)
433 buckets[hash]->previous = le;
439 * Output in "human-readable" format. Uses 3 digits max and puts
440 * unit suffixes at the end. Makes output compact and easy to read,
441 * especially on huge disks.
445 unit_adjust(double *val)
449 unsigned int unit_sz;
453 unit_sz = abval ? ilogb(abval) / 10 : 0;
455 if (unit_sz >= UNIT_MAX) {
458 unit = unitp[unit_sz];
459 *val /= (double)valp[unit_sz];
466 prthumanval(double bytes)
471 unit = unit_adjust(&bytes);
476 (void)printf("%3.0f%c", bytes, "BKMGTPE"[unit]);
478 (void)printf("%3.1f%c", bytes, "BKMGTPE"[unit]);
484 (void)fprintf(stderr,
485 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n");
490 ignoreadd(const char *mask)
492 struct ignentry *ign;
494 ign = calloc(1, sizeof(*ign));
496 errx(1, "cannot allocate memory");
497 ign->mask = strdup(mask);
498 if (ign->mask == NULL)
499 errx(1, "cannot allocate memory");
500 SLIST_INSERT_HEAD(&ignores, ign, next);
506 struct ignentry *ign;
508 while (!SLIST_EMPTY(&ignores)) {
509 ign = SLIST_FIRST(&ignores);
510 SLIST_REMOVE_HEAD(&ignores, next);
519 struct ignentry *ign;
521 SLIST_FOREACH(ign, &ignores, next)
522 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)