du.c revision 1.20 1 /* $NetBSD: du.c,v 1.20 2003/04/18 13:16:50 grant 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.20 2003/04/18 13:16:50 grant 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;
83 int Hflag, Lflag, Pflag, aflag, ch, cflag, gkmflag, notused, rval, sflag;
84 char **save;
85
86 save = argv;
87 Hflag = Lflag = Pflag = aflag = cflag = gkmflag = sflag = 0;
88 totalblocks = 0;
89 ftsoptions = FTS_PHYSICAL;
90 while ((ch = getopt(argc, argv, "HLPacghkmrsx")) != -1)
91 switch (ch) {
92 case 'H':
93 Hflag = 1;
94 Lflag = Pflag = 0;
95 break;
96 case 'L':
97 Lflag = 1;
98 Hflag = Pflag = 0;
99 break;
100 case 'P':
101 Pflag = 1;
102 Hflag = Lflag = 0;
103 break;
104 case 'a':
105 aflag = 1;
106 break;
107 case 'c':
108 cflag = 1;
109 break;
110 case 'g':
111 blocksize = 1024 * 1024 * 1024;
112 gkmflag = 1;
113 break;
114 case 'h':
115 hflag = 1;
116 break;
117 case 'k':
118 blocksize = 1024;
119 gkmflag = 1;
120 break;
121 case 'm':
122 blocksize = 1024 * 1024;
123 gkmflag = 1;
124 break;
125 case 'r':
126 break;
127 case 's':
128 sflag = 1;
129 break;
130 case 'x':
131 ftsoptions |= FTS_XDEV;
132 break;
133 case '?':
134 default:
135 usage();
136 }
137 argc -= optind;
138 argv += optind;
139
140 /*
141 * XXX
142 * Because of the way that fts(3) works, logical walks will not count
143 * the blocks actually used by symbolic links. We rationalize this by
144 * noting that users computing logical sizes are likely to do logical
145 * copies, so not counting the links is correct. The real reason is
146 * that we'd have to re-implement the kernel's symbolic link traversing
147 * algorithm to get this right. If, for example, you have relative
148 * symbolic links referencing other relative symbolic links, it gets
149 * very nasty, very fast. The bottom line is that it's documented in
150 * the man page, so it's a feature.
151 */
152 if (Hflag)
153 ftsoptions |= FTS_COMFOLLOW;
154 if (Lflag) {
155 ftsoptions &= ~FTS_PHYSICAL;
156 ftsoptions |= FTS_LOGICAL;
157 }
158
159 if (aflag) {
160 if (sflag)
161 usage();
162 listdirs = listfiles = 1;
163 } else if (sflag)
164 listdirs = listfiles = 0;
165 else {
166 listfiles = 0;
167 listdirs = 1;
168 }
169
170 if (!*argv) {
171 argv = save;
172 argv[0] = ".";
173 argv[1] = NULL;
174 }
175
176 if (!gkmflag)
177 (void)getbsize(¬used, &blocksize);
178 blocksize /= 512;
179
180 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
181 err(1, "fts_open `%s'", *argv);
182
183 for (rval = 0; (p = fts_read(fts)) != NULL;)
184 switch (p->fts_info) {
185 case FTS_D: /* Ignore. */
186 break;
187 case FTS_DP:
188 p->fts_parent->fts_number +=
189 p->fts_number += p->fts_statp->st_blocks;
190 if (cflag)
191 totalblocks += p->fts_statp->st_blocks;
192 /*
193 * If listing each directory, or not listing files
194 * or directories and this is post-order of the
195 * root of a traversal, display the total.
196 */
197 if (listdirs || (!listfiles && !p->fts_level))
198 prstat(p->fts_path, p->fts_number);
199 break;
200 case FTS_DC: /* Ignore. */
201 break;
202 case FTS_DNR: /* Warn, continue. */
203 case FTS_ERR:
204 case FTS_NS:
205 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
206 rval = 1;
207 break;
208 default:
209 if (p->fts_statp->st_nlink > 1 && linkchk(p))
210 break;
211 /*
212 * If listing each file, or a non-directory file was
213 * the root of a traversal, display the total.
214 */
215 if (listfiles || !p->fts_level)
216 prstat(p->fts_path, p->fts_statp->st_blocks);
217 p->fts_parent->fts_number += p->fts_statp->st_blocks;
218 if (cflag)
219 totalblocks += p->fts_statp->st_blocks;
220 }
221 if (errno)
222 err(1, "fts_read");
223 if (cflag)
224 prstat("total", totalblocks);
225 exit(rval);
226 }
227
228 void
229 prstat(const char *fname, int64_t blocks)
230 {
231 if (hflag) {
232 char buf[5];
233 int64_t sz = blocks * 512;
234
235 humanize_number(buf, sizeof(buf), sz, "", HN_AUTOSCALE,
236 HN_B | HN_NOSPACE | HN_DECIMAL);
237
238 (void)printf("%s\t%s\n", buf, fname);
239 } else
240 (void)printf("%lld\t%s\n",
241 (long long)howmany(blocks, (int64_t)blocksize),
242 fname);
243 }
244
245
246 typedef struct _ID {
247 dev_t dev;
248 ino_t inode;
249 } ID;
250
251 int
252 linkchk(p)
253 FTSENT *p;
254 {
255 static ID *files;
256 static int maxfiles, nfiles;
257 ID *fp, *start;
258 ino_t ino;
259 dev_t dev;
260
261 ino = p->fts_statp->st_ino;
262 dev = p->fts_statp->st_dev;
263 if ((start = files) != NULL)
264 for (fp = start + nfiles - 1; fp >= start; --fp)
265 if (ino == fp->inode && dev == fp->dev)
266 return (1);
267
268 if (nfiles == maxfiles && (files = realloc((char *)files,
269 (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
270 err(1, "realloc");
271 files[nfiles].inode = ino;
272 files[nfiles].dev = dev;
273 ++nfiles;
274 return (0);
275 }
276
277 void
278 usage()
279 {
280
281 (void)fprintf(stderr,
282 "usage: du [-H | -L | -P] [-a | -s] [-cghkmrx] [file ...]\n");
283 exit(1);
284 }
285