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: src/usr.bin/du/du.c,v 1.38 2005/04/09 14:31:40 stefanf Exp $");
51 #include <sys/mount.h>
52 #include <sys/param.h>
53 #include <sys/queue.h>
70 #include <get_compat.h>
72 #define COMPAT_MODE(func, mode) (1)
75 #define KILO_SZ(n) (n)
76 #define MEGA_SZ(n) ((n) * (n))
77 #define GIGA_SZ(n) ((n) * (n) * (n))
78 #define TERA_SZ(n) ((n) * (n) * (n) * (n))
79 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
81 #define KILO_2_SZ (KILO_SZ(1024ULL))
82 #define MEGA_2_SZ (MEGA_SZ(1024ULL))
83 #define GIGA_2_SZ (GIGA_SZ(1024ULL))
84 #define TERA_2_SZ (TERA_SZ(1024ULL))
85 #define PETA_2_SZ (PETA_SZ(1024ULL))
87 #define KILO_SI_SZ (KILO_SZ(1000ULL))
88 #define MEGA_SI_SZ (MEGA_SZ(1000ULL))
89 #define GIGA_SI_SZ (GIGA_SZ(1000ULL))
90 #define TERA_SI_SZ (TERA_SZ(1000ULL))
91 #define PETA_SI_SZ (PETA_SZ(1000ULL))
93 #define TWO_TB (2LL * 1024LL * 1024LL * 1024LL * 1024LL)
95 unsigned long long vals_si
[] = {1, KILO_SI_SZ
, MEGA_SI_SZ
, GIGA_SI_SZ
, TERA_SI_SZ
, PETA_SI_SZ
};
96 unsigned long long vals_base2
[] = {1, KILO_2_SZ
, MEGA_2_SZ
, GIGA_2_SZ
, TERA_2_SZ
, PETA_2_SZ
};
97 unsigned long long *valp
;
99 typedef enum { NONE
, KILO
, MEGA
, GIGA
, TERA
, PETA
, UNIT_MAX
} unit_t
;
101 int unitp
[] = { NONE
, KILO
, MEGA
, GIGA
, TERA
, PETA
};
103 SLIST_HEAD(ignhead
, ignentry
) ignores
;
106 SLIST_ENTRY(ignentry
) next
;
109 static int linkchk(FTSENT
*);
110 static void usage(void);
111 void prthumanval(double);
112 unit_t
unit_adjust(double *);
113 void ignoreadd(const char *);
114 void ignoreclean(void);
115 int ignorep(FTSENT
*);
118 main(int argc
, char *argv
[])
122 off_t savednumber
= 0;
127 int Hflag
, Lflag
, Pflag
, aflag
, sflag
, dflag
, cflag
, hflag
, ch
, notused
, rval
;
129 static char dot
[] = ".";
130 off_t
*ftsnum
, *ftsparnum
;
132 setlocale(LC_ALL
, "");
134 Hflag
= Lflag
= Pflag
= aflag
= sflag
= dflag
= cflag
= hflag
= 0;
139 SLIST_INIT(&ignores
);
141 while ((ch
= getopt(argc
, argv
, "HI:LPasd:chkmrx")) != -1)
167 depth
= atoi(optarg
);
168 if (errno
== ERANGE
|| depth
< 0) {
169 warnx("invalid argument to option d: %s", optarg
);
177 putenv("BLOCKSIZE=512");
183 putenv("BLOCKSIZE=1024");
187 putenv("BLOCKSIZE=1048576");
189 case 'r': /* Compatibility. */
192 ftsoptions
|= FTS_XDEV
;
204 * Because of the way that fts(3) works, logical walks will not count
205 * the blocks actually used by symbolic links. We rationalize this by
206 * noting that users computing logical sizes are likely to do logical
207 * copies, so not counting the links is correct. The real reason is
208 * that we'd have to re-implement the kernel's symbolic link traversing
209 * algorithm to get this right. If, for example, you have relative
210 * symbolic links referencing other relative symbolic links, it gets
211 * very nasty, very fast. The bottom line is that it's documented in
212 * the man page, so it's a feature.
215 if (Hflag
+ Lflag
+ Pflag
> 1)
218 if (Hflag
+ Lflag
+ Pflag
== 0)
219 Pflag
= 1; /* -P (physical) is default */
222 ftsoptions
|= FTS_COMFOLLOW
;
225 ftsoptions
|= FTS_LOGICAL
;
228 ftsoptions
|= FTS_PHYSICAL
;
248 (void) getbsize(¬used
, &blocksize
);
253 if ((fts
= fts_open(argv
, ftsoptions
, NULL
)) == NULL
)
256 while ((p
= fts_read(fts
)) != NULL
) {
257 switch (p
->fts_info
) {
258 case FTS_D
: /* Ignore. */
260 fts_set(fts
, p
, FTS_SKIP
);
266 ftsparnum
= (off_t
*)&p
->fts_parent
->fts_number
;
267 ftsnum
= (off_t
*)&p
->fts_number
;
268 if (p
->fts_statp
->st_size
< TWO_TB
) {
269 ftsparnum
[0] += ftsnum
[0] += p
->fts_statp
->st_blocks
;
271 ftsparnum
[0] += ftsnum
[0] += howmany(p
->fts_statp
->st_size
, 512LL);
274 if (p
->fts_level
<= depth
) {
276 (void) prthumanval(howmany(*ftsnum
, blocksize
));
277 (void) printf("\t%s\n", p
->fts_path
);
279 (void) printf("%jd\t%s\n",
280 (intmax_t)howmany(*ftsnum
, blocksize
),
285 case FTS_DC
: /* Ignore. */
286 if (COMPAT_MODE("bin/du", "unix2003")) {
287 errx(1, "Can't follow symlink cycle from %s to %s", p
->fts_path
, p
->fts_cycle
->fts_path
);
290 case FTS_DNR
: /* Warn, continue. */
293 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
297 if (COMPAT_MODE("bin/du", "unix2003")) {
299 int rc
= stat(p
->fts_path
, &sb
);
300 if (rc
< 0 && errno
== ELOOP
) {
301 errx(1, "Too many symlinks at %s", p
->fts_path
);
308 if (p
->fts_statp
->st_nlink
> 1 && linkchk(p
))
311 if (listall
|| p
->fts_level
== 0) {
313 if (p
->fts_statp
->st_size
< TWO_TB
) {
314 (void) prthumanval(howmany(p
->fts_statp
->st_blocks
,
317 (void) prthumanval(howmany(howmany(p
->fts_statp
->st_size
, 512LL),
320 (void) printf("\t%s\n", p
->fts_path
);
322 if (p
->fts_statp
->st_size
< TWO_TB
) {
323 (void) printf("%jd\t%s\n",
324 (intmax_t)howmany(p
->fts_statp
->st_blocks
, blocksize
),
327 (void) printf("%jd\t%s\n",
328 (intmax_t)howmany(howmany(p
->fts_statp
->st_size
, 512LL), blocksize
),
334 ftsparnum
= (off_t
*)&p
->fts_parent
->fts_number
;
335 if (p
->fts_statp
->st_size
< TWO_TB
) {
336 ftsparnum
[0] += p
->fts_statp
->st_blocks
;
338 ftsparnum
[0] += p
->fts_statp
->st_size
/ 512LL;
341 savednumber
= ((off_t
*)&p
->fts_parent
->fts_number
)[0];
349 (void) prthumanval(howmany(savednumber
, blocksize
));
350 (void) printf("\ttotal\n");
352 (void) printf("%jd\ttotal\n", (intmax_t)howmany(savednumber
, blocksize
));
364 struct links_entry
*next
;
365 struct links_entry
*previous
;
370 static const size_t links_hash_initial_size
= 8192;
371 static struct links_entry
**buckets
;
372 static struct links_entry
*free_list
;
373 static size_t number_buckets
;
374 static unsigned long number_entries
;
375 static char stop_allocating
;
376 struct links_entry
*le
, **new_buckets
;
383 /* If necessary, initialize the hash table. */
384 if (buckets
== NULL
) {
385 number_buckets
= links_hash_initial_size
;
386 buckets
= malloc(number_buckets
* sizeof(buckets
[0]));
388 errx(1, "No memory for hardlink detection");
389 for (i
= 0; i
< number_buckets
; i
++)
393 /* If the hash table is getting too full, enlarge it. */
394 if (number_entries
> number_buckets
* 10 && !stop_allocating
) {
395 new_size
= number_buckets
* 2;
396 new_buckets
= malloc(new_size
* sizeof(struct links_entry
*));
398 /* Try releasing the free list to see if that helps. */
399 if (new_buckets
== NULL
&& free_list
!= NULL
) {
400 while (free_list
!= NULL
) {
402 free_list
= le
->next
;
405 new_buckets
= malloc(new_size
* sizeof(new_buckets
[0]));
408 if (new_buckets
== NULL
) {
410 warnx("No more memory for tracking hard links");
412 memset(new_buckets
, 0,
413 new_size
* sizeof(struct links_entry
*));
414 for (i
= 0; i
< number_buckets
; i
++) {
415 while (buckets
[i
] != NULL
) {
416 /* Remove entry from old bucket. */
418 buckets
[i
] = le
->next
;
420 /* Add entry to new bucket. */
421 hash
= (le
->dev
^ le
->ino
) % new_size
;
423 if (new_buckets
[hash
] != NULL
)
424 new_buckets
[hash
]->previous
=
426 le
->next
= new_buckets
[hash
];
428 new_buckets
[hash
] = le
;
432 buckets
= new_buckets
;
433 number_buckets
= new_size
;
437 /* Try to locate this entry in the hash table. */
438 hash
= ( st
->st_dev
^ st
->st_ino
) % number_buckets
;
439 for (le
= buckets
[hash
]; le
!= NULL
; le
= le
->next
) {
440 if (le
->dev
== st
->st_dev
&& le
->ino
== st
->st_ino
) {
442 * Save memory by releasing an entry when we've seen
445 if (--le
->links
<= 0) {
446 if (le
->previous
!= NULL
)
447 le
->previous
->next
= le
->next
;
448 if (le
->next
!= NULL
)
449 le
->next
->previous
= le
->previous
;
450 if (buckets
[hash
] == le
)
451 buckets
[hash
] = le
->next
;
453 /* Recycle this node through the free list */
454 if (stop_allocating
) {
457 le
->next
= free_list
;
468 /* Add this entry to the links cache. */
469 if (free_list
!= NULL
) {
470 /* Pull a node from the free list if we can. */
472 free_list
= le
->next
;
474 /* Malloc one if we have to. */
475 le
= malloc(sizeof(struct links_entry
));
478 warnx("No more memory for tracking hard links");
481 le
->dev
= st
->st_dev
;
482 le
->ino
= st
->st_ino
;
483 le
->links
= st
->st_nlink
- 1;
485 le
->next
= buckets
[hash
];
487 if (buckets
[hash
] != NULL
)
488 buckets
[hash
]->previous
= le
;
494 * Output in "human-readable" format. Uses 3 digits max and puts
495 * unit suffixes at the end. Makes output compact and easy to read,
496 * especially on huge disks.
500 unit_adjust(double *val
)
504 unsigned int unit_sz
;
508 unit_sz
= abval ?
ilogb(abval
) / 10 : 0;
510 if (unit_sz
>= UNIT_MAX
) {
513 unit
= unitp
[unit_sz
];
514 *val
/= (double)valp
[unit_sz
];
521 prthumanval(double bytes
)
526 unit
= unit_adjust(&bytes
);
531 (void)printf("%3.0f%c", bytes
, "BKMGTPE"[unit
]);
533 (void)printf("%3.1f%c", bytes
, "BKMGTPE"[unit
]);
539 (void)fprintf(stderr
,
540 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k | -m] [-x] [-I mask] [file ...]\n");
545 ignoreadd(const char *mask
)
547 struct ignentry
*ign
;
549 ign
= calloc(1, sizeof(*ign
));
551 errx(1, "cannot allocate memory");
552 ign
->mask
= strdup(mask
);
553 if (ign
->mask
== NULL
)
554 errx(1, "cannot allocate memory");
555 SLIST_INSERT_HEAD(&ignores
, ign
, next
);
561 struct ignentry
*ign
;
563 while (!SLIST_EMPTY(&ignores
)) {
564 ign
= SLIST_FIRST(&ignores
);
565 SLIST_REMOVE_HEAD(&ignores
, next
);
574 struct ignentry
*ign
;
577 if (S_ISDIR(ent
->fts_statp
->st_mode
) && !strcmp("fd", ent
->fts_name
)) {
579 int rc
= statfs(ent
->fts_accpath
, &sfsb
);
580 if (rc
>= 0 && !strcmp("fdesc", sfsb
.f_fstypename
)) {
581 /* Don't cd into /dev/fd/N since one of those is likely to be
582 the cwd as of the start of du which causes all manner of
583 unpleasant surprises */
587 #endif /* __APPLE__ */
588 SLIST_FOREACH(ign
, &ignores
, next
)
589 if (fnmatch(ign
->mask
, ent
->fts_name
, 0) != FNM_NOMATCH
)