du.c revision 1.21 1 /* $NetBSD: du.c,v 1.21 2003/05/10 02:09:39 simonb Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Newcomb.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
25 *
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
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
42 The Regents of the University of California. All rights reserved.\n");
43 #endif /* not lint */
44
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
48 #else
49 __RCSID("$NetBSD: du.c,v 1.21 2003/05/10 02:09:39 simonb Exp $");
50 #endif
51 #endif /* not lint */
52
53 #include <sys/types.h>
54 #include <sys/stat.h>
55
56 #include <dirent.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <fts.h>
60 #include <util.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65
66 int linkchk __P((FTSENT *));
67 int main __P((int, char **));
68 void prstat __P((const char *, int64_t));
69 void usage __P((void));
70
71 int hflag;
72 long blocksize;
73
74 int
75 main(argc, argv)
76 int argc;
77 char *argv[];
78 {
79 FTS *fts;
80 FTSENT *p;
81 int64_t totalblocks;
82 int ftsoptions, listdirs, listfiles, notused;
83 int Hflag, Lflag, Pflag, aflag, ch, cflag, gkmflag, rval, sflag;
84 char *noargv[2];
85
86 Hflag = Lflag = Pflag = aflag = cflag = gkmflag = sflag = 0;
87 totalblocks = 0;
88 ftsoptions = FTS_PHYSICAL;
89 while ((ch = getopt(argc, argv, "HLPacghkmrsx")) != -1)
90 switch (ch) {
91 case 'H':
92 Hflag = 1;
93 Lflag = Pflag = 0;
94 break;
95 case 'L':
96 Lflag = 1;
97 Hflag = Pflag = 0;
98 break;
99 case 'P':
100 Pflag = 1;
101 Hflag = Lflag = 0;
102 break;
103 case 'a':
104 aflag = 1;
105 break;
106 case 'c':
107 cflag = 1;
108 break;
109 case 'g':
110 blocksize = 1024 * 1024 * 1024;
111 gkmflag = 1;
112 break;
113 case 'h':
114 hflag = 1;
115 break;
116 case 'k':
117 blocksize = 1024;
118 gkmflag = 1;
119 break;
120 case 'm':
121 blocksize = 1024 * 1024;
122 gkmflag = 1;
123 break;
124 case 'r':
125 break;
126 case 's':
127 sflag = 1;
128 break;
129 case 'x':
130 ftsoptions |= FTS_XDEV;
131 break;
132 case '?':
133 default:
134 usage();
135 }
136 argc -= optind;
137 argv += optind;
138
139 /*
140 * XXX
141 * Because of the way that fts(3) works, logical walks will not count
142 * the blocks actually used by symbolic links. We rationalize this by
143 * noting that users computing logical sizes are likely to do logical
144 * copies, so not counting the links is correct. The real reason is
145 * that we'd have to re-implement the kernel's symbolic link traversing
146 * algorithm to get this right. If, for example, you have relative
147 * symbolic links referencing other relative symbolic links, it gets
148 * very nasty, very fast. The bottom line is that it's documented in
149 * the man page, so it's a feature.
150 */
151 if (Hflag)
152 ftsoptions |= FTS_COMFOLLOW;
153 if (Lflag) {
154 ftsoptions &= ~FTS_PHYSICAL;
155 ftsoptions |= FTS_LOGICAL;
156 }
157
158 if (aflag) {
159 if (sflag)
160 usage();
161 listdirs = listfiles = 1;
162 } else if (sflag)
163 listdirs = listfiles = 0;
164 else {
165 listfiles = 0;
166 listdirs = 1;
167 }
168
169 if (!*argv) {
170 noargv[0] = ".";
171 noargv[1] = NULL;
172 argv = noargv;
173 }
174
175 if (!gkmflag)
176 (void)getbsize(¬used, &blocksize);
177 blocksize /= 512;
178
179 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
180 err(1, "fts_open `%s'", *argv);
181
182 for (rval = 0; (p = fts_read(fts)) != NULL;)
183 switch (p->fts_info) {
184 case FTS_D: /* Ignore. */
185 break;
186 case FTS_DP:
187 p->fts_parent->fts_number +=
188 p->fts_number += p->fts_statp->st_blocks;
189 if (cflag)
190 totalblocks += p->fts_statp->st_blocks;
191 /*
192 * If listing each directory, or not listing files
193 * or directories and this is post-order of the
194 * root of a traversal, display the total.
195 */
196 if (listdirs || (!listfiles && !p->fts_level))
197 prstat(p->fts_path, p->fts_number);
198 break;
199 case FTS_DC: /* Ignore. */
200 break;
201 case FTS_DNR: /* Warn, continue. */
202 case FTS_ERR:
203 case FTS_NS:
204 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
205 rval = 1;
206 break;
207 default:
208 if (p->fts_statp->st_nlink > 1 && linkchk(p))
209 break;
210 /*
211 * If listing each file, or a non-directory file was
212 * the root of a traversal, display the total.
213 */
214 if (listfiles || !p->fts_level)
215 prstat(p->fts_path, p->fts_statp->st_blocks);
216 p->fts_parent->fts_number += p->fts_statp->st_blocks;
217 if (cflag)
218 totalblocks += p->fts_statp->st_blocks;
219 }
220 if (errno)
221 err(1, "fts_read");
222 if (cflag)
223 prstat("total", totalblocks);
224 exit(rval);
225 }
226
227 void
228 prstat(const char *fname, int64_t blocks)
229 {
230 if (hflag) {
231 char buf[5];
232 int64_t sz = blocks * 512;
233
234 humanize_number(buf, sizeof(buf), sz, "", HN_AUTOSCALE,
235 HN_B | HN_NOSPACE | HN_DECIMAL);
236
237 (void)printf("%s\t%s\n", buf, fname);
238 } else
239 (void)printf("%lld\t%s\n",
240 (long long)howmany(blocks, (int64_t)blocksize),
241 fname);
242 }
243
244
245 typedef struct _ID {
246 dev_t dev;
247 ino_t inode;
248 } ID;
249
250 int
251 linkchk(p)
252 FTSENT *p;
253 {
254 static ID *files;
255 static int maxfiles, nfiles;
256 ID *fp, *start;
257 ino_t ino;
258 dev_t dev;
259
260 ino = p->fts_statp->st_ino;
261 dev = p->fts_statp->st_dev;
262 if ((start = files) != NULL)
263 for (fp = start + nfiles - 1; fp >= start; --fp)
264 if (ino == fp->inode && dev == fp->dev)
265 return (1);
266
267 if (nfiles == maxfiles && (files = realloc((char *)files,
268 (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
269 err(1, "realloc");
270 files[nfiles].inode = ino;
271 files[nfiles].dev = dev;
272 ++nfiles;
273 return (0);
274 }
275
276 void
277 usage()
278 {
279
280 (void)fprintf(stderr,
281 "usage: du [-H | -L | -P] [-a | -s] [-cghkmrx] [file ...]\n");
282 exit(1);
283 }
284