Add ifmedia(4) reference.
[dragonfly/netmp.git] / usr.bin / du / du.c
blobfa484c35aa35627b056bf2effc6e16f3a8c39d82
1 /*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Chris Newcomb.
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.
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.
36 * @(#) Copyright (c) 1989, 1993, 1994 The Regents of the University of California. All rights reserved.
37 * @(#)du.c 8.5 (Berkeley) 5/4/95
38 * $FreeBSD: src/usr.bin/du/du.c,v 1.17.2.4 2002/12/12 16:29:39 trhodes Exp $
39 * $DragonFly: src/usr.bin/du/du.c,v 1.10 2008/07/13 03:37:43 dillon Exp $
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/stat.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fnmatch.h>
49 #include <fts.h>
50 #include <math.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <sysexits.h>
55 #include <unistd.h>
57 #define KILO_SZ(n) (n)
58 #define MEGA_SZ(n) ((n) * (n))
59 #define GIGA_SZ(n) ((n) * (n) * (n))
60 #define TERA_SZ(n) ((n) * (n) * (n) * (n))
61 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
63 #define KILO_2_SZ (KILO_SZ(1024ULL))
64 #define MEGA_2_SZ (MEGA_SZ(1024ULL))
65 #define GIGA_2_SZ (GIGA_SZ(1024ULL))
66 #define TERA_2_SZ (TERA_SZ(1024ULL))
67 #define PETA_2_SZ (PETA_SZ(1024ULL))
69 #define KILO_SI_SZ (KILO_SZ(1000ULL))
70 #define MEGA_SI_SZ (MEGA_SZ(1000ULL))
71 #define GIGA_SI_SZ (GIGA_SZ(1000ULL))
72 #define TERA_SI_SZ (TERA_SZ(1000ULL))
73 #define PETA_SI_SZ (PETA_SZ(1000ULL))
75 #define HASHSIZE 256 /* power of 2 only */
76 #define HASHMASK (HASHSIZE - 1)
78 unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ};
79 unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
80 unsigned long long *valp;
82 typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
84 int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
86 SLIST_HEAD(ignhead, ignentry) ignores;
87 struct ignentry {
88 char *mask;
89 SLIST_ENTRY(ignentry) next;
92 static int linkchk(FTSENT *);
93 static void usage(void);
94 void prthumanval(double);
95 unit_t unit_adjust(double *);
96 void ignoreadd(const char *);
97 void ignoreclean(void);
98 int ignorep(FTSENT *);
100 static char period[] = ".";
102 typedef long long du_number_t;
105 main(int argc, char **argv)
107 FTS *fts;
108 FTSENT *p;
109 long blocksize;
110 du_number_t savednumber = 0;
111 int ftsoptions;
112 int listall;
113 int depth;
114 int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval;
115 char **save;
117 Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0;
119 save = argv;
120 ftsoptions = 0;
121 depth = INT_MAX;
122 SLIST_INIT(&ignores);
124 while ((ch = getopt(argc, argv, "HI:LPasd:chkrx")) != -1)
125 switch (ch) {
126 case 'H':
127 Hflag = 1;
128 break;
129 case 'I':
130 ignoreadd(optarg);
131 break;
132 case 'L':
133 if (Pflag)
134 usage();
135 Lflag = 1;
136 break;
137 case 'P':
138 if (Lflag)
139 usage();
140 Pflag = 1;
141 break;
142 case 'a':
143 aflag = 1;
144 break;
145 case 's':
146 sflag = 1;
147 break;
148 case 'd':
149 dflag = 1;
150 errno = 0;
151 depth = atoi(optarg);
152 if (errno == ERANGE || depth < 0) {
153 warnx("invalid argument to option d: %s", optarg);
154 usage();
156 break;
157 case 'c':
158 cflag = 1;
159 break;
160 case 'h':
161 if (putenv("BLOCKSIZE=512") == -1)
162 warn("putenv: cannot set BLOCKSIZE=512");
163 hflag = 1;
164 valp = vals_base2;
165 break;
166 case 'k':
167 hflag = 0;
168 if (putenv("BLOCKSIZE=1024") == -1)
169 warn("putenv: cannot set BLOCKSIZE=1024");
170 break;
171 case 'r': /* Compatibility. */
172 break;
173 case 'x':
174 ftsoptions |= FTS_XDEV;
175 break;
176 case '?':
177 default:
178 usage();
181 argc -= optind;
182 argv += optind;
185 * XXX
186 * Because of the way that fts(3) works, logical walks will not count
187 * the blocks actually used by symbolic links. We rationalize this by
188 * noting that users computing logical sizes are likely to do logical
189 * copies, so not counting the links is correct. The real reason is
190 * that we'd have to re-implement the kernel's symbolic link traversing
191 * algorithm to get this right. If, for example, you have relative
192 * symbolic links referencing other relative symbolic links, it gets
193 * very nasty, very fast. The bottom line is that it's documented in
194 * the man page, so it's a feature.
197 if (Hflag + Lflag + Pflag > 1)
198 usage();
200 if (Hflag + Lflag + Pflag == 0)
201 Pflag = 1; /* -P (physical) is default */
203 if (Hflag)
204 ftsoptions |= FTS_COMFOLLOW;
206 if (Lflag)
207 ftsoptions |= FTS_LOGICAL;
209 if (Pflag)
210 ftsoptions |= FTS_PHYSICAL;
212 listall = 0;
214 if (aflag) {
215 if (sflag || dflag)
216 usage();
217 listall = 1;
218 } else if (sflag) {
219 if (dflag)
220 usage();
221 depth = 0;
224 if (!*argv) {
225 argv = save;
226 argv[0] = period;
227 argv[1] = NULL;
230 (void) getbsize(&notused, &blocksize);
231 blocksize /= 512;
233 rval = 0;
235 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
236 err(1, "fts_open");
238 while ((p = fts_read(fts)) != NULL) {
239 switch (p->fts_info) {
240 case FTS_D: /* Ignore. */
241 if (ignorep(p))
242 fts_set(fts, p, FTS_SKIP);
243 break;
244 case FTS_DP:
245 if (ignorep(p))
246 break;
248 if (p->fts_pointer == NULL) {
249 p->fts_pointer = malloc(sizeof(du_number_t));
250 *(du_number_t *)p->fts_pointer = 0;
252 *(du_number_t *)p->fts_pointer += p->fts_statp->st_blocks;
254 if (p->fts_parent->fts_pointer == NULL) {
255 p->fts_parent->fts_pointer = malloc(sizeof(du_number_t));
256 *(du_number_t *)p->fts_parent->fts_pointer = 0;
258 *(du_number_t *)p->fts_parent->fts_pointer += *(du_number_t *)p->fts_pointer += p->fts_statp->st_blocks;
260 if (p->fts_level <= depth) {
261 if (hflag) {
262 (void) prthumanval(howmany(*(du_number_t *)p->fts_pointer, blocksize));
263 (void) printf("\t%s\n", p->fts_path);
264 } else {
265 (void) printf("%lld\t%s\n",
266 howmany(*(du_number_t *)p->fts_pointer, blocksize),
267 p->fts_path);
270 if (p->fts_pointer) {
271 free(p->fts_pointer);
272 p->fts_pointer = NULL;
274 break;
275 case FTS_DC: /* Ignore. */
276 break;
277 case FTS_DNR: /* Warn, continue. */
278 case FTS_ERR:
279 case FTS_NS:
280 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
281 rval = 1;
282 break;
283 default:
284 if (ignorep(p))
285 break;
287 if (p->fts_statp->st_nlink > 1 && linkchk(p))
288 break;
290 if (listall || p->fts_level == 0) {
291 if (hflag) {
292 (void) prthumanval(howmany(p->fts_statp->st_blocks,
293 blocksize));
294 (void) printf("\t%s\n", p->fts_path);
295 } else {
296 (void) printf("%lld\t%s\n",
297 howmany((long long)p->fts_statp->st_blocks, blocksize),
298 p->fts_path);
301 if (p->fts_parent->fts_pointer == NULL) {
302 p->fts_parent->fts_pointer = malloc(sizeof(du_number_t));
303 *(du_number_t *)p->fts_parent->fts_pointer = 0;
305 *(du_number_t *)p->fts_parent->fts_pointer += p->fts_statp->st_blocks;
307 if (p->fts_parent->fts_pointer)
308 savednumber = *(du_number_t *)p->fts_parent->fts_pointer;
311 if (errno)
312 err(1, "fts_read");
314 if (cflag) {
315 if (hflag) {
316 (void) prthumanval(howmany(savednumber, blocksize));
317 (void) printf("\ttotal\n");
318 } else {
319 (void) printf("%lld\ttotal\n", howmany(savednumber, blocksize));
323 ignoreclean();
324 exit(rval);
327 static int
328 linkchk(FTSENT *p)
330 struct links_entry {
331 struct links_entry *next;
332 struct links_entry *previous;
333 int links;
334 dev_t dev;
335 ino_t ino;
338 static const size_t links_hash_initial_size = 8192;
339 static struct links_entry **buckets;
340 static struct links_entry *free_list;
341 static size_t number_buckets;
342 static unsigned long number_entries;
343 static char stop_allocating;
344 struct links_entry *le, **new_buckets;
345 struct stat *st;
346 size_t i, new_size;
347 int hash;
349 st = p->fts_statp;
351 /* If necessary, initialize the hash table. */
352 if (buckets == NULL) {
353 number_buckets = links_hash_initial_size;
354 buckets = malloc(number_buckets * sizeof(buckets[0]));
355 if (buckets == NULL)
356 errx(1, "No memory for hardlink detection");
357 for (i = 0; i < number_buckets; i++)
358 buckets[i] = NULL;
361 /* If the hash table is getting too full, enlarge it. */
362 if (number_entries > number_buckets * 10 && !stop_allocating) {
363 new_size = number_buckets * 2;
364 new_buckets = malloc(new_size * sizeof(struct links_entry *));
366 /* Try releasing the free list to see if that helps. */
367 if (new_buckets == NULL && free_list != NULL) {
368 while (free_list != NULL) {
369 le = free_list;
370 free_list = le->next;
371 free(le);
373 new_buckets = malloc(new_size * sizeof(new_buckets[0]));
376 if (new_buckets == NULL) {
377 stop_allocating = 1;
378 warnx("No more memory for tracking hard links");
379 } else {
380 memset(new_buckets, 0, new_size * sizeof(struct links_entry *));
381 for (i = 0; i < number_buckets; i++) {
382 while (buckets[i] != NULL) {
383 /* Remove entry from old bucket. */
384 le = buckets[i];
385 buckets[i] = le->next;
387 /* Add entry to new bucket. */
388 hash = (le->dev ^ le->ino) % new_size;
390 if (new_buckets[hash] != NULL)
391 new_buckets[hash]->previous = le;
392 le->next = new_buckets[hash];
393 le->previous = NULL;
394 new_buckets[hash] = le;
397 free(buckets);
398 buckets = new_buckets;
399 number_buckets = new_size;
403 /* Try to locate this entry in the hash table. */
404 hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
405 for (le = buckets[hash]; le != NULL; le = le->next) {
406 if (le->dev == st->st_dev && le->ino == st->st_ino) {
408 * Save memory by releasing an entry when we've seen
409 * all of it's links.
411 if (--le->links <= 0) {
412 if (le->previous != NULL)
413 le->previous->next = le->next;
414 if (le->next != NULL)
415 le->next->previous = le->previous;
416 if (buckets[hash] == le)
417 buckets[hash] = le->next;
418 number_entries--;
419 /* Recycle this node through the free list */
420 if (stop_allocating) {
421 free(le);
422 } else {
423 le->next = free_list;
424 free_list = le;
427 return (1);
431 if (stop_allocating)
432 return (0);
434 /* Add this entry to the links cache. */
435 if (free_list != NULL) {
436 /* Pull a node from the free list if we can. */
437 le = free_list;
438 free_list = le->next;
439 } else
440 /* Malloc one if we have to. */
441 le = malloc(sizeof(struct links_entry));
442 if (le == NULL) {
443 stop_allocating = 1;
444 warnx("No more memory for tracking hard links");
445 return (0);
447 le->dev = st->st_dev;
448 le->ino = st->st_ino;
449 le->links = st->st_nlink - 1;
450 number_entries++;
451 le->next = buckets[hash];
452 le->previous = NULL;
453 if (buckets[hash] != NULL)
454 buckets[hash]->previous = le;
455 buckets[hash] = le;
456 return (0);
460 * Output in "human-readable" format. Uses 3 digits max and puts
461 * unit suffixes at the end. Makes output compact and easy to read,
462 * especially on huge disks.
465 unit_t
466 unit_adjust(double *val)
468 double abval;
469 unit_t unit;
470 unsigned int unit_sz;
472 abval = fabs(*val);
474 unit_sz = abval ? ilogb(abval) / 10 : 0;
476 if (unit_sz >= UNIT_MAX) {
477 unit = NONE;
478 } else {
479 unit = unitp[unit_sz];
480 *val /= (double)valp[unit_sz];
483 return (unit);
486 void
487 prthumanval(double bytes)
489 unit_t unit;
491 bytes *= 512;
492 unit = unit_adjust(&bytes);
494 if (bytes == 0)
495 (void)printf(" 0B");
496 else if (bytes > 10)
497 (void)printf("%3.0f%c", bytes, "BKMGTPE"[unit]);
498 else
499 (void)printf("%3.1f%c", bytes, "BKMGTPE"[unit]);
502 static void
503 usage(void)
505 (void)fprintf(stderr,
506 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n");
507 exit(EX_USAGE);
510 void
511 ignoreadd(const char *mask)
513 struct ignentry *ign;
515 ign = calloc(1, sizeof(*ign));
516 if (ign == NULL)
517 errx(1, "cannot allocate memory");
518 ign->mask = strdup(mask);
519 if (ign->mask == NULL)
520 errx(1, "cannot allocate memory");
521 SLIST_INSERT_HEAD(&ignores, ign, next);
524 void
525 ignoreclean(void)
527 struct ignentry *ign;
529 while (!SLIST_EMPTY(&ignores)) {
530 ign = SLIST_FIRST(&ignores);
531 SLIST_REMOVE_HEAD(&ignores, next);
532 free(ign->mask);
533 free(ign);
538 ignorep(FTSENT *ent)
540 struct ignentry *ign;
542 SLIST_FOREACH(ign, &ignores, next)
543 if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
544 return 1;
545 return 0;