Annotation of src/usr.bin/du/du.c, Revision 1.18
1.18 ! jmc 1: /* $OpenBSD: du.c,v 1.17 2004/06/21 09:44:31 otto Exp $ */
1.3 millert 2: /* $NetBSD: du.c,v 1.11 1996/10/18 07:20:35 thorpej Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1989, 1993, 1994
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Chris Newcomb.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
1.12 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: static char copyright[] =
38: "@(#) Copyright (c) 1989, 1993, 1994\n\
39: The Regents of the University of California. All rights reserved.\n";
40: #endif /* not lint */
41:
42: #ifndef lint
43: #if 0
44: static char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
45: #else
1.18 ! jmc 46: static char rcsid[] = "$OpenBSD: du.c,v 1.17 2004/06/21 09:44:31 otto Exp $";
1.1 deraadt 47: #endif
48: #endif /* not lint */
49:
50: #include <sys/types.h>
51: #include <sys/stat.h>
52:
53: #include <dirent.h>
54: #include <err.h>
55: #include <errno.h>
56: #include <fts.h>
57: #include <stdio.h>
58: #include <stdlib.h>
59: #include <string.h>
1.17 otto 60: #include <sys/tree.h>
1.1 deraadt 61: #include <unistd.h>
1.16 otto 62: #include <util.h>
1.1 deraadt 63:
1.14 deraadt 64:
1.11 millert 65: int linkchk(FTSENT *);
66: void prtout(quad_t, char *, int);
67: void usage(void);
1.1 deraadt 68:
69: int
1.13 deraadt 70: main(int argc, char *argv[])
1.1 deraadt 71: {
72: FTS *fts;
73: FTSENT *p;
1.7 pjanzen 74: long blocksize;
75: quad_t totalblocks;
1.1 deraadt 76: int ftsoptions, listdirs, listfiles;
1.7 pjanzen 77: int Hflag, Lflag, Pflag, aflag, cflag, hflag, kflag, sflag;
78: int ch, notused, rval;
1.1 deraadt 79: char **save;
80:
81: save = argv;
1.7 pjanzen 82: Hflag = Lflag = Pflag = aflag = cflag = hflag = kflag = sflag = 0;
1.3 millert 83: totalblocks = 0;
1.1 deraadt 84: ftsoptions = FTS_PHYSICAL;
1.7 pjanzen 85: while ((ch = getopt(argc, argv, "HLPachksxr")) != -1)
1.1 deraadt 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;
1.3 millert 102: case 'c':
103: cflag = 1;
104: break;
1.7 pjanzen 105: case 'h':
106: hflag = 1;
107: break;
1.1 deraadt 108: case 'k':
109: kflag = 1;
110: break;
111: case 's':
112: sflag = 1;
113: break;
1.5 deraadt 114: case 'r':
115: break;
1.1 deraadt 116: case 'x':
117: ftsoptions |= FTS_XDEV;
118: break;
119: case '?':
120: default:
121: usage();
122: }
123: argc -= optind;
124: argv += optind;
125:
126: /*
127: * XXX
128: * Because of the way that fts(3) works, logical walks will not count
129: * the blocks actually used by symbolic links. We rationalize this by
130: * noting that users computing logical sizes are likely to do logical
131: * copies, so not counting the links is correct. The real reason is
132: * that we'd have to re-implement the kernel's symbolic link traversing
133: * algorithm to get this right. If, for example, you have relative
134: * symbolic links referencing other relative symbolic links, it gets
135: * very nasty, very fast. The bottom line is that it's documented in
136: * the man page, so it's a feature.
137: */
138: if (Hflag)
139: ftsoptions |= FTS_COMFOLLOW;
140: if (Lflag) {
141: ftsoptions &= ~FTS_PHYSICAL;
142: ftsoptions |= FTS_LOGICAL;
143: }
144:
145: if (aflag) {
146: if (sflag)
147: usage();
148: listdirs = listfiles = 1;
149: } else if (sflag)
150: listdirs = listfiles = 0;
151: else {
152: listfiles = 0;
153: listdirs = 1;
154: }
155:
156: if (!*argv) {
157: argv = save;
158: argv[0] = ".";
159: argv[1] = NULL;
160: }
161:
1.8 pjanzen 162: if (hflag)
163: blocksize = 512;
164: else if (kflag)
165: blocksize = 1024;
166: else
1.1 deraadt 167: (void)getbsize(¬used, &blocksize);
168: blocksize /= 512;
169:
170: if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
1.3 millert 171: err(1, "fts_open");
1.1 deraadt 172:
173: for (rval = 0; (p = fts_read(fts)) != NULL;)
174: switch (p->fts_info) {
175: case FTS_D: /* Ignore. */
176: break;
177: case FTS_DP:
178: p->fts_parent->fts_number +=
179: p->fts_number += p->fts_statp->st_blocks;
1.3 millert 180: if (cflag)
181: totalblocks += p->fts_statp->st_blocks;
1.1 deraadt 182: /*
183: * If listing each directory, or not listing files
184: * or directories and this is post-order of the
185: * root of a traversal, display the total.
186: */
1.3 millert 187: if (listdirs || (!listfiles && !p->fts_level))
1.7 pjanzen 188: prtout((quad_t)howmany(p->fts_number, blocksize),
189: p->fts_path, hflag);
1.1 deraadt 190: break;
191: case FTS_DC: /* Ignore. */
192: break;
193: case FTS_DNR: /* Warn, continue. */
194: case FTS_ERR:
195: case FTS_NS:
196: warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
197: rval = 1;
198: break;
199: default:
200: if (p->fts_statp->st_nlink > 1 && linkchk(p))
201: break;
202: /*
203: * If listing each file, or a non-directory file was
204: * the root of a traversal, display the total.
205: */
206: if (listfiles || !p->fts_level)
1.7 pjanzen 207: prtout(howmany(p->fts_statp->st_blocks, blocksize),
208: p->fts_path, hflag);
1.1 deraadt 209: p->fts_parent->fts_number += p->fts_statp->st_blocks;
1.3 millert 210: if (cflag)
211: totalblocks += p->fts_statp->st_blocks;
1.1 deraadt 212: }
213: if (errno)
214: err(1, "fts_read");
1.7 pjanzen 215: if (cflag) {
216: prtout((quad_t)howmany(totalblocks, blocksize), "total", hflag);
217: }
1.6 ericj 218: exit(rval);
1.1 deraadt 219: }
220:
1.17 otto 221:
222: struct links_entry {
223: RB_ENTRY(links_entry) entry;
224: struct links_entry *fnext;
225: int links;
226: dev_t dev;
227: ino_t ino;
228: };
229:
230: int
231: links_cmp(struct links_entry *e1, struct links_entry *e2)
232: {
233: if (e1->dev == e2->dev) {
234: if (e1->ino == e2->ino)
235: return (0);
236: else
237: return (e1->ino < e2->ino ? -1 : 1);
238: }
239: else
240: return (e1->dev < e2->dev ? -1 : 1);
241: }
242:
243: RB_HEAD(ltree, links_entry) links = RB_INITIALIZER(&links);
244:
245: RB_GENERATE(ltree, links_entry, entry, links_cmp);
246:
247:
1.1 deraadt 248: int
1.13 deraadt 249: linkchk(FTSENT *p)
1.1 deraadt 250: {
1.17 otto 251: static struct links_entry *free_list = NULL;
252: static int stop_allocating = 0;
253: struct links_entry ltmp, *le;
1.16 otto 254: struct stat *st;
255:
256: st = p->fts_statp;
257:
1.17 otto 258: ltmp.ino = st->st_ino;
259: ltmp.dev = st->st_dev;
1.16 otto 260:
1.17 otto 261: le = RB_FIND(ltree, &links, <mp);
262: if (le != NULL) {
263: /*
264: * Save memory by releasing an entry when we've seen
265: * all of it's links.
266: */
267: if (--le->links <= 0) {
268: RB_REMOVE(ltree, &links, le);
269: /* Recycle this node through the free list */
270: if (stop_allocating) {
1.16 otto 271: free(le);
1.17 otto 272: } else {
273: le->fnext = free_list;
274: free_list = le;
1.16 otto 275: }
276: }
1.17 otto 277: return (1);
1.16 otto 278: }
1.7 pjanzen 279:
1.16 otto 280: if (stop_allocating)
281: return (0);
1.7 pjanzen 282:
1.16 otto 283: /* Add this entry to the links cache. */
284: if (free_list != NULL) {
285: /* Pull a node from the free list if we can. */
286: le = free_list;
1.17 otto 287: free_list = le->fnext;
1.16 otto 288: } else
289: /* Malloc one if we have to. */
290: le = malloc(sizeof(struct links_entry));
1.17 otto 291:
1.16 otto 292: if (le == NULL) {
293: stop_allocating = 1;
294: warnx("No more memory for tracking hard links");
295: return (0);
1.7 pjanzen 296: }
1.17 otto 297:
1.16 otto 298: le->dev = st->st_dev;
299: le->ino = st->st_ino;
300: le->links = st->st_nlink - 1;
1.17 otto 301: le->fnext = NULL;
302:
303: RB_INSERT(ltree, &links, le);
304:
1.16 otto 305: return (0);
1.7 pjanzen 306: }
307:
308: void
1.13 deraadt 309: prtout(quad_t size, char *path, int hflag)
1.7 pjanzen 310: {
311: if (!hflag)
1.9 deraadt 312: (void)printf("%lld\t%s\n", (long long)size, path);
1.7 pjanzen 313: else {
1.16 otto 314: char buf[FMT_SCALED_STRSIZE];
1.7 pjanzen 315:
1.16 otto 316: if (fmt_scaled(size * 512, buf) == 0)
317: (void)printf("%s\t%s\n", buf, path);
1.7 pjanzen 318: else
1.16 otto 319: (void)printf("%lld\t%s\n", (long long)size, path);
1.7 pjanzen 320: }
321: }
322:
1.1 deraadt 323: void
1.13 deraadt 324: usage(void)
1.1 deraadt 325: {
326:
327: (void)fprintf(stderr,
1.18 ! jmc 328: "usage: du [-a | -s] [-chkrx] [-H | -L | -P] [file ...]\n");
1.1 deraadt 329: exit(1);
330: }