1 /* $NetBSD: du.c,v 1.35 2011/09/01 13:37:33 joerg Exp $ */
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
45 __RCSID("$NetBSD: du.c,v 1.35 2011/09/01 13:37:33 joerg Exp $");
49 #include <sys/param.h>
50 #include <sys/types.h>
64 static int linkchk(dev_t
, ino_t
);
65 static void prstat(const char *, int64_t);
66 __dead
static void usage(void);
69 static long blocksize
;
72 main(int argc
, char *argv
[])
77 int ftsoptions
, listfiles
;
79 int Hflag
, Lflag
, aflag
, ch
, cflag
, dflag
, gkmflag
, nflag
, rval
, sflag
;
80 const char *noargv
[2];
82 Hflag
= Lflag
= aflag
= cflag
= dflag
= gkmflag
= nflag
= sflag
= 0;
84 ftsoptions
= FTS_PHYSICAL
;
86 while ((ch
= getopt(argc
, argv
, "HLPacd:ghkmnrsx")) != -1)
107 depth
= atoi(optarg
);
108 if (depth
< 0 || depth
> SHRT_MAX
) {
109 warnx("invalid argument to option d: %s",
115 blocksize
= 1024 * 1024 * 1024;
126 blocksize
= 1024 * 1024;
138 ftsoptions
|= FTS_XDEV
;
149 * Because of the way that fts(3) works, logical walks will not count
150 * the blocks actually used by symbolic links. We rationalize this by
151 * noting that users computing logical sizes are likely to do logical
152 * copies, so not counting the links is correct. The real reason is
153 * that we'd have to re-implement the kernel's symbolic link traversing
154 * algorithm to get this right. If, for example, you have relative
155 * symbolic links referencing other relative symbolic links, it gets
156 * very nasty, very fast. The bottom line is that it's documented in
157 * the man page, so it's a feature.
160 ftsoptions
|= FTS_COMFOLLOW
;
162 ftsoptions
&= ~FTS_PHYSICAL
;
163 ftsoptions
|= FTS_LOGICAL
;
180 argv
= __UNCONST(noargv
);
184 (void)getbsize(NULL
, &blocksize
);
187 if ((fts
= fts_open(argv
, ftsoptions
, NULL
)) == NULL
)
188 err(1, "fts_open `%s'", *argv
);
190 for (rval
= 0; (p
= fts_read(fts
)) != NULL
;) {
192 switch (p
->fts_info
) {
198 if (p
->fts_statp
->st_flags
& UF_NODUMP
) {
199 fts_set(fts
, p
, FTS_SKIP
);
204 switch (p
->fts_info
) {
205 case FTS_D
: /* Ignore. */
208 p
->fts_parent
->fts_number
+=
209 p
->fts_number
+= p
->fts_statp
->st_blocks
;
211 totalblocks
+= p
->fts_statp
->st_blocks
;
213 * If listing each directory, or not listing files
214 * or directories and this is post-order of the
215 * root of a traversal, display the total.
217 if (p
->fts_level
<= depth
218 || (!listfiles
&& !p
->fts_level
))
219 prstat(p
->fts_path
, p
->fts_number
);
221 case FTS_DC
: /* Ignore. */
223 case FTS_DNR
: /* Warn, continue. */
226 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
230 if (p
->fts_statp
->st_nlink
> 1 &&
231 linkchk(p
->fts_statp
->st_dev
, p
->fts_statp
->st_ino
))
234 * If listing each file, or a non-directory file was
235 * the root of a traversal, display the total.
237 if (listfiles
|| !p
->fts_level
)
238 prstat(p
->fts_path
, p
->fts_statp
->st_blocks
);
239 p
->fts_parent
->fts_number
+= p
->fts_statp
->st_blocks
;
241 totalblocks
+= p
->fts_statp
->st_blocks
;
247 prstat("total", totalblocks
);
252 prstat(const char *fname
, int64_t blocks
)
256 int64_t sz
= blocks
* 512;
258 humanize_number(buf
, sizeof(buf
), sz
, "", HN_AUTOSCALE
,
259 HN_B
| HN_NOSPACE
| HN_DECIMAL
);
261 (void)printf("%s\t%s\n", buf
, fname
);
263 (void)printf("%lld\t%s\n",
264 (long long)howmany(blocks
, (int64_t)blocksize
),
269 linkchk(dev_t dev
, ino_t ino
)
271 static struct entry
{
275 static int htshift
; /* log(allocated size) */
276 static int htmask
; /* allocated size - 1 */
277 static int htused
; /* 2*number of insertions */
278 static int sawzero
; /* Whether zero is in table or not */
281 /* this constant is (1<<64)/((1+sqrt(5))/2)
282 * aka (word size)/(golden ratio)
284 const uint64_t HTCONST
= 11400714819323198485ULL;
285 const int HTBITS
= CHAR_BIT
* sizeof(tmp
);
287 /* Never store zero in hashtable */
288 if (dev
== 0 && ino
== 0) {
294 /* Extend hash table if necessary, keep load under 0.5 */
295 if (htused
<<1 >= htmask
) {
296 struct entry
*ohtable
;
299 htshift
= 10; /* starting hashtable size */
301 htshift
++; /* exponential hashtable growth */
303 htmask
= (1 << htshift
) - 1;
307 htable
= calloc(htmask
+1, sizeof(*htable
));
311 /* populate newly allocated hashtable */
314 for (i
= 0; i
<= htmask
>>1; i
++)
315 if (ohtable
[i
].ino
|| ohtable
[i
].dev
)
316 linkchk(ohtable
[i
].dev
, ohtable
[i
].ino
);
321 /* multiplicative hashing */
326 h
= tmp
>> (HTBITS
- htshift
);
327 h2
= 1 | ( tmp
>> (HTBITS
- (htshift
<<1) - 1)); /* must be odd */
329 /* open address hashtable search with double hash probing */
330 while (htable
[h
].ino
|| htable
[h
].dev
) {
331 if ((htable
[h
].ino
== ino
) && (htable
[h
].dev
== dev
))
333 h
= (h
+ h2
) & htmask
;
336 /* Insert the current entry into hashtable */
347 (void)fprintf(stderr
,
348 "usage: du [-H | -L | -P] [-a | -d depth | -s] [-cghkmnrx] [file ...]\n");