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
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
38 static const char copyright[] =
39 "@(#) Copyright (c) 1989, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
47 static const char rcsid[] =
52 #include <sys/param.h>
65 #define KILO_SZ(n) (n)
66 #define MEGA_SZ(n) ((n) * (n))
67 #define GIGA_SZ(n) ((n) * (n) * (n))
68 #define TERA_SZ(n) ((n) * (n) * (n) * (n))
69 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
71 #define KILO_2_SZ (KILO_SZ(1024ULL))
72 #define MEGA_2_SZ (MEGA_SZ(1024ULL))
73 #define GIGA_2_SZ (GIGA_SZ(1024ULL))
74 #define TERA_2_SZ (TERA_SZ(1024ULL))
75 #define PETA_2_SZ (PETA_SZ(1024ULL))
77 #define KILO_SI_SZ (KILO_SZ(1000ULL))
78 #define MEGA_SI_SZ (MEGA_SZ(1000ULL))
79 #define GIGA_SI_SZ (GIGA_SZ(1000ULL))
80 #define TERA_SI_SZ (TERA_SZ(1000ULL))
81 #define PETA_SI_SZ (PETA_SZ(1000ULL))
83 unsigned long long vals_si [] = {1, KILO_SI_SZ, MEGA_SI_SZ, GIGA_SI_SZ, TERA_SI_SZ, PETA_SI_SZ};
84 unsigned long long vals_base2[] = {1, KILO_2_SZ, MEGA_2_SZ, GIGA_2_SZ, TERA_2_SZ, PETA_2_SZ};
85 unsigned long long *valp;
87 typedef enum { NONE, KILO, MEGA, GIGA, TERA, PETA, UNIT_MAX } unit_t;
89 int unitp [] = { NONE, KILO, MEGA, GIGA, TERA, PETA };
91 int linkchk __P((FTSENT *));
92 static void usage __P((void));
93 void prthumanval __P((double));
94 unit_t unit_adjust __P((double *));
103 long blocksize, savednumber = 0;
107 int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag, hflag, ch, notused, rval;
110 Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag = 0;
116 while ((ch = getopt(argc, argv, "HLPasd:chkrx")) != -1)
140 depth = atoi(optarg);
141 if (errno == ERANGE || depth < 0) {
142 warnx("invalid argument to option d: %s", optarg);
150 putenv("BLOCKSIZE=512");
155 putenv("BLOCKSIZE=1024");
157 case 'r': /* Compatibility. */
160 ftsoptions |= FTS_XDEV;
172 * Because of the way that fts(3) works, logical walks will not count
173 * the blocks actually used by symbolic links. We rationalize this by
174 * noting that users computing logical sizes are likely to do logical
175 * copies, so not counting the links is correct. The real reason is
176 * that we'd have to re-implement the kernel's symbolic link traversing
177 * algorithm to get this right. If, for example, you have relative
178 * symbolic links referencing other relative symbolic links, it gets
179 * very nasty, very fast. The bottom line is that it's documented in
180 * the man page, so it's a feature.
183 if (Hflag + Lflag + Pflag > 1)
186 if (Hflag + Lflag + Pflag == 0)
187 Pflag = 1; /* -P (physical) is default */
190 ftsoptions |= FTS_COMFOLLOW;
193 ftsoptions |= FTS_LOGICAL;
196 ftsoptions |= FTS_PHYSICAL;
216 (void) getbsize(¬used, &blocksize);
221 if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
224 while ((p = fts_read(fts)) != NULL) {
225 switch (p->fts_info) {
226 case FTS_D: /* Ignore. */
229 p->fts_parent->fts_number +=
230 p->fts_number += p->fts_statp->st_blocks;
232 if (p->fts_level <= depth) {
234 (void) prthumanval(howmany(p->fts_number, blocksize));
235 (void) printf("\t%s\n", p->fts_path);
237 (void) printf("%ld\t%s\n",
238 howmany(p->fts_number, blocksize),
243 case FTS_DC: /* Ignore. */
245 case FTS_DNR: /* Warn, continue. */
248 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
252 if (p->fts_statp->st_nlink > 1 && linkchk(p))
255 if (listall || p->fts_level == 0) {
257 (void) prthumanval(howmany(p->fts_statp->st_blocks,
259 (void) printf("\t%s\n", p->fts_path);
261 (void) printf("%qd\t%s\n",
262 howmany(p->fts_statp->st_blocks, blocksize),
267 p->fts_parent->fts_number += p->fts_statp->st_blocks;
269 savednumber = p->fts_parent->fts_number;
277 (void) prthumanval(howmany(savednumber, blocksize));
278 (void) printf("\ttotal\n");
280 (void) printf("%ld\ttotal\n", howmany(savednumber, blocksize));
299 static int maxfiles, nfiles;
304 ino = p->fts_statp->st_ino;
305 dev = p->fts_statp->st_dev;
306 if ((start = files) != NULL)
307 for (fp = start + nfiles - 1; fp >= start; --fp)
308 if (ino == fp->inode && dev == fp->dev)
311 if (nfiles == maxfiles && (files = realloc((char *)files,
312 (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
313 errx(1, "can't allocate memory");
314 files[nfiles].inode = ino;
315 files[nfiles].dev = dev;
321 * Output in "human-readable" format. Uses 3 digits max and puts
322 * unit suffixes at the end. Makes output compact and easy to read,
323 * especially on huge disks.
332 unsigned int unit_sz;
336 unit_sz = abval ? ilogb(abval) / 10 : 0;
338 if (unit_sz >= UNIT_MAX) {
341 unit = unitp[unit_sz];
342 *val /= (double)valp[unit_sz];
355 unit = unit_adjust(&bytes);
360 (void)printf("%3.0f%c", bytes, "BKMGTPE"[unit]);
362 (void)printf("%3.1f%c", bytes, "BKMGTPE"[unit]);
368 (void)fprintf(stderr,
369 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [file ...]\n");