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.12 2008/08/12 03:35:35 y0netan1 Exp $
42 #include <sys/param.h>
43 #include <sys/queue.h>
57 #define HASHSIZE 256 /* power of 2 only */
58 #define HASHMASK (HASHSIZE - 1)
60 SLIST_HEAD(ignhead
, ignentry
) ignores
;
63 SLIST_ENTRY(ignentry
) next
;
66 static int linkchk(FTSENT
*);
67 static void usage(void);
68 void prthumanval(int64_t);
69 void ignoreadd(const char *);
70 void ignoreclean(void);
71 int ignorep(FTSENT
*);
73 static char period
[] = ".";
75 typedef long long du_number_t
;
78 main(int argc
, char **argv
)
83 du_number_t savednumber
= 0;
87 int Hflag
, Lflag
, Pflag
, aflag
, sflag
, dflag
, cflag
, hflag
, ch
, notused
, rval
;
90 Hflag
= Lflag
= Pflag
= aflag
= sflag
= dflag
= cflag
= hflag
= 0;
97 while ((ch
= getopt(argc
, argv
, "HI:LPasd:chkrx")) != -1)
124 depth
= atoi(optarg
);
125 if (errno
== ERANGE
|| depth
< 0) {
126 warnx("invalid argument to option d: %s", optarg
);
134 if (putenv("BLOCKSIZE=512") == -1)
135 warn("putenv: cannot set BLOCKSIZE=512");
140 if (putenv("BLOCKSIZE=1024") == -1)
141 warn("putenv: cannot set BLOCKSIZE=1024");
143 case 'r': /* Compatibility. */
146 ftsoptions
|= FTS_XDEV
;
158 * Because of the way that fts(3) works, logical walks will not count
159 * the blocks actually used by symbolic links. We rationalize this by
160 * noting that users computing logical sizes are likely to do logical
161 * copies, so not counting the links is correct. The real reason is
162 * that we'd have to re-implement the kernel's symbolic link traversing
163 * algorithm to get this right. If, for example, you have relative
164 * symbolic links referencing other relative symbolic links, it gets
165 * very nasty, very fast. The bottom line is that it's documented in
166 * the man page, so it's a feature.
169 if (Hflag
+ Lflag
+ Pflag
> 1)
172 if (Hflag
+ Lflag
+ Pflag
== 0)
173 Pflag
= 1; /* -P (physical) is default */
176 ftsoptions
|= FTS_COMFOLLOW
;
179 ftsoptions
|= FTS_LOGICAL
;
182 ftsoptions
|= FTS_PHYSICAL
;
202 (void) getbsize(¬used
, &blocksize
);
207 if ((fts
= fts_open(argv
, ftsoptions
, NULL
)) == NULL
)
210 while ((p
= fts_read(fts
)) != NULL
) {
211 switch (p
->fts_info
) {
212 case FTS_D
: /* Ignore. */
214 fts_set(fts
, p
, FTS_SKIP
);
220 if (p
->fts_pointer
== NULL
) {
221 p
->fts_pointer
= malloc(sizeof(du_number_t
));
222 *(du_number_t
*)p
->fts_pointer
= 0;
224 *(du_number_t
*)p
->fts_pointer
+= p
->fts_statp
->st_blocks
;
226 if (p
->fts_parent
->fts_pointer
== NULL
) {
227 p
->fts_parent
->fts_pointer
= malloc(sizeof(du_number_t
));
228 *(du_number_t
*)p
->fts_parent
->fts_pointer
= 0;
230 *(du_number_t
*)p
->fts_parent
->fts_pointer
+= *(du_number_t
*)p
->fts_pointer
+= p
->fts_statp
->st_blocks
;
232 if (p
->fts_level
<= depth
) {
234 (void) prthumanval(howmany(*(du_number_t
*)p
->fts_pointer
, blocksize
));
235 (void) printf("\t%s\n", p
->fts_path
);
237 (void) printf("%lld\t%s\n",
238 howmany(*(du_number_t
*)p
->fts_pointer
, blocksize
),
242 if (p
->fts_pointer
) {
243 free(p
->fts_pointer
);
244 p
->fts_pointer
= NULL
;
247 case FTS_DC
: /* Ignore. */
249 case FTS_DNR
: /* Warn, continue. */
252 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
259 if (p
->fts_statp
->st_nlink
> 1 && linkchk(p
))
262 if (listall
|| p
->fts_level
== 0) {
264 (void) prthumanval(howmany(p
->fts_statp
->st_blocks
,
266 (void) printf("\t%s\n", p
->fts_path
);
268 (void) printf("%lld\t%s\n",
269 howmany((long long)p
->fts_statp
->st_blocks
, blocksize
),
273 if (p
->fts_parent
->fts_pointer
== NULL
) {
274 p
->fts_parent
->fts_pointer
= malloc(sizeof(du_number_t
));
275 *(du_number_t
*)p
->fts_parent
->fts_pointer
= 0;
277 *(du_number_t
*)p
->fts_parent
->fts_pointer
+= p
->fts_statp
->st_blocks
;
279 if (p
->fts_parent
->fts_pointer
)
280 savednumber
= *(du_number_t
*)p
->fts_parent
->fts_pointer
;
288 (void) prthumanval(howmany(savednumber
, blocksize
));
289 (void) printf("\ttotal\n");
291 (void) printf("%lld\ttotal\n", howmany(savednumber
, blocksize
));
303 struct links_entry
*next
;
304 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, new_size
* sizeof(struct links_entry
*));
353 for (i
= 0; i
< number_buckets
; i
++) {
354 while (buckets
[i
] != NULL
) {
355 /* Remove entry from old bucket. */
357 buckets
[i
] = le
->next
;
359 /* Add entry to new bucket. */
360 hash
= (le
->dev
^ le
->ino
) % new_size
;
362 if (new_buckets
[hash
] != NULL
)
363 new_buckets
[hash
]->previous
= le
;
364 le
->next
= new_buckets
[hash
];
366 new_buckets
[hash
] = le
;
370 buckets
= new_buckets
;
371 number_buckets
= new_size
;
375 /* Try to locate this entry in the hash table. */
376 hash
= ( st
->st_dev
^ st
->st_ino
) % number_buckets
;
377 for (le
= buckets
[hash
]; le
!= NULL
; le
= le
->next
) {
378 if (le
->dev
== st
->st_dev
&& le
->ino
== st
->st_ino
) {
380 * Save memory by releasing an entry when we've seen
383 if (--le
->links
<= 0) {
384 if (le
->previous
!= NULL
)
385 le
->previous
->next
= le
->next
;
386 if (le
->next
!= NULL
)
387 le
->next
->previous
= le
->previous
;
388 if (buckets
[hash
] == le
)
389 buckets
[hash
] = le
->next
;
391 /* Recycle this node through the free list */
392 if (stop_allocating
) {
395 le
->next
= free_list
;
406 /* Add this entry to the links cache. */
407 if (free_list
!= NULL
) {
408 /* Pull a node from the free list if we can. */
410 free_list
= le
->next
;
412 /* Malloc one if we have to. */
413 le
= malloc(sizeof(struct links_entry
));
416 warnx("No more memory for tracking hard links");
419 le
->dev
= st
->st_dev
;
420 le
->ino
= st
->st_ino
;
421 le
->links
= st
->st_nlink
- 1;
423 le
->next
= buckets
[hash
];
425 if (buckets
[hash
] != NULL
)
426 buckets
[hash
]->previous
= le
;
432 prthumanval(int64_t bytes
)
434 char buf
[sizeof("999M")];
438 humanize_number(buf
, sizeof(buf
), bytes
, "", HN_AUTOSCALE
,
439 HN_B
| HN_NOSPACE
| HN_DECIMAL
);
441 (void) printf("%4s", buf
);
447 (void)fprintf(stderr
,
448 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n");
453 ignoreadd(const char *mask
)
455 struct ignentry
*ign
;
457 ign
= calloc(1, sizeof(*ign
));
459 errx(1, "cannot allocate memory");
460 ign
->mask
= strdup(mask
);
461 if (ign
->mask
== NULL
)
462 errx(1, "cannot allocate memory");
463 SLIST_INSERT_HEAD(&ignores
, ign
, next
);
469 struct ignentry
*ign
;
471 while (!SLIST_EMPTY(&ignores
)) {
472 ign
= SLIST_FIRST(&ignores
);
473 SLIST_REMOVE_HEAD(&ignores
, next
);
482 struct ignentry
*ign
;
484 SLIST_FOREACH(ign
, &ignores
, next
)
485 if (fnmatch(ign
->mask
, ent
->fts_name
, 0) != FNM_NOMATCH
)