]> git.saurik.com Git - apple/file_cmds.git/blob - du/du.c
8b1c46b045a5f18895f62725662ce07240583d25
[apple/file_cmds.git] / du / du.c
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 const 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 #if 0
45 static const char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
46 #endif
47 #endif /* not lint */
48 #include <sys/cdefs.h>
49 __RCSID("$FreeBSD: src/usr.bin/du/du.c,v 1.28 2002/12/30 18:13:07 mike Exp $");
50
51 #include <sys/param.h>
52 #include <sys/queue.h>
53 #include <sys/stat.h>
54
55 #include <err.h>
56 #include <errno.h>
57 #include <fnmatch.h>
58 #include <fts.h>
59 #include <math.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <sysexits.h>
64 #include <unistd.h>
65 #include <sys/param.h>
66 #include <sys/mount.h>
67
68 #ifdef __APPLE__
69 #include "get_compat.h"
70 #else
71 #define COMPAT_MODE(func, mode) 1
72 #endif
73
74 #define KILO_SZ(n) (n)
75 #define MEGA_SZ(n) ((n) * (n))
76 #define GIGA_SZ(n) ((n) * (n) * (n))
77 #define TERA_SZ(n) ((n) * (n) * (n) * (n))
78 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
79
80 #define KILO_2_SZ (KILO_SZ(1024ULL))
81 #define MEGA_2_SZ (MEGA_SZ(1024ULL))
82 #define GIGA_2_SZ (GIGA_SZ(1024ULL))
83 #define TERA_2_SZ (TERA_SZ(1024ULL))
84 #define PETA_2_SZ (PETA_SZ(1024ULL))
85
86 #define KILO_SI_SZ (KILO_SZ(1000ULL))
87 #define MEGA_SI_SZ (MEGA_SZ(1000ULL))
88 #define GIGA_SI_SZ (GIGA_SZ(1000ULL))
89 #define TERA_SI_SZ (TERA_SZ(1000ULL))
90 #define PETA_SI_SZ (PETA_SZ(1000ULL))
91
92 #define TWO_TB (2LL * 1024LL * 1024LL * 1024LL * 1024LL)
93
94 unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ};
95 unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
96 unsigned long long *valp;
97
98 typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
99
100 int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
101
102 SLIST_HEAD(ignhead, ignentry) ignores;
103 struct ignentry {
104 char *mask;
105 SLIST_ENTRY(ignentry) next;
106 };
107
108 int linkchk(FTSENT *);
109 static void usage(void);
110 void prthumanval(double);
111 unit_t unit_adjust(double *);
112 void ignoreadd(const char *);
113 void ignoreclean(void);
114 int ignorep(FTSENT *);
115
116 int
117 main(int argc, char *argv[])
118 {
119 FTS *fts;
120 FTSENT *p;
121 long blocksize;
122 int ftsoptions;
123 int listall;
124 int depth;
125 int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval;
126 char **save;
127 static char dot[] = ".";
128 off_t *ftsnum, *ftsparnum, savednumber = 0;
129
130 Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0;
131
132 save = argv;
133 ftsoptions = 0;
134 depth = INT_MAX;
135 SLIST_INIT(&ignores);
136
137 while ((ch = getopt(argc, argv, "HI:LPasd:chkrx")) != -1)
138 switch (ch) {
139 case 'H':
140 Hflag = 1;
141 Pflag = Lflag = 0;
142 break;
143 case 'I':
144 ignoreadd(optarg);
145 break;
146 case 'L':
147 if (Pflag && COMPAT_MODE("bin/du", "legacy")) {
148 usage();
149 } else {
150 Hflag = Pflag = 0;
151 }
152 Lflag = 1;
153 break;
154 case 'P':
155 if (Lflag && COMPAT_MODE("bin/du", "legacy")) {
156 usage();
157 } else {
158 Hflag = Lflag = 0;
159 }
160 Pflag = 1;
161 break;
162 case 'a':
163 aflag = 1;
164 break;
165 case 's':
166 sflag = 1;
167 break;
168 case 'd':
169 dflag = 1;
170 errno = 0;
171 depth = atoi(optarg);
172 if (errno == ERANGE || depth < 0) {
173 warnx("invalid argument to option d: %s", optarg);
174 usage();
175 }
176 break;
177 case 'c':
178 cflag = 1;
179 break;
180 case 'h':
181 putenv("BLOCKSIZE=512");
182 hflag = 1;
183 valp = vals_base2;
184 break;
185 case 'k':
186 if (!hflag)
187 putenv("BLOCKSIZE=1024");
188 break;
189 case 'r': /* Compatibility. */
190 break;
191 case 'x':
192 ftsoptions |= FTS_XDEV;
193 break;
194 case '?':
195 default:
196 usage();
197 }
198
199 argc -= optind;
200 argv += optind;
201
202 /*
203 * XXX
204 * Because of the way that fts(3) works, logical walks will not count
205 * the blocks actually used by symbolic links. We rationalize this by
206 * noting that users computing logical sizes are likely to do logical
207 * copies, so not counting the links is correct. The real reason is
208 * that we'd have to re-implement the kernel's symbolic link traversing
209 * algorithm to get this right. If, for example, you have relative
210 * symbolic links referencing other relative symbolic links, it gets
211 * very nasty, very fast. The bottom line is that it's documented in
212 * the man page, so it's a feature.
213 */
214
215 if (Hflag + Lflag + Pflag > 1)
216 usage();
217
218 if (Hflag + Lflag + Pflag == 0)
219 Pflag = 1; /* -P (physical) is default */
220
221 if (Hflag)
222 ftsoptions |= FTS_COMFOLLOW;
223
224 if (Lflag)
225 ftsoptions |= FTS_LOGICAL;
226
227 if (Pflag)
228 ftsoptions |= FTS_PHYSICAL;
229
230 listall = 0;
231
232 if (aflag) {
233 if (sflag || dflag)
234 usage();
235 listall = 1;
236 } else if (sflag) {
237 if (dflag)
238 usage();
239 depth = 0;
240 }
241
242 if (!*argv) {
243 argv = save;
244 argv[0] = dot;
245 argv[1] = NULL;
246 }
247
248 (void) getbsize(&notused, &blocksize);
249 blocksize /= 512;
250
251 rval = 0;
252
253 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
254 err(1, "fts_open");
255
256 while ((p = fts_read(fts)) != NULL) {
257 switch (p->fts_info) {
258 case FTS_D: /* Ignore. */
259 if (ignorep(p))
260 fts_set(fts, p, FTS_SKIP);
261 break;
262 case FTS_DP:
263 if (ignorep(p))
264 break;
265
266 ftsparnum = (off_t *)&p->fts_parent->fts_number;
267 ftsnum = (off_t *)&p->fts_number;
268 if (p->fts_statp->st_size < TWO_TB) {
269 ftsparnum[0] += ftsnum[0] += p->fts_statp->st_blocks;
270 } else {
271 ftsparnum[0] += ftsnum[0] += howmany(p->fts_statp->st_size, 512LL);
272 }
273
274 if (p->fts_level <= depth) {
275 if (hflag) {
276 (void) prthumanval(howmany(*ftsnum, blocksize));
277 (void) printf("\t%s\n", p->fts_path);
278 } else {
279 (void) printf("%lld\t%s\n",
280 howmany(*ftsnum, blocksize),
281 p->fts_path);
282 }
283 }
284 break;
285 case FTS_DC: /* Ignore. */
286 if (COMPAT_MODE("bin/du", "unix2003")) {
287 errx(1, "Can't follow symlink cycle from %s to %s", p->fts_path, p->fts_cycle->fts_path);
288 }
289 break;
290 case FTS_DNR: /* Warn, continue. */
291 case FTS_ERR:
292 case FTS_NS:
293 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
294 rval = 1;
295 break;
296 case FTS_SLNONE:
297 if (COMPAT_MODE("bin/du", "unix2003")) {
298 struct stat sb;
299 int rc = stat(p->fts_path, &sb);
300 if (rc < 0 && errno == ELOOP) {
301 errx(1, "Too many symlinks at %s", p->fts_path);
302 }
303 }
304 default:
305 if (ignorep(p))
306 break;
307
308 if (p->fts_statp->st_nlink > 1 && linkchk(p))
309 break;
310
311 if (listall || p->fts_level == 0) {
312 if (hflag) {
313 if (p->fts_statp->st_size < TWO_TB) {
314 (void) prthumanval(howmany(p->fts_statp->st_blocks,
315 blocksize));
316 } else {
317 (void) prthumanval(howmany(howmany(p->fts_statp->st_size, 512LL),
318 blocksize));
319 }
320 (void) printf("\t%s\n", p->fts_path);
321 } else {
322 if (p->fts_statp->st_size < TWO_TB) {
323 (void) printf("%qd\t%s\n",
324 (long long)howmany(p->fts_statp->st_blocks, blocksize),
325 p->fts_path);
326 } else {
327 (void) printf("%qd\t%s\n",
328 (long long)howmany(howmany(p->fts_statp->st_size, 512LL), blocksize),
329 p->fts_path);
330 }
331 }
332 }
333
334 ftsparnum = (off_t *)&p->fts_parent->fts_number;
335 if (p->fts_statp->st_size < TWO_TB) {
336 ftsparnum[0] += p->fts_statp->st_blocks;
337 } else {
338 ftsparnum[0] += p->fts_statp->st_size / 512LL;
339 }
340 }
341 savednumber = ((off_t *)&p->fts_parent->fts_number)[0];
342 }
343
344 if (errno)
345 err(1, "fts_read");
346
347 if (cflag) {
348 if (hflag) {
349 (void) prthumanval(howmany(savednumber, blocksize));
350 (void) printf("\ttotal\n");
351 } else {
352 (void) printf("%lld\ttotal\n", howmany(savednumber, blocksize));
353 }
354 }
355
356 ignoreclean();
357 exit(rval);
358 }
359
360
361 typedef struct _ID {
362 dev_t dev;
363 ino_t inode;
364 } ID;
365
366
367 int
368 linkchk(FTSENT *p)
369 {
370 static ID *files;
371 static int maxfiles, nfiles;
372 ID *fp, *start;
373 ino_t ino;
374 dev_t dev;
375
376 ino = p->fts_statp->st_ino;
377 dev = p->fts_statp->st_dev;
378 if ((start = files) != NULL)
379 for (fp = start + nfiles - 1; fp >= start; --fp)
380 if (ino == fp->inode && dev == fp->dev) {
381 return (1);
382 }
383
384 if (nfiles == maxfiles && (files = realloc((char *)files,
385 (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
386 errx(1, "can't allocate memory");
387 files[nfiles].inode = ino;
388 files[nfiles].dev = dev;
389 ++nfiles;
390 return (0);
391 }
392
393 /*
394 * Output in "human-readable" format. Uses 3 digits max and puts
395 * unit suffixes at the end. Makes output compact and easy to read,
396 * especially on huge disks.
397 *
398 */
399 unit_t
400 unit_adjust(double *val)
401 {
402 double abval;
403 unit_t unit;
404 unsigned int unit_sz;
405
406 abval = fabs(*val);
407
408 unit_sz = abval ? ilogb(abval) / 10 : 0;
409
410 if (unit_sz >= UNIT_MAX) {
411 unit = NONE;
412 } else {
413 unit = unitp[unit_sz];
414 *val /= (double)valp[unit_sz];
415 }
416
417 return (unit);
418 }
419
420 void
421 prthumanval(double bytes)
422 {
423 unit_t unit;
424
425 bytes *= 512;
426 unit = unit_adjust(&bytes);
427
428 if (bytes == 0)
429 (void)printf(" 0B");
430 else if (bytes > 10)
431 (void)printf("%3.0f%c", bytes, "BKMGTPE"[unit]);
432 else
433 (void)printf("%3.1f%c", bytes, "BKMGTPE"[unit]);
434 }
435
436 static void
437 usage(void)
438 {
439 (void)fprintf(stderr,
440 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n");
441 exit(EX_USAGE);
442 }
443
444 void
445 ignoreadd(const char *mask)
446 {
447 struct ignentry *ign;
448
449 ign = calloc(1, sizeof(*ign));
450 if (ign == NULL)
451 errx(1, "cannot allocate memory");
452 ign->mask = strdup(mask);
453 if (ign->mask == NULL)
454 errx(1, "cannot allocate memory");
455 SLIST_INSERT_HEAD(&ignores, ign, next);
456 }
457
458 void
459 ignoreclean(void)
460 {
461 struct ignentry *ign;
462
463 while (!SLIST_EMPTY(&ignores)) {
464 ign = SLIST_FIRST(&ignores);
465 SLIST_REMOVE_HEAD(&ignores, next);
466 free(ign->mask);
467 free(ign);
468 }
469 }
470
471 int
472 ignorep(FTSENT *ent)
473 {
474 struct ignentry *ign;
475
476 if (S_ISDIR(ent->fts_statp->st_mode) && !strcmp("fd", ent->fts_name)) {
477 struct statfs sfsb;
478 int rc = statfs(ent->fts_accpath, &sfsb);
479 if (rc >= 0 && !strcmp("fdesc", sfsb.f_fstypename)) {
480 /* Don't cd into /dev/fd/N since one of those is likely to be
481 the cwd as of the start of du which causes all manner of
482 unpleasant surprises */
483 return 1;
484 }
485 }
486 SLIST_FOREACH(ign, &ignores, next)
487 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
488 return 1;
489 return 0;
490 }