du.c revision 1.1.1.3 1 /*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Newcomb.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1989, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
41 #endif /* not lint */
42
43 #ifndef lint
44 static char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
45 #endif /* not lint */
46
47 #include <sys/param.h>
48 #include <sys/stat.h>
49
50 #include <dirent.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <fts.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 int linkchk __P((FTSENT *));
60 void usage __P((void));
61
62 int
63 main(argc, argv)
64 int argc;
65 char *argv[];
66 {
67 FTS *fts;
68 FTSENT *p;
69 long blocksize;
70 int ftsoptions, listdirs, listfiles;
71 int Hflag, Lflag, Pflag, aflag, ch, notused, rval, sflag;
72 char **save;
73
74 save = argv;
75 Hflag = Lflag = Pflag = aflag = sflag = 0;
76 ftsoptions = FTS_PHYSICAL;
77 while ((ch = getopt(argc, argv, "HLPasx")) != EOF)
78 switch (ch) {
79 case 'H':
80 Hflag = 1;
81 Lflag = Pflag = 0;
82 break;
83 case 'L':
84 Lflag = 1;
85 Hflag = Pflag = 0;
86 break;
87 case 'P':
88 Pflag = 1;
89 Hflag = Lflag = 0;
90 break;
91 case 'a':
92 aflag = 1;
93 break;
94 case 's':
95 sflag = 1;
96 break;
97 case 'x':
98 ftsoptions |= FTS_XDEV;
99 break;
100 case '?':
101 default:
102 usage();
103 }
104 argc -= optind;
105 argv += optind;
106
107 /*
108 * XXX
109 * Because of the way that fts(3) works, logical walks will not count
110 * the blocks actually used by symbolic links. We rationalize this by
111 * noting that users computing logical sizes are likely to do logical
112 * copies, so not counting the links is correct. The real reason is
113 * that we'd have to re-implement the kernel's symbolic link traversing
114 * algorithm to get this right. If, for example, you have relative
115 * symbolic links referencing other relative symbolic links, it gets
116 * very nasty, very fast. The bottom line is that it's documented in
117 * the man page, so it's a feature.
118 */
119 if (Hflag)
120 ftsoptions |= FTS_COMFOLLOW;
121 if (Lflag) {
122 ftsoptions &= ~FTS_PHYSICAL;
123 ftsoptions |= FTS_LOGICAL;
124 }
125
126 if (aflag) {
127 if (sflag)
128 usage();
129 listdirs = listfiles = 1;
130 } else if (sflag)
131 listdirs = listfiles = 0;
132 else {
133 listfiles = 0;
134 listdirs = 1;
135 }
136
137 if (!*argv) {
138 argv = save;
139 argv[0] = ".";
140 argv[1] = NULL;
141 }
142
143 (void)getbsize(¬used, &blocksize);
144 blocksize /= 512;
145
146 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
147 err(1, NULL);
148
149 for (rval = 0; (p = fts_read(fts)) != NULL;)
150 switch (p->fts_info) {
151 case FTS_D: /* Ignore. */
152 break;
153 case FTS_DP:
154 p->fts_parent->fts_number +=
155 p->fts_number += p->fts_statp->st_blocks;
156 /*
157 * If listing each directory, or not listing files
158 * or directories and this is post-order of the
159 * root of a traversal, display the total.
160 */
161 if (listdirs || !listfiles && !p->fts_level)
162 (void)printf("%ld\t%s\n",
163 howmany(p->fts_number, blocksize),
164 p->fts_path);
165 break;
166 case FTS_DC: /* Ignore. */
167 break;
168 case FTS_DNR: /* Warn, continue. */
169 case FTS_ERR:
170 case FTS_NS:
171 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
172 rval = 1;
173 break;
174 default:
175 if (p->fts_statp->st_nlink > 1 && linkchk(p))
176 break;
177 /*
178 * If listing each file, or a non-directory file was
179 * the root of a traversal, display the total.
180 */
181 if (listfiles || !p->fts_level)
182 (void)printf("%qd\t%s\n",
183 howmany(p->fts_statp->st_blocks, blocksize),
184 p->fts_path);
185 p->fts_parent->fts_number += p->fts_statp->st_blocks;
186 }
187 if (errno)
188 err(1, "fts_read");
189 exit(0);
190 }
191
192 typedef struct _ID {
193 dev_t dev;
194 ino_t inode;
195 } ID;
196
197 int
198 linkchk(p)
199 FTSENT *p;
200 {
201 static ID *files;
202 static int maxfiles, nfiles;
203 ID *fp, *start;
204 ino_t ino;
205 dev_t dev;
206
207 ino = p->fts_statp->st_ino;
208 dev = p->fts_statp->st_dev;
209 if ((start = files) != NULL)
210 for (fp = start + nfiles - 1; fp >= start; --fp)
211 if (ino == fp->inode && dev == fp->dev)
212 return (1);
213
214 if (nfiles == maxfiles && (files = realloc((char *)files,
215 (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
216 err(1, "");
217 files[nfiles].inode = ino;
218 files[nfiles].dev = dev;
219 ++nfiles;
220 return (0);
221 }
222
223 void
224 usage()
225 {
226
227 (void)fprintf(stderr,
228 "usage: du [-H | -L | -P] [-a | -s] [-x] [file ...]\n");
229 exit(1);
230 }
231