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