du.c revision 1.13 1 /* $NetBSD: du.c,v 1.13 1997/10/19 14:06:18 mrg 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.13 1997/10/19 14:06:18 mrg 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 <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64
65 int linkchk __P((FTSENT *));
66 int main __P((int, char **));
67 void usage __P((void));
68
69 int
70 main(argc, argv)
71 int argc;
72 char *argv[];
73 {
74 FTS *fts;
75 FTSENT *p;
76 long blocksize, totalblocks;
77 int ftsoptions, listdirs, listfiles;
78 int Hflag, Lflag, Pflag, aflag, ch, cflag, kflag, notused, rval, sflag;
79 char **save;
80
81 save = argv;
82 Hflag = Lflag = Pflag = aflag = cflag = kflag = sflag = 0;
83 totalblocks = 0;
84 ftsoptions = FTS_PHYSICAL;
85 while ((ch = getopt(argc, argv, "HLPacksx")) != -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 'k':
106 blocksize = 1024;
107 kflag = 1;
108 break;
109 case 's':
110 sflag = 1;
111 break;
112 case 'x':
113 ftsoptions |= FTS_XDEV;
114 break;
115 case '?':
116 default:
117 usage();
118 }
119 argc -= optind;
120 argv += optind;
121
122 /*
123 * XXX
124 * Because of the way that fts(3) works, logical walks will not count
125 * the blocks actually used by symbolic links. We rationalize this by
126 * noting that users computing logical sizes are likely to do logical
127 * copies, so not counting the links is correct. The real reason is
128 * that we'd have to re-implement the kernel's symbolic link traversing
129 * algorithm to get this right. If, for example, you have relative
130 * symbolic links referencing other relative symbolic links, it gets
131 * very nasty, very fast. The bottom line is that it's documented in
132 * the man page, so it's a feature.
133 */
134 if (Hflag)
135 ftsoptions |= FTS_COMFOLLOW;
136 if (Lflag) {
137 ftsoptions &= ~FTS_PHYSICAL;
138 ftsoptions |= FTS_LOGICAL;
139 }
140
141 if (aflag) {
142 if (sflag)
143 usage();
144 listdirs = listfiles = 1;
145 } else if (sflag)
146 listdirs = listfiles = 0;
147 else {
148 listfiles = 0;
149 listdirs = 1;
150 }
151
152 if (!*argv) {
153 argv = save;
154 argv[0] = ".";
155 argv[1] = NULL;
156 }
157
158 if (!kflag)
159 (void)getbsize(¬used, &blocksize);
160 blocksize /= 512;
161
162 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
163 err(1, "fts_open `%s'", *argv);
164
165 for (rval = 0; (p = fts_read(fts)) != NULL;)
166 switch (p->fts_info) {
167 case FTS_D: /* Ignore. */
168 break;
169 case FTS_DP:
170 p->fts_parent->fts_number +=
171 p->fts_number += p->fts_statp->st_blocks;
172 if (cflag)
173 totalblocks += p->fts_statp->st_blocks;
174 /*
175 * If listing each directory, or not listing files
176 * or directories and this is post-order of the
177 * root of a traversal, display the total.
178 */
179 if (listdirs || (!listfiles && !p->fts_level))
180 (void)printf("%ld\t%s\n",
181 howmany(p->fts_number, blocksize),
182 p->fts_path);
183 break;
184 case FTS_DC: /* Ignore. */
185 break;
186 case FTS_DNR: /* Warn, continue. */
187 case FTS_ERR:
188 case FTS_NS:
189 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
190 rval = 1;
191 break;
192 default:
193 if (p->fts_statp->st_nlink > 1 && linkchk(p))
194 break;
195 /*
196 * If listing each file, or a non-directory file was
197 * the root of a traversal, display the total.
198 */
199 if (listfiles || !p->fts_level)
200 (void)printf("%qd\t%s\n", (long long)
201 howmany(p->fts_statp->st_blocks, blocksize),
202 p->fts_path);
203 p->fts_parent->fts_number += p->fts_statp->st_blocks;
204 if (cflag)
205 totalblocks += p->fts_statp->st_blocks;
206 }
207 if (errno)
208 err(1, "fts_read");
209 if (cflag)
210 (void)printf("%ld\ttotal\n",
211 howmany(totalblocks, blocksize));
212 exit(0);
213 }
214
215 typedef struct _ID {
216 dev_t dev;
217 ino_t inode;
218 } ID;
219
220 int
221 linkchk(p)
222 FTSENT *p;
223 {
224 static ID *files;
225 static int maxfiles, nfiles;
226 ID *fp, *start;
227 ino_t ino;
228 dev_t dev;
229
230 ino = p->fts_statp->st_ino;
231 dev = p->fts_statp->st_dev;
232 if ((start = files) != NULL)
233 for (fp = start + nfiles - 1; fp >= start; --fp)
234 if (ino == fp->inode && dev == fp->dev)
235 return (1);
236
237 if (nfiles == maxfiles && (files = realloc((char *)files,
238 (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
239 err(1, "realloc");
240 files[nfiles].inode = ino;
241 files[nfiles].dev = dev;
242 ++nfiles;
243 return (0);
244 }
245
246 void
247 usage()
248 {
249
250 (void)fprintf(stderr,
251 "usage: du [-H | -L | -P] [-a | -s] [-ckx] [file ...]\n");
252 exit(1);
253 }
254