1be82cc86Sbostic /*
2c2a22a30Sbostic * Copyright (c) 1989, 1993, 1994
30f784a8bSbostic * The Regents of the University of California. All rights reserved.
4be82cc86Sbostic *
5be82cc86Sbostic * This code is derived from software contributed to Berkeley by
6be82cc86Sbostic * Chris Newcomb.
7be82cc86Sbostic *
84cd9d18bSbostic * %sccs.include.redist.c%
9be82cc86Sbostic */
10c671e6eeSmckusic
11be82cc86Sbostic #ifndef lint
120f784a8bSbostic static char copyright[] =
13c2a22a30Sbostic "@(#) Copyright (c) 1989, 1993, 1994\n\
140f784a8bSbostic The Regents of the University of California. All rights reserved.\n";
15be82cc86Sbostic #endif /* not lint */
16be82cc86Sbostic
17be82cc86Sbostic #ifndef lint
18*07aa32fbSbostic static char sccsid[] = "@(#)du.c 8.5 (Berkeley) 05/04/95";
19be82cc86Sbostic #endif /* not lint */
20be82cc86Sbostic
217149f5afSbill #include <sys/param.h>
227149f5afSbill #include <sys/stat.h>
23be82cc86Sbostic
247f733f2cSbostic #include <dirent.h>
257f733f2cSbostic #include <err.h>
267f733f2cSbostic #include <errno.h>
277f733f2cSbostic #include <fts.h>
287f733f2cSbostic #include <stdio.h>
297f733f2cSbostic #include <stdlib.h>
307f733f2cSbostic #include <string.h>
31*07aa32fbSbostic #include <unistd.h>
327f733f2cSbostic
330637e3c8Sbostic int linkchk __P((FTSENT *));
340637e3c8Sbostic void usage __P((void));
350637e3c8Sbostic
367f733f2cSbostic int
main(argc,argv)377149f5afSbill main(argc, argv)
38e2dd9371Sroot int argc;
390637e3c8Sbostic char *argv[];
407149f5afSbill {
41c2a22a30Sbostic FTS *fts;
42c2a22a30Sbostic FTSENT *p;
43dbf208f0Sbostic long blocksize;
44c2a22a30Sbostic int ftsoptions, listdirs, listfiles;
45c2a22a30Sbostic int Hflag, Lflag, Pflag, aflag, ch, notused, rval, sflag;
46ec4f1f8eSbostic char **save;
477149f5afSbill
48ec4f1f8eSbostic save = argv;
49c2a22a30Sbostic Hflag = Lflag = Pflag = aflag = sflag = 0;
50c2a22a30Sbostic ftsoptions = FTS_PHYSICAL;
51c2a22a30Sbostic while ((ch = getopt(argc, argv, "HLPasx")) != EOF)
52be82cc86Sbostic switch (ch) {
53ab11773eSelan case 'H':
54c2a22a30Sbostic Hflag = 1;
55c2a22a30Sbostic Lflag = Pflag = 0;
56c2a22a30Sbostic break;
57c2a22a30Sbostic case 'L':
58c2a22a30Sbostic Lflag = 1;
59c2a22a30Sbostic Hflag = Pflag = 0;
60c2a22a30Sbostic break;
61c2a22a30Sbostic case 'P':
62c2a22a30Sbostic Pflag = 1;
63c2a22a30Sbostic Hflag = Lflag = 0;
64ab11773eSelan break;
65be82cc86Sbostic case 'a':
660d3a1fe2Sbostic aflag = 1;
67be82cc86Sbostic break;
68be82cc86Sbostic case 's':
690d3a1fe2Sbostic sflag = 1;
70be82cc86Sbostic break;
71be82cc86Sbostic case 'x':
72ec4f1f8eSbostic ftsoptions |= FTS_XDEV;
73be82cc86Sbostic break;
74be82cc86Sbostic case '?':
75be82cc86Sbostic default:
760d3a1fe2Sbostic usage();
77377b7101Sroot }
78c2a22a30Sbostic argc -= optind;
79be82cc86Sbostic argv += optind;
80be82cc86Sbostic
81c2a22a30Sbostic /*
82c2a22a30Sbostic * XXX
83c2a22a30Sbostic * Because of the way that fts(3) works, logical walks will not count
84c2a22a30Sbostic * the blocks actually used by symbolic links. We rationalize this by
85c2a22a30Sbostic * noting that users computing logical sizes are likely to do logical
86c2a22a30Sbostic * copies, so not counting the links is correct. The real reason is
87c2a22a30Sbostic * that we'd have to re-implement the kernel's symbolic link traversing
88c2a22a30Sbostic * algorithm to get this right. If, for example, you have relative
89c2a22a30Sbostic * symbolic links referencing other relative symbolic links, it gets
90c2a22a30Sbostic * very nasty, very fast. The bottom line is that it's documented in
91c2a22a30Sbostic * the man page, so it's a feature.
92c2a22a30Sbostic */
93c2a22a30Sbostic if (Hflag)
94c2a22a30Sbostic ftsoptions |= FTS_COMFOLLOW;
95c2a22a30Sbostic if (Lflag) {
96c2a22a30Sbostic ftsoptions &= ~FTS_PHYSICAL;
97c2a22a30Sbostic ftsoptions |= FTS_LOGICAL;
98c2a22a30Sbostic }
99c2a22a30Sbostic
1000d3a1fe2Sbostic if (aflag) {
1010d3a1fe2Sbostic if (sflag)
1020d3a1fe2Sbostic usage();
1030d3a1fe2Sbostic listdirs = listfiles = 1;
1040d3a1fe2Sbostic } else if (sflag)
1050d3a1fe2Sbostic listdirs = listfiles = 0;
1060d3a1fe2Sbostic else {
1070d3a1fe2Sbostic listfiles = 0;
1080d3a1fe2Sbostic listdirs = 1;
1090d3a1fe2Sbostic }
1100d3a1fe2Sbostic
111ec4f1f8eSbostic if (!*argv) {
112ec4f1f8eSbostic argv = save;
113ec4f1f8eSbostic argv[0] = ".";
114ec4f1f8eSbostic argv[1] = NULL;
115ec4f1f8eSbostic }
116be82cc86Sbostic
117e82c7a6dSbostic (void)getbsize(¬used, &blocksize);
1180637e3c8Sbostic blocksize /= 512;
1190637e3c8Sbostic
120ebe4c317Sbostic if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
121c2a22a30Sbostic err(1, NULL);
122ec4f1f8eSbostic
123c2a22a30Sbostic for (rval = 0; (p = fts_read(fts)) != NULL;)
124ec4f1f8eSbostic switch (p->fts_info) {
125c2a22a30Sbostic case FTS_D: /* Ignore. */
126ec4f1f8eSbostic break;
127ec4f1f8eSbostic case FTS_DP:
128ec4f1f8eSbostic p->fts_parent->fts_number +=
1292611fccdSbostic p->fts_number += p->fts_statp->st_blocks;
130ec4f1f8eSbostic /*
131ec4f1f8eSbostic * If listing each directory, or not listing files
132ec4f1f8eSbostic * or directories and this is post-order of the
133ec4f1f8eSbostic * root of a traversal, display the total.
134ec4f1f8eSbostic */
135ec4f1f8eSbostic if (listdirs || !listfiles && !p->fts_level)
1360637e3c8Sbostic (void)printf("%ld\t%s\n",
1370637e3c8Sbostic howmany(p->fts_number, blocksize),
1380637e3c8Sbostic p->fts_path);
139ec4f1f8eSbostic break;
140c2a22a30Sbostic case FTS_DC: /* Ignore. */
141c2a22a30Sbostic break;
142c2a22a30Sbostic case FTS_DNR: /* Warn, continue. */
143ec4f1f8eSbostic case FTS_ERR:
1449aee2cfdSbostic case FTS_NS:
1453e8725b6Sbostic warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
146c2a22a30Sbostic rval = 1;
1479aa4812dSbostic break;
148ec4f1f8eSbostic default:
1492611fccdSbostic if (p->fts_statp->st_nlink > 1 && linkchk(p))
150ec4f1f8eSbostic break;
151ec4f1f8eSbostic /*
152ec4f1f8eSbostic * If listing each file, or a non-directory file was
153ec4f1f8eSbostic * the root of a traversal, display the total.
154ec4f1f8eSbostic */
155ec4f1f8eSbostic if (listfiles || !p->fts_level)
15676f518f6Sbostic (void)printf("%qd\t%s\n",
1570637e3c8Sbostic howmany(p->fts_statp->st_blocks, blocksize),
1580637e3c8Sbostic p->fts_path);
1592611fccdSbostic p->fts_parent->fts_number += p->fts_statp->st_blocks;
160be82cc86Sbostic }
161ebe4c317Sbostic if (errno)
162c2a22a30Sbostic err(1, "fts_read");
1637149f5afSbill exit(0);
1647149f5afSbill }
1657149f5afSbill
166ec4f1f8eSbostic typedef struct _ID {
167ec4f1f8eSbostic dev_t dev;
168ec4f1f8eSbostic ino_t inode;
169ec4f1f8eSbostic } ID;
170e2dd9371Sroot
1710637e3c8Sbostic int
linkchk(p)172ec4f1f8eSbostic linkchk(p)
173df0db63cSpendry FTSENT *p;
1747149f5afSbill {
175ec4f1f8eSbostic static ID *files;
176ec4f1f8eSbostic static int maxfiles, nfiles;
177df0db63cSpendry ID *fp, *start;
178df0db63cSpendry ino_t ino;
179df0db63cSpendry dev_t dev;
1807149f5afSbill
1812611fccdSbostic ino = p->fts_statp->st_ino;
1822611fccdSbostic dev = p->fts_statp->st_dev;
183c2a22a30Sbostic if ((start = files) != NULL)
184ec4f1f8eSbostic for (fp = start + nfiles - 1; fp >= start; --fp)
185ec4f1f8eSbostic if (ino == fp->inode && dev == fp->dev)
186ec4f1f8eSbostic return (1);
187be82cc86Sbostic
1880637e3c8Sbostic if (nfiles == maxfiles && (files = realloc((char *)files,
189ebe4c317Sbostic (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
1907f733f2cSbostic err(1, "");
191ec4f1f8eSbostic files[nfiles].inode = ino;
192ec4f1f8eSbostic files[nfiles].dev = dev;
193ec4f1f8eSbostic ++nfiles;
194ec4f1f8eSbostic return (0);
1957149f5afSbill }
1960d3a1fe2Sbostic
1970637e3c8Sbostic void
usage()1980d3a1fe2Sbostic usage()
1990d3a1fe2Sbostic {
200df0db63cSpendry
201c2a22a30Sbostic (void)fprintf(stderr,
202c2a22a30Sbostic "usage: du [-H | -L | -P] [-a | -s] [-x] [file ...]\n");
2030d3a1fe2Sbostic exit(1);
2040d3a1fe2Sbostic }
205