]>
git.saurik.com Git - apple/file_cmds.git/blob - du/du.c
1 /* $NetBSD: du.c,v 1.14 1998/02/15 17:08:18 kleink 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
41 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
42 The Regents of the University of California. All rights reserved.\n");
47 static char sccsid
[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
49 __RCSID("$NetBSD: du.c,v 1.14 1998/02/15 17:08:18 kleink Exp $");
53 #include <sys/types.h>
65 int linkchk
__P((FTSENT
*));
66 int main
__P((int, char **));
67 void usage
__P((void));
76 long blocksize
, totalblocks
;
77 int ftsoptions
, listdirs
, listfiles
;
78 int Hflag
, Lflag
, Pflag
, aflag
, ch
, cflag
, kflag
, notused
, rval
, sflag
;
82 Hflag
= Lflag
= Pflag
= aflag
= cflag
= kflag
= sflag
= 0;
84 ftsoptions
= FTS_PHYSICAL
;
85 while ((ch
= getopt(argc
, argv
, "HLPackrsx")) != -1)
115 ftsoptions
|= FTS_XDEV
;
126 * Because of the way that fts(3) works, logical walks will not count
127 * the blocks actually used by symbolic links. We rationalize this by
128 * noting that users computing logical sizes are likely to do logical
129 * copies, so not counting the links is correct. The real reason is
130 * that we'd have to re-implement the kernel's symbolic link traversing
131 * algorithm to get this right. If, for example, you have relative
132 * symbolic links referencing other relative symbolic links, it gets
133 * very nasty, very fast. The bottom line is that it's documented in
134 * the man page, so it's a feature.
137 ftsoptions
|= FTS_COMFOLLOW
;
139 ftsoptions
&= ~FTS_PHYSICAL
;
140 ftsoptions
|= FTS_LOGICAL
;
146 listdirs
= listfiles
= 1;
148 listdirs
= listfiles
= 0;
161 (void)getbsize(¬used
, &blocksize
);
164 if ((fts
= fts_open(argv
, ftsoptions
, NULL
)) == NULL
)
165 err(1, "fts_open `%s'", *argv
);
167 for (rval
= 0; (p
= fts_read(fts
)) != NULL
;)
168 switch (p
->fts_info
) {
169 case FTS_D
: /* Ignore. */
172 p
->fts_parent
->fts_number
+=
173 p
->fts_number
+= p
->fts_statp
->st_blocks
;
175 totalblocks
+= p
->fts_statp
->st_blocks
;
177 * If listing each directory, or not listing files
178 * or directories and this is post-order of the
179 * root of a traversal, display the total.
181 if (listdirs
|| (!listfiles
&& !p
->fts_level
))
182 (void)printf("%ld\t%s\n",
183 howmany(p
->fts_number
, blocksize
),
186 case FTS_DC
: /* Ignore. */
188 case FTS_DNR
: /* Warn, continue. */
191 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
195 if (p
->fts_statp
->st_nlink
> 1 && linkchk(p
))
198 * If listing each file, or a non-directory file was
199 * the root of a traversal, display the total.
201 if (listfiles
|| !p
->fts_level
)
202 (void)printf("%qd\t%s\n", (long long)
203 howmany(p
->fts_statp
->st_blocks
, blocksize
),
205 p
->fts_parent
->fts_number
+= p
->fts_statp
->st_blocks
;
207 totalblocks
+= p
->fts_statp
->st_blocks
;
212 (void)printf("%ld\ttotal\n",
213 howmany(totalblocks
, blocksize
));
227 static int maxfiles
, nfiles
;
232 ino
= p
->fts_statp
->st_ino
;
233 dev
= p
->fts_statp
->st_dev
;
234 if ((start
= files
) != NULL
)
235 for (fp
= start
+ nfiles
- 1; fp
>= start
; --fp
)
236 if (ino
== fp
->inode
&& dev
== fp
->dev
)
239 if (nfiles
== maxfiles
&& (files
= realloc((char *)files
,
240 (u_int
)(sizeof(ID
) * (maxfiles
+= 128)))) == NULL
)
242 files
[nfiles
].inode
= ino
;
243 files
[nfiles
].dev
= dev
;
252 (void)fprintf(stderr
,
253 "usage: du [-H | -L | -P] [-a | -s] [-ckx] [file ...]\n");