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";
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 $");
51 #include <sys/param.h>
52 #include <sys/queue.h>
66 #define KILO_SZ(n) (n)
67 #define MEGA_SZ(n) ((n) * (n))
68 #define GIGA_SZ(n) ((n) * (n) * (n))
69 #define TERA_SZ(n) ((n) * (n) * (n) * (n))
70 #define PETA_SZ(n) ((n) * (n) * (n) * (n) * (n))
72 #define KILO_2_SZ (KILO_SZ(1024ULL))
73 #define MEGA_2_SZ (MEGA_SZ(1024ULL))
74 #define GIGA_2_SZ (GIGA_SZ(1024ULL))
75 #define TERA_2_SZ (TERA_SZ(1024ULL))
76 #define PETA_2_SZ (PETA_SZ(1024ULL))
78 #define KILO_SI_SZ (KILO_SZ(1000ULL))
79 #define MEGA_SI_SZ (MEGA_SZ(1000ULL))
80 #define GIGA_SI_SZ (GIGA_SZ(1000ULL))
81 #define TERA_SI_SZ (TERA_SZ(1000ULL))
82 #define PETA_SI_SZ (PETA_SZ(1000ULL))
84 #define TWO_TB (2LL * 1024LL * 1024LL * 1024LL * 1024LL)
86 unsigned long long vals_si
[] = {1, KILO_SI_SZ
, MEGA_SI_SZ
, GIGA_SI_SZ
, TERA_SI_SZ
, PETA_SI_SZ
};
87 unsigned long long vals_base2
[] = {1, KILO_2_SZ
, MEGA_2_SZ
, GIGA_2_SZ
, TERA_2_SZ
, PETA_2_SZ
};
88 unsigned long long *valp
;
90 typedef enum { NONE
, KILO
, MEGA
, GIGA
, TERA
, PETA
, UNIT_MAX
} unit_t
;
92 int unitp
[] = { NONE
, KILO
, MEGA
, GIGA
, TERA
, PETA
};
94 SLIST_HEAD(ignhead
, ignentry
) ignores
;
97 SLIST_ENTRY(ignentry
) next
;
100 int linkchk(FTSENT
*);
101 static void usage(void);
102 void prthumanval(double);
103 unit_t
unit_adjust(double *);
104 void ignoreadd(const char *);
105 void ignoreclean(void);
106 int ignorep(FTSENT
*);
109 main(int argc
, char *argv
[])
117 int Hflag
, Lflag
, Pflag
, aflag
, sflag
, dflag
, cflag
, hflag
, ch
, notused
, rval
;
119 static char dot
[] = ".";
120 off_t
*ftsnum
, *ftsparnum
, savednumber
= 0;
122 Hflag
= Lflag
= Pflag
= aflag
= sflag
= dflag
= cflag
= hflag
= 0;
127 SLIST_INIT(&ignores
);
129 while ((ch
= getopt(argc
, argv
, "HI:LPasd:chkrx")) != -1)
156 depth
= atoi(optarg
);
157 if (errno
== ERANGE
|| depth
< 0) {
158 warnx("invalid argument to option d: %s", optarg
);
166 putenv("BLOCKSIZE=512");
172 putenv("BLOCKSIZE=1024");
174 case 'r': /* Compatibility. */
177 ftsoptions
|= FTS_XDEV
;
189 * Because of the way that fts(3) works, logical walks will not count
190 * the blocks actually used by symbolic links. We rationalize this by
191 * noting that users computing logical sizes are likely to do logical
192 * copies, so not counting the links is correct. The real reason is
193 * that we'd have to re-implement the kernel's symbolic link traversing
194 * algorithm to get this right. If, for example, you have relative
195 * symbolic links referencing other relative symbolic links, it gets
196 * very nasty, very fast. The bottom line is that it's documented in
197 * the man page, so it's a feature.
200 if (Hflag
+ Lflag
+ Pflag
> 1)
203 if (Hflag
+ Lflag
+ Pflag
== 0)
204 Pflag
= 1; /* -P (physical) is default */
207 ftsoptions
|= FTS_COMFOLLOW
;
210 ftsoptions
|= FTS_LOGICAL
;
213 ftsoptions
|= FTS_PHYSICAL
;
233 (void) getbsize(¬used
, &blocksize
);
238 if ((fts
= fts_open(argv
, ftsoptions
, NULL
)) == NULL
)
241 while ((p
= fts_read(fts
)) != NULL
) {
242 switch (p
->fts_info
) {
243 case FTS_D
: /* Ignore. */
245 fts_set(fts
, p
, FTS_SKIP
);
251 ftsparnum
= (off_t
*)&p
->fts_parent
->fts_number
;
252 ftsnum
= (off_t
*)&p
->fts_number
;
253 if (p
->fts_statp
->st_size
< TWO_TB
) {
254 ftsparnum
[0] += ftsnum
[0] += p
->fts_statp
->st_blocks
;
256 ftsparnum
[0] += ftsnum
[0] += howmany(p
->fts_statp
->st_size
, 512LL);
259 if (p
->fts_level
<= depth
) {
261 (void) prthumanval(howmany(*ftsnum
, blocksize
));
262 (void) printf("\t%s\n", p
->fts_path
);
264 (void) printf("%lld\t%s\n",
265 howmany(*ftsnum
, blocksize
),
270 case FTS_DC
: /* Ignore. */
272 case FTS_DNR
: /* Warn, continue. */
275 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
282 if (p
->fts_statp
->st_nlink
> 1 && linkchk(p
))
285 if (listall
|| p
->fts_level
== 0) {
287 if (p
->fts_statp
->st_size
< TWO_TB
) {
288 (void) prthumanval(howmany(p
->fts_statp
->st_blocks
,
291 (void) prthumanval(howmany(howmany(p
->fts_statp
->st_size
, 512LL),
294 (void) printf("\t%s\n", p
->fts_path
);
296 if (p
->fts_statp
->st_size
< TWO_TB
) {
297 (void) printf("%qd\t%s\n",
298 (long long)howmany(p
->fts_statp
->st_blocks
, blocksize
),
301 (void) printf("%qd\t%s\n",
302 (long long)howmany(howmany(p
->fts_statp
->st_size
, 512LL), blocksize
),
308 ftsparnum
= (off_t
*)&p
->fts_parent
->fts_number
;
309 if (p
->fts_statp
->st_size
< TWO_TB
) {
310 ftsparnum
[0] += p
->fts_statp
->st_blocks
;
312 ftsparnum
[0] += p
->fts_statp
->st_size
/ 512LL;
315 savednumber
= ((off_t
*)&p
->fts_parent
->fts_number
)[0];
323 (void) prthumanval(howmany(savednumber
, blocksize
));
324 (void) printf("\ttotal\n");
326 (void) printf("%lld\ttotal\n", howmany(savednumber
, blocksize
));
345 static int maxfiles
, nfiles
;
350 ino
= p
->fts_statp
->st_ino
;
351 dev
= p
->fts_statp
->st_dev
;
352 if ((start
= files
) != NULL
)
353 for (fp
= start
+ nfiles
- 1; fp
>= start
; --fp
)
354 if (ino
== fp
->inode
&& dev
== fp
->dev
)
357 if (nfiles
== maxfiles
&& (files
= realloc((char *)files
,
358 (u_int
)(sizeof(ID
) * (maxfiles
+= 128)))) == NULL
)
359 errx(1, "can't allocate memory");
360 files
[nfiles
].inode
= ino
;
361 files
[nfiles
].dev
= dev
;
367 * Output in "human-readable" format. Uses 3 digits max and puts
368 * unit suffixes at the end. Makes output compact and easy to read,
369 * especially on huge disks.
373 unit_adjust(double *val
)
377 unsigned int unit_sz
;
381 unit_sz
= abval ?
ilogb(abval
) / 10 : 0;
383 if (unit_sz
>= UNIT_MAX
) {
386 unit
= unitp
[unit_sz
];
387 *val
/= (double)valp
[unit_sz
];
394 prthumanval(double bytes
)
399 unit
= unit_adjust(&bytes
);
404 (void)printf("%3.0f%c", bytes
, "BKMGTPE"[unit
]);
406 (void)printf("%3.1f%c", bytes
, "BKMGTPE"[unit
]);
412 (void)fprintf(stderr
,
413 "usage: du [-H | -L | -P] [-a | -s | -d depth] [-c] [-h | -k] [-x] [-I mask] [file ...]\n");
418 ignoreadd(const char *mask
)
420 struct ignentry
*ign
;
422 ign
= calloc(1, sizeof(*ign
));
424 errx(1, "cannot allocate memory");
425 ign
->mask
= strdup(mask
);
426 if (ign
->mask
== NULL
)
427 errx(1, "cannot allocate memory");
428 SLIST_INSERT_HEAD(&ignores
, ign
, next
);
434 struct ignentry
*ign
;
436 while (!SLIST_EMPTY(&ignores
)) {
437 ign
= SLIST_FIRST(&ignores
);
438 SLIST_REMOVE_HEAD(&ignores
, next
);
447 struct ignentry
*ign
;
449 SLIST_FOREACH(ign
, &ignores
, next
)
450 if (fnmatch(ign
->mask
, ent
->fts_name
, 0) != FNM_NOMATCH
)