1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1989, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Chris Newcomb.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-12-06 22:58:23 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1989, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
2000-01-25 17:46:59 +00:00
|
|
|
#if 0
|
1998-12-06 22:58:23 +00:00
|
|
|
static const char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
|
2000-01-25 17:46:59 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:25:07 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1997-12-29 20:56:21 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <sys/param.h>
|
2001-06-13 06:38:58 +00:00
|
|
|
#include <sys/queue.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
2001-06-13 06:38:58 +00:00
|
|
|
#include <fnmatch.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <fts.h>
|
2004-05-24 22:22:29 +00:00
|
|
|
#include <libutil.h>
|
2004-07-15 08:54:41 +00:00
|
|
|
#include <locale.h>
|
2005-01-07 00:12:24 +00:00
|
|
|
#include <stdint.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2000-01-25 17:46:59 +00:00
|
|
|
#include <sysexits.h>
|
1997-03-11 13:43:33 +00:00
|
|
|
#include <unistd.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-06-13 06:38:58 +00:00
|
|
|
SLIST_HEAD(ignhead, ignentry) ignores;
|
|
|
|
struct ignentry {
|
|
|
|
char *mask;
|
|
|
|
SLIST_ENTRY(ignentry) next;
|
|
|
|
};
|
|
|
|
|
2004-04-30 18:17:51 +00:00
|
|
|
static int linkchk(FTSENT *);
|
2002-03-22 01:22:50 +00:00
|
|
|
static void usage(void);
|
2008-11-04 19:23:48 +00:00
|
|
|
static void prthumanval(int64_t);
|
|
|
|
static void ignoreadd(const char *);
|
|
|
|
static void ignoreclean(void);
|
|
|
|
static int ignorep(FTSENT *);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2008-11-04 19:23:48 +00:00
|
|
|
static int nodumpflag = 0;
|
2008-11-06 16:30:38 +00:00
|
|
|
static int Aflag;
|
|
|
|
static long blocksize, cblocksize;
|
2006-05-06 22:04:59 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
int
|
2002-07-28 15:50:38 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
1997-12-29 20:56:21 +00:00
|
|
|
FTS *fts;
|
|
|
|
FTSENT *p;
|
2008-11-06 16:30:38 +00:00
|
|
|
off_t savednumber, curblocks;
|
1997-12-29 20:56:21 +00:00
|
|
|
int ftsoptions;
|
|
|
|
int listall;
|
|
|
|
int depth;
|
2008-02-25 19:06:43 +00:00
|
|
|
int Hflag, Lflag, Pflag, aflag, sflag, dflag, cflag;
|
|
|
|
int hflag, lflag, ch, notused, rval;
|
1997-12-29 20:56:21 +00:00
|
|
|
char **save;
|
2001-12-02 13:48:40 +00:00
|
|
|
static char dot[] = ".";
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2004-07-15 08:54:41 +00:00
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
2008-02-25 19:06:43 +00:00
|
|
|
Hflag = Lflag = Pflag = aflag = sflag = dflag = cflag = hflag =
|
2008-11-06 16:30:38 +00:00
|
|
|
lflag = Aflag = 0;
|
2004-04-30 18:17:51 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
save = argv;
|
1997-12-29 20:56:21 +00:00
|
|
|
ftsoptions = 0;
|
2008-11-04 19:17:32 +00:00
|
|
|
savednumber = 0;
|
2008-11-06 16:30:38 +00:00
|
|
|
cblocksize = DEV_BSIZE;
|
|
|
|
blocksize = 0;
|
1996-10-23 06:53:57 +00:00
|
|
|
depth = INT_MAX;
|
2001-06-13 06:38:58 +00:00
|
|
|
SLIST_INIT(&ignores);
|
2004-04-30 18:17:51 +00:00
|
|
|
|
2008-11-06 16:30:38 +00:00
|
|
|
while ((ch = getopt(argc, argv, "AB:HI:LPasd:chklmnrx")) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch (ch) {
|
2008-11-06 16:30:38 +00:00
|
|
|
case 'A':
|
|
|
|
Aflag = 1;
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
errno = 0;
|
|
|
|
cblocksize = atoi(optarg);
|
|
|
|
if (errno == ERANGE || cblocksize <= 0) {
|
|
|
|
warnx("invalid argument to option B: %s",
|
|
|
|
optarg);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
2008-11-04 19:17:32 +00:00
|
|
|
case 'H':
|
|
|
|
Hflag = 1;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
ignoreadd(optarg);
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
if (Pflag)
|
|
|
|
usage();
|
|
|
|
Lflag = 1;
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
if (Lflag)
|
1996-10-23 06:53:57 +00:00
|
|
|
usage();
|
2008-11-04 19:17:32 +00:00
|
|
|
Pflag = 1;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
dflag = 1;
|
|
|
|
errno = 0;
|
|
|
|
depth = atoi(optarg);
|
|
|
|
if (errno == ERANGE || depth < 0) {
|
|
|
|
warnx("invalid argument to option d: %s",
|
|
|
|
optarg);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
cflag = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
hflag = 1;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
hflag = 0;
|
2008-11-06 16:30:38 +00:00
|
|
|
blocksize = 1024;
|
2008-11-04 19:17:32 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
lflag = 1;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
hflag = 0;
|
2008-11-06 16:30:38 +00:00
|
|
|
blocksize = 1048576;
|
2008-11-04 19:17:32 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nodumpflag = 1;
|
|
|
|
break;
|
|
|
|
case 'r': /* Compatibility. */
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
ftsoptions |= FTS_XDEV;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/* NOTREACHED */
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
1997-12-29 20:56:21 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* Because of the way that fts(3) works, logical walks will not count
|
|
|
|
* the blocks actually used by symbolic links. We rationalize this by
|
|
|
|
* noting that users computing logical sizes are likely to do logical
|
|
|
|
* copies, so not counting the links is correct. The real reason is
|
|
|
|
* that we'd have to re-implement the kernel's symbolic link traversing
|
|
|
|
* algorithm to get this right. If, for example, you have relative
|
|
|
|
* symbolic links referencing other relative symbolic links, it gets
|
|
|
|
* very nasty, very fast. The bottom line is that it's documented in
|
|
|
|
* the man page, so it's a feature.
|
|
|
|
*/
|
1997-12-29 20:56:21 +00:00
|
|
|
|
|
|
|
if (Hflag + Lflag + Pflag > 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (Hflag + Lflag + Pflag == 0)
|
|
|
|
Pflag = 1; /* -P (physical) is default */
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if (Hflag)
|
|
|
|
ftsoptions |= FTS_COMFOLLOW;
|
1997-12-29 20:56:21 +00:00
|
|
|
|
|
|
|
if (Lflag)
|
1994-05-27 12:33:43 +00:00
|
|
|
ftsoptions |= FTS_LOGICAL;
|
1997-12-29 20:56:21 +00:00
|
|
|
|
|
|
|
if (Pflag)
|
|
|
|
ftsoptions |= FTS_PHYSICAL;
|
|
|
|
|
2008-11-06 16:30:38 +00:00
|
|
|
if (!Aflag && (cblocksize % DEV_BSIZE) != 0)
|
|
|
|
cblocksize = howmany(cblocksize, DEV_BSIZE) * DEV_BSIZE;
|
|
|
|
|
1997-12-29 20:56:21 +00:00
|
|
|
listall = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (aflag) {
|
1996-10-23 06:53:57 +00:00
|
|
|
if (sflag || dflag)
|
1994-05-27 12:33:43 +00:00
|
|
|
usage();
|
1997-12-29 20:56:21 +00:00
|
|
|
listall = 1;
|
1996-10-23 06:53:57 +00:00
|
|
|
} else if (sflag) {
|
|
|
|
if (dflag)
|
|
|
|
usage();
|
1997-12-29 20:56:21 +00:00
|
|
|
depth = 0;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!*argv) {
|
|
|
|
argv = save;
|
2001-12-02 13:48:40 +00:00
|
|
|
argv[0] = dot;
|
1994-05-27 12:33:43 +00:00
|
|
|
argv[1] = NULL;
|
|
|
|
}
|
|
|
|
|
2008-11-06 16:30:38 +00:00
|
|
|
if (blocksize == 0)
|
|
|
|
(void)getbsize(¬used, &blocksize);
|
|
|
|
|
|
|
|
if (!Aflag) {
|
|
|
|
cblocksize /= DEV_BSIZE;
|
|
|
|
blocksize /= DEV_BSIZE;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1997-12-29 20:56:21 +00:00
|
|
|
rval = 0;
|
2004-04-30 18:17:51 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
|
1997-12-29 20:56:21 +00:00
|
|
|
err(1, "fts_open");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
1997-12-29 20:56:21 +00:00
|
|
|
while ((p = fts_read(fts)) != NULL) {
|
1994-05-27 12:33:43 +00:00
|
|
|
switch (p->fts_info) {
|
2008-11-04 19:17:32 +00:00
|
|
|
case FTS_D: /* Ignore. */
|
|
|
|
if (ignorep(p))
|
|
|
|
fts_set(fts, p, FTS_SKIP);
|
|
|
|
break;
|
|
|
|
case FTS_DP:
|
|
|
|
if (ignorep(p))
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2008-11-04 19:17:32 +00:00
|
|
|
|
2008-11-06 16:30:38 +00:00
|
|
|
curblocks = Aflag ?
|
|
|
|
howmany(p->fts_statp->st_size, cblocksize) :
|
|
|
|
howmany(p->fts_statp->st_blocks, cblocksize);
|
2008-11-04 19:17:32 +00:00
|
|
|
p->fts_parent->fts_bignum += p->fts_bignum +=
|
2008-11-06 16:30:38 +00:00
|
|
|
curblocks;
|
2008-11-04 19:17:32 +00:00
|
|
|
|
|
|
|
if (p->fts_level <= depth) {
|
|
|
|
if (hflag) {
|
2008-11-06 16:30:38 +00:00
|
|
|
prthumanval(p->fts_bignum);
|
2008-11-04 19:17:32 +00:00
|
|
|
(void)printf("\t%s\n", p->fts_path);
|
|
|
|
} else {
|
|
|
|
(void)printf("%jd\t%s\n",
|
2008-11-06 23:55:28 +00:00
|
|
|
(intmax_t)howmany(p->fts_bignum *
|
|
|
|
cblocksize, blocksize),
|
|
|
|
p->fts_path);
|
2000-03-26 14:21:57 +00:00
|
|
|
}
|
2008-11-04 19:17:32 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FTS_DC: /* Ignore. */
|
|
|
|
break;
|
|
|
|
case FTS_DNR: /* Warn, continue. */
|
|
|
|
case FTS_ERR:
|
|
|
|
case FTS_NS:
|
|
|
|
warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
|
|
|
|
rval = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (ignorep(p))
|
1997-12-29 20:56:21 +00:00
|
|
|
break;
|
2008-11-04 19:17:32 +00:00
|
|
|
|
|
|
|
if (lflag == 0 && p->fts_statp->st_nlink > 1 &&
|
|
|
|
linkchk(p))
|
1997-12-29 20:56:21 +00:00
|
|
|
break;
|
2008-11-04 19:17:32 +00:00
|
|
|
|
2008-11-06 16:30:38 +00:00
|
|
|
curblocks = Aflag ?
|
|
|
|
howmany(p->fts_statp->st_size, cblocksize) :
|
|
|
|
howmany(p->fts_statp->st_blocks, cblocksize);
|
|
|
|
|
2008-11-04 19:17:32 +00:00
|
|
|
if (listall || p->fts_level == 0) {
|
|
|
|
if (hflag) {
|
2008-11-06 16:30:38 +00:00
|
|
|
prthumanval(curblocks);
|
2008-11-04 19:17:32 +00:00
|
|
|
(void)printf("\t%s\n", p->fts_path);
|
|
|
|
} else {
|
|
|
|
(void)printf("%jd\t%s\n",
|
2008-11-06 23:55:28 +00:00
|
|
|
(intmax_t)howmany(curblocks *
|
|
|
|
cblocksize, blocksize),
|
|
|
|
p->fts_path);
|
2000-03-26 14:21:57 +00:00
|
|
|
}
|
2008-11-04 19:17:32 +00:00
|
|
|
}
|
1997-12-29 20:56:21 +00:00
|
|
|
|
2008-11-06 16:30:38 +00:00
|
|
|
p->fts_parent->fts_bignum += curblocks;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2005-01-07 00:12:24 +00:00
|
|
|
savednumber = p->fts_parent->fts_bignum;
|
1997-12-29 20:56:21 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
if (errno)
|
|
|
|
err(1, "fts_read");
|
1997-12-29 20:56:21 +00:00
|
|
|
|
2000-03-26 14:21:57 +00:00
|
|
|
if (cflag) {
|
2000-01-25 17:46:59 +00:00
|
|
|
if (hflag) {
|
2008-11-06 16:30:38 +00:00
|
|
|
prthumanval(savednumber);
|
2008-11-04 19:17:32 +00:00
|
|
|
(void)printf("\ttotal\n");
|
2000-01-25 17:46:59 +00:00
|
|
|
} else {
|
2008-11-04 19:17:32 +00:00
|
|
|
(void)printf("%jd\ttotal\n", (intmax_t)howmany(
|
2008-11-06 16:30:38 +00:00
|
|
|
savednumber * cblocksize, blocksize));
|
2000-01-25 17:46:59 +00:00
|
|
|
}
|
2000-03-26 14:21:57 +00:00
|
|
|
}
|
1997-12-29 20:56:21 +00:00
|
|
|
|
2001-06-13 06:38:58 +00:00
|
|
|
ignoreclean();
|
1997-08-29 11:40:00 +00:00
|
|
|
exit(rval);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2004-04-30 18:17:51 +00:00
|
|
|
static int
|
|
|
|
linkchk(FTSENT *p)
|
|
|
|
{
|
|
|
|
struct links_entry {
|
2004-05-01 21:47:31 +00:00
|
|
|
struct links_entry *next;
|
|
|
|
struct links_entry *previous;
|
|
|
|
int links;
|
|
|
|
dev_t dev;
|
|
|
|
ino_t ino;
|
2004-04-30 18:17:51 +00:00
|
|
|
};
|
2004-05-01 21:47:31 +00:00
|
|
|
static const size_t links_hash_initial_size = 8192;
|
|
|
|
static struct links_entry **buckets;
|
|
|
|
static struct links_entry *free_list;
|
|
|
|
static size_t number_buckets;
|
|
|
|
static unsigned long number_entries;
|
|
|
|
static char stop_allocating;
|
|
|
|
struct links_entry *le, **new_buckets;
|
|
|
|
struct stat *st;
|
|
|
|
size_t i, new_size;
|
2005-04-09 14:31:41 +00:00
|
|
|
int hash;
|
2004-04-30 18:17:51 +00:00
|
|
|
|
|
|
|
st = p->fts_statp;
|
|
|
|
|
|
|
|
/* If necessary, initialize the hash table. */
|
|
|
|
if (buckets == NULL) {
|
|
|
|
number_buckets = links_hash_initial_size;
|
|
|
|
buckets = malloc(number_buckets * sizeof(buckets[0]));
|
|
|
|
if (buckets == NULL)
|
2004-05-02 17:54:57 +00:00
|
|
|
errx(1, "No memory for hardlink detection");
|
2004-04-30 18:17:51 +00:00
|
|
|
for (i = 0; i < number_buckets; i++)
|
|
|
|
buckets[i] = NULL;
|
|
|
|
}
|
1997-12-29 20:56:21 +00:00
|
|
|
|
2004-04-30 18:17:51 +00:00
|
|
|
/* If the hash table is getting too full, enlarge it. */
|
|
|
|
if (number_entries > number_buckets * 10 && !stop_allocating) {
|
|
|
|
new_size = number_buckets * 2;
|
|
|
|
new_buckets = malloc(new_size * sizeof(struct links_entry *));
|
|
|
|
|
|
|
|
/* Try releasing the free list to see if that helps. */
|
|
|
|
if (new_buckets == NULL && free_list != NULL) {
|
|
|
|
while (free_list != NULL) {
|
|
|
|
le = free_list;
|
|
|
|
free_list = le->next;
|
|
|
|
free(le);
|
|
|
|
}
|
2008-11-04 19:17:32 +00:00
|
|
|
new_buckets = malloc(new_size *
|
|
|
|
sizeof(new_buckets[0]));
|
2004-04-30 18:17:51 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2004-04-30 18:17:51 +00:00
|
|
|
if (new_buckets == NULL) {
|
|
|
|
stop_allocating = 1;
|
2004-05-02 17:54:57 +00:00
|
|
|
warnx("No more memory for tracking hard links");
|
2004-04-30 18:17:51 +00:00
|
|
|
} else {
|
|
|
|
memset(new_buckets, 0,
|
|
|
|
new_size * sizeof(struct links_entry *));
|
|
|
|
for (i = 0; i < number_buckets; i++) {
|
|
|
|
while (buckets[i] != NULL) {
|
|
|
|
/* Remove entry from old bucket. */
|
|
|
|
le = buckets[i];
|
|
|
|
buckets[i] = le->next;
|
|
|
|
|
|
|
|
/* Add entry to new bucket. */
|
|
|
|
hash = (le->dev ^ le->ino) % new_size;
|
|
|
|
|
|
|
|
if (new_buckets[hash] != NULL)
|
|
|
|
new_buckets[hash]->previous =
|
|
|
|
le;
|
|
|
|
le->next = new_buckets[hash];
|
|
|
|
le->previous = NULL;
|
|
|
|
new_buckets[hash] = le;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(buckets);
|
|
|
|
buckets = new_buckets;
|
|
|
|
number_buckets = new_size;
|
|
|
|
}
|
|
|
|
}
|
1997-12-29 20:56:21 +00:00
|
|
|
|
2004-04-30 18:17:51 +00:00
|
|
|
/* Try to locate this entry in the hash table. */
|
|
|
|
hash = ( st->st_dev ^ st->st_ino ) % number_buckets;
|
|
|
|
for (le = buckets[hash]; le != NULL; le = le->next) {
|
|
|
|
if (le->dev == st->st_dev && le->ino == st->st_ino) {
|
|
|
|
/*
|
|
|
|
* Save memory by releasing an entry when we've seen
|
|
|
|
* all of it's links.
|
|
|
|
*/
|
|
|
|
if (--le->links <= 0) {
|
|
|
|
if (le->previous != NULL)
|
|
|
|
le->previous->next = le->next;
|
|
|
|
if (le->next != NULL)
|
|
|
|
le->next->previous = le->previous;
|
|
|
|
if (buckets[hash] == le)
|
|
|
|
buckets[hash] = le->next;
|
|
|
|
number_entries--;
|
|
|
|
/* Recycle this node through the free list */
|
|
|
|
if (stop_allocating) {
|
|
|
|
free(le);
|
|
|
|
} else {
|
|
|
|
le->next = free_list;
|
|
|
|
free_list = le;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stop_allocating)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* Add this entry to the links cache. */
|
|
|
|
if (free_list != NULL) {
|
|
|
|
/* Pull a node from the free list if we can. */
|
|
|
|
le = free_list;
|
|
|
|
free_list = le->next;
|
|
|
|
} else
|
|
|
|
/* Malloc one if we have to. */
|
|
|
|
le = malloc(sizeof(struct links_entry));
|
|
|
|
if (le == NULL) {
|
|
|
|
stop_allocating = 1;
|
2004-05-02 17:54:57 +00:00
|
|
|
warnx("No more memory for tracking hard links");
|
2004-04-30 18:17:51 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
le->dev = st->st_dev;
|
|
|
|
le->ino = st->st_ino;
|
|
|
|
le->links = st->st_nlink - 1;
|
|
|
|
number_entries++;
|
|
|
|
le->next = buckets[hash];
|
|
|
|
le->previous = NULL;
|
|
|
|
if (buckets[hash] != NULL)
|
|
|
|
buckets[hash]->previous = le;
|
|
|
|
buckets[hash] = le;
|
1994-05-27 12:33:43 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-11-04 19:23:48 +00:00
|
|
|
static void
|
2004-05-24 22:22:29 +00:00
|
|
|
prthumanval(int64_t bytes)
|
2000-01-25 17:46:59 +00:00
|
|
|
{
|
2004-05-24 22:22:29 +00:00
|
|
|
char buf[5];
|
|
|
|
|
2008-11-06 16:30:38 +00:00
|
|
|
bytes *= cblocksize;
|
|
|
|
if (!Aflag)
|
|
|
|
bytes *= DEV_BSIZE;
|
2000-01-25 17:46:59 +00:00
|
|
|
|
2004-05-24 22:22:29 +00:00
|
|
|
humanize_number(buf, sizeof(buf), bytes, "", HN_AUTOSCALE,
|
|
|
|
HN_B | HN_NOSPACE | HN_DECIMAL);
|
2000-01-25 17:46:59 +00:00
|
|
|
|
2004-05-24 22:22:29 +00:00
|
|
|
(void)printf("%4s", buf);
|
2000-01-25 17:46:59 +00:00
|
|
|
}
|
|
|
|
|
1997-06-30 06:50:07 +00:00
|
|
|
static void
|
2002-07-28 15:50:38 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
2008-11-06 16:30:38 +00:00
|
|
|
"usage: du [-A] [-H | -L | -P] [-a | -s | -d depth] [-c] "
|
|
|
|
"[-l] [-h | -k | -m | -B bsize] [-n] [-x] [-I mask] "
|
|
|
|
"[file ...]\n");
|
2000-01-25 17:46:59 +00:00
|
|
|
exit(EX_USAGE);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-06-13 06:38:58 +00:00
|
|
|
|
2008-11-04 19:23:48 +00:00
|
|
|
static void
|
2002-07-28 15:50:38 +00:00
|
|
|
ignoreadd(const char *mask)
|
2001-06-13 06:38:58 +00:00
|
|
|
{
|
|
|
|
struct ignentry *ign;
|
|
|
|
|
|
|
|
ign = calloc(1, sizeof(*ign));
|
|
|
|
if (ign == NULL)
|
|
|
|
errx(1, "cannot allocate memory");
|
|
|
|
ign->mask = strdup(mask);
|
|
|
|
if (ign->mask == NULL)
|
|
|
|
errx(1, "cannot allocate memory");
|
|
|
|
SLIST_INSERT_HEAD(&ignores, ign, next);
|
|
|
|
}
|
|
|
|
|
2008-11-04 19:23:48 +00:00
|
|
|
static void
|
2002-07-28 15:50:38 +00:00
|
|
|
ignoreclean(void)
|
2001-06-13 06:38:58 +00:00
|
|
|
{
|
|
|
|
struct ignentry *ign;
|
|
|
|
|
|
|
|
while (!SLIST_EMPTY(&ignores)) {
|
|
|
|
ign = SLIST_FIRST(&ignores);
|
|
|
|
SLIST_REMOVE_HEAD(&ignores, next);
|
|
|
|
free(ign->mask);
|
|
|
|
free(ign);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-04 19:23:48 +00:00
|
|
|
static int
|
2002-07-28 15:50:38 +00:00
|
|
|
ignorep(FTSENT *ent)
|
2001-06-13 06:38:58 +00:00
|
|
|
{
|
|
|
|
struct ignentry *ign;
|
|
|
|
|
2006-05-06 22:04:59 +00:00
|
|
|
if (nodumpflag && (ent->fts_statp->st_flags & UF_NODUMP))
|
|
|
|
return 1;
|
2001-06-13 06:38:58 +00:00
|
|
|
SLIST_FOREACH(ign, &ignores, next)
|
|
|
|
if (fnmatch(ign->mask, ent->fts_name, 0) != FNM_NOMATCH)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|