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.4 2003/10/04 20:36:43 hmp 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 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 *);
101 main(int argc, char **argv)
105 long blocksize, savednumber = 0;
109 int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval;
112 Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0;
117 SLIST_INIT(&ignores);
119 while ((ch = getopt(argc, argv, "HI:LPasd:chkrx")) != -1)
146 depth = atoi(optarg);
147 if (errno == ERANGE || depth < 0) {
148 warnx("invalid argument to option d: %s", optarg);
156 putenv("BLOCKSIZE=512");
162 putenv("BLOCKSIZE=1024");
164 case 'r': /* Compatibility. */
167 ftsoptions |= FTS_XDEV;
179 * Because of the way that fts(3) works, logical walks will not count
180 * the blocks actually used by symbolic links. We rationalize this by
181 * noting that users computing logical sizes are likely to do logical
182 * copies, so not counting the links is correct. The real reason is
183 * that we'd have to re-implement the kernel's symbolic link traversing
184 * algorithm to get this right. If, for example, you have relative
185 * symbolic links referencing other relative symbolic links, it gets
186 * very nasty, very fast. The bottom line is that it's documented in
187 * the man page, so it's a feature.
190 if (Hflag + Lflag + Pflag > 1)
193 if (Hflag + Lflag + Pflag == 0)
194 Pflag = 1; /* -P (physical) is default */
197 ftsoptions |= FTS_COMFOLLOW;
200 ftsoptions |= FTS_LOGICAL;
203 ftsoptions |= FTS_PHYSICAL;
223 (void) getbsize(¬used, &blocksize);
228 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
231 while ((p = fts_read(fts)) != NULL) {
232 switch (p->fts_info) {
233 case FTS_D: /* Ignore. */
235 fts_set(fts, p, FTS_SKIP);
241 p->fts_parent->fts_number +=
242 p->fts_number += p->fts_statp->st_blocks;
244 if (p->fts_level <= depth) {
246 (void) prthumanval(howmany(p->fts_number, blocksize));
247 (void) printf("\t%s\n", p->fts_path);
249 (void) printf("%ld\t%s\n",
250 howmany(p->fts_number, blocksize),
255 case FTS_DC: /* Ignore. */
257 case FTS_DNR: /* Warn, continue. */
260 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
267 if (p->fts_statp->st_nlink > 1 && linkchk(p))
270 if (listall || p->fts_level == 0) {
272 (void) prthumanval(howmany(p->fts_statp->st_blocks,
274 (void) printf("\t%s\n", p->fts_path);
276 (void) printf("%qd\t%s\n",
277 howmany(p->fts_statp->st_blocks, blocksize),
282 p->fts_parent->fts_number += p->fts_statp->st_blocks;
284 savednumber = p->fts_parent->fts_number;
292 (void) prthumanval(howmany(savednumber, blocksize));
293 (void) printf("\ttotal\n");
295 (void) printf("%ld\ttotal\n", howmany(savednumber, blocksize));
314 static int *maxfiles, *nfiles;
315 static ID *filesph[HASHSIZE];
316 static int maxfilesh[HASHSIZE], nfilesh[HASHSIZE];
322 ino = p->fts_statp->st_ino;
323 index = ino & HASHMASK;
324 files = &filesph[index];
325 maxfiles = &maxfilesh[index];
326 nfiles = &nfilesh[index];
328 dev = p->fts_statp->st_dev;
329 if ((start = *files) != NULL) {
330 for (fp = start + *nfiles - 1; fp >= start; --fp)
331 if (ino == fp->inode && dev == fp->dev)
335 if (*nfiles == *maxfiles) {
336 *maxfiles = (*maxfiles + 128) + (*maxfiles / 2);
337 *files = realloc((char *)*files, sizeof(ID) * *maxfiles);
339 errx(1, "can't allocate memory");
341 (*files)[*nfiles].inode = ino;
342 (*files)[*nfiles].dev = dev;
348 * Output in "human-readable" format. Uses 3 digits max and puts
349 * unit suffixes at the end. Makes output compact and easy to read,
350 * especially on huge disks.
354 unit_adjust(double *val)
358 unsigned int unit_sz;
362 unit_sz = abval ? ilogb(abval) / 10 : 0;
364 if (unit_sz >= UNIT_MAX) {
367 unit = unitp[unit_sz];
368 *val /= (double)valp[unit_sz];
375 prthumanval(double bytes)
380 unit = unit_adjust(&bytes);
385 (void)printf("%3.0f%c", bytes, "BKMGTPE"[unit]);
387 (void)printf("%3.1f%c", bytes, "BKMGTPE"[unit]);
393 (void)fprintf(stderr,
394 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n");
399 ignoreadd(const char *mask)
401 struct ignentry *ign;
403 ign = calloc(1, sizeof(*ign));
405 errx(1, "cannot allocate memory");
406 ign->mask = strdup(mask);
407 if (ign->mask == NULL)
408 errx(1, "cannot allocate memory");
409 SLIST_INSERT_HEAD(&ignores, ign, next);
415 struct ignentry *ign;
417 while (!SLIST_EMPTY(&ignores)) {
418 ign = SLIST_FIRST(&ignores);
419 SLIST_REMOVE_HEAD(&ignores, next);
428 struct ignentry *ign;
430 SLIST_FOREACH(ign, &ignores, next)
431 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)