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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
33 * @(#)du.c 8.5 (Berkeley) 5/4/95
34 * $FreeBSD: src/usr.bin/du/du.c,v 1.17.2.4 2002/12/12 16:29:39 trhodes Exp $
35 * $DragonFly: src/usr.bin/du/du.c,v 1.12 2008/08/12 03:35:35 y0netan1 Exp $
38 #include <sys/param.h>
39 #include <sys/queue.h>
53 #define HASHSIZE 256 /* power of 2 only */
54 #define HASHMASK (HASHSIZE - 1)
56 SLIST_HEAD(ignhead
, ignentry
) ignores
;
59 SLIST_ENTRY(ignentry
) next
;
62 static int linkchk(FTSENT
*);
63 static void usage(void);
64 void prthumanval(int64_t);
65 void ignoreadd(const char *);
66 void ignoreclean(void);
67 int ignorep(FTSENT
*);
69 static char period
[] = ".";
71 typedef long long du_number_t
;
74 main(int argc
, char **argv
)
79 du_number_t savednumber
= 0;
83 int Hflag
, Lflag
, Pflag
, aflag
, sflag
, dflag
, cflag
, hflag
, ch
, notused
, rval
;
86 Hflag
= Lflag
= Pflag
= aflag
= sflag
= dflag
= cflag
= hflag
= 0;
93 while ((ch
= getopt(argc
, argv
, "HI:LPasd:chkrx")) != -1)
120 depth
= atoi(optarg
);
121 if (errno
== ERANGE
|| depth
< 0) {
122 warnx("invalid argument to option d: %s", optarg
);
130 if (setenv("BLOCKSIZE", "512", 1) == -1)
131 warn("setenv: cannot set BLOCKSIZE=512");
136 if (setenv("BLOCKSIZE", "1024", 1) == -1)
137 warn("setenv: cannot set BLOCKSIZE=1024");
139 case 'r': /* Compatibility. */
142 ftsoptions
|= FTS_XDEV
;
154 * Because of the way that fts(3) works, logical walks will not count
155 * the blocks actually used by symbolic links. We rationalize this by
156 * noting that users computing logical sizes are likely to do logical
157 * copies, so not counting the links is correct. The real reason is
158 * that we'd have to re-implement the kernel's symbolic link traversing
159 * algorithm to get this right. If, for example, you have relative
160 * symbolic links referencing other relative symbolic links, it gets
161 * very nasty, very fast. The bottom line is that it's documented in
162 * the man page, so it's a feature.
165 if (Hflag
+ Lflag
+ Pflag
> 1)
168 if (Hflag
+ Lflag
+ Pflag
== 0)
169 Pflag
= 1; /* -P (physical) is default */
172 ftsoptions
|= FTS_COMFOLLOW
;
175 ftsoptions
|= FTS_LOGICAL
;
178 ftsoptions
|= FTS_PHYSICAL
;
198 (void) getbsize(¬used
, &blocksize
);
203 if ((fts
= fts_open(argv
, ftsoptions
, NULL
)) == NULL
)
206 while ((p
= fts_read(fts
)) != NULL
) {
207 switch (p
->fts_info
) {
208 case FTS_D
: /* Ignore. */
210 fts_set(fts
, p
, FTS_SKIP
);
216 if (p
->fts_pointer
== NULL
) {
217 p
->fts_pointer
= malloc(sizeof(du_number_t
));
218 *(du_number_t
*)p
->fts_pointer
= 0;
220 *(du_number_t
*)p
->fts_pointer
+= p
->fts_statp
->st_blocks
;
222 if (p
->fts_parent
->fts_pointer
== NULL
) {
223 p
->fts_parent
->fts_pointer
= malloc(sizeof(du_number_t
));
224 *(du_number_t
*)p
->fts_parent
->fts_pointer
= 0;
226 *(du_number_t
*)p
->fts_parent
->fts_pointer
+= *(du_number_t
*)p
->fts_pointer
+= p
->fts_statp
->st_blocks
;
228 if (p
->fts_level
<= depth
) {
230 (void) prthumanval(howmany(*(du_number_t
*)p
->fts_pointer
, blocksize
));
231 (void) printf("\t%s\n", p
->fts_path
);
233 (void) printf("%lld\t%s\n",
234 howmany(*(du_number_t
*)p
->fts_pointer
, blocksize
),
238 if (p
->fts_pointer
) {
239 free(p
->fts_pointer
);
240 p
->fts_pointer
= NULL
;
243 case FTS_DC
: /* Ignore. */
245 case FTS_DNR
: /* Warn, continue. */
248 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
255 if (p
->fts_statp
->st_nlink
> 1 && linkchk(p
))
258 if (listall
|| p
->fts_level
== 0) {
260 (void) prthumanval(howmany(p
->fts_statp
->st_blocks
,
262 (void) printf("\t%s\n", p
->fts_path
);
264 (void) printf("%lld\t%s\n",
265 howmany((long long)p
->fts_statp
->st_blocks
, blocksize
),
269 if (p
->fts_parent
->fts_pointer
== NULL
) {
270 p
->fts_parent
->fts_pointer
= malloc(sizeof(du_number_t
));
271 *(du_number_t
*)p
->fts_parent
->fts_pointer
= 0;
273 *(du_number_t
*)p
->fts_parent
->fts_pointer
+= p
->fts_statp
->st_blocks
;
275 if (p
->fts_parent
->fts_pointer
)
276 savednumber
= *(du_number_t
*)p
->fts_parent
->fts_pointer
;
286 (void) prthumanval(howmany(savednumber
, blocksize
));
287 (void) printf("\ttotal\n");
289 (void) printf("%lld\ttotal\n", howmany(savednumber
, blocksize
));
301 struct links_entry
*next
;
302 struct links_entry
*previous
;
308 static const size_t links_hash_initial_size
= 8192;
309 static struct links_entry
**buckets
;
310 static struct links_entry
*free_list
;
311 static size_t number_buckets
;
312 static unsigned long number_entries
;
313 static char stop_allocating
;
314 struct links_entry
*le
, **new_buckets
;
321 /* If necessary, initialize the hash table. */
322 if (buckets
== NULL
) {
323 number_buckets
= links_hash_initial_size
;
324 buckets
= malloc(number_buckets
* sizeof(buckets
[0]));
326 errx(1, "No memory for hardlink detection");
327 for (i
= 0; i
< number_buckets
; i
++)
331 /* If the hash table is getting too full, enlarge it. */
332 if (number_entries
> number_buckets
* 10 && !stop_allocating
) {
333 new_size
= number_buckets
* 2;
334 new_buckets
= malloc(new_size
* sizeof(struct links_entry
*));
336 /* Try releasing the free list to see if that helps. */
337 if (new_buckets
== NULL
&& free_list
!= NULL
) {
338 while (free_list
!= NULL
) {
340 free_list
= le
->next
;
343 new_buckets
= malloc(new_size
* sizeof(new_buckets
[0]));
346 if (new_buckets
== NULL
) {
348 warnx("No more memory for tracking hard links");
350 memset(new_buckets
, 0, new_size
* sizeof(struct links_entry
*));
351 for (i
= 0; i
< number_buckets
; i
++) {
352 while (buckets
[i
] != NULL
) {
353 /* Remove entry from old bucket. */
355 buckets
[i
] = le
->next
;
357 /* Add entry to new bucket. */
358 hash
= (le
->dev
^ le
->ino
) % new_size
;
360 if (new_buckets
[hash
] != NULL
)
361 new_buckets
[hash
]->previous
= le
;
362 le
->next
= new_buckets
[hash
];
364 new_buckets
[hash
] = le
;
368 buckets
= new_buckets
;
369 number_buckets
= new_size
;
373 /* Try to locate this entry in the hash table. */
374 hash
= ( st
->st_dev
^ st
->st_ino
) % number_buckets
;
375 for (le
= buckets
[hash
]; le
!= NULL
; le
= le
->next
) {
376 if (le
->dev
== st
->st_dev
&& le
->ino
== st
->st_ino
) {
378 * Save memory by releasing an entry when we've seen
381 if (--le
->links
<= 0) {
382 if (le
->previous
!= NULL
)
383 le
->previous
->next
= le
->next
;
384 if (le
->next
!= NULL
)
385 le
->next
->previous
= le
->previous
;
386 if (buckets
[hash
] == le
)
387 buckets
[hash
] = le
->next
;
389 /* Recycle this node through the free list */
390 if (stop_allocating
) {
393 le
->next
= free_list
;
404 /* Add this entry to the links cache. */
405 if (free_list
!= NULL
) {
406 /* Pull a node from the free list if we can. */
408 free_list
= le
->next
;
410 /* Malloc one if we have to. */
411 le
= malloc(sizeof(struct links_entry
));
414 warnx("No more memory for tracking hard links");
417 le
->dev
= st
->st_dev
;
418 le
->ino
= st
->st_ino
;
419 le
->links
= st
->st_nlink
- 1;
421 le
->next
= buckets
[hash
];
423 if (buckets
[hash
] != NULL
)
424 buckets
[hash
]->previous
= le
;
430 prthumanval(int64_t bytes
)
432 char buf
[sizeof("999M")];
436 humanize_number(buf
, sizeof(buf
), bytes
, "", HN_AUTOSCALE
,
437 HN_B
| HN_NOSPACE
| HN_DECIMAL
);
439 (void) printf("%4s", buf
);
445 (void)fprintf(stderr
,
446 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n");
451 ignoreadd(const char *mask
)
453 struct ignentry
*ign
;
455 ign
= calloc(1, sizeof(*ign
));
457 errx(1, "cannot allocate memory");
458 ign
->mask
= strdup(mask
);
459 if (ign
->mask
== NULL
)
460 errx(1, "cannot allocate memory");
461 SLIST_INSERT_HEAD(&ignores
, ign
, next
);
467 struct ignentry
*ign
;
469 while (!SLIST_EMPTY(&ignores
)) {
470 ign
= SLIST_FIRST(&ignores
);
471 SLIST_REMOVE_HEAD(&ignores
, next
);
480 struct ignentry
*ign
;
482 SLIST_FOREACH(ign
, &ignores
, next
)
483 if (fnmatch(ign
->mask
, ent
->fts_name
, 0) != FNM_NOMATCH
)