PR/34220: Tyler Spivey: feature request: --max-depth for du

Thanks to Matt Fleming for porting the -d feature from FreeBSD!
This commit is contained in:
elad 2006-09-23 23:20:20 +00:00
parent 4df78a97d5
commit 9cd65b7c28
2 changed files with 36 additions and 13 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: du.1,v 1.19 2004/05/17 15:36:17 wiz Exp $
.\" $NetBSD: du.1,v 1.20 2006/09/23 23:20:20 elad Exp $
.\"
.\" Copyright (c) 1990, 1993
.\" The Regents of the University of California. All rights reserved.
@ -29,7 +29,7 @@
.\"
.\" @(#)du.1 8.2 (Berkeley) 4/1/94
.\"
.Dd May 17, 2004
.Dd September 24, 2006
.Dt DU 1
.Os
.Sh NAME
@ -38,7 +38,7 @@
.Sh SYNOPSIS
.Nm
.Op Fl H | Fl L | Fl P
.Op Fl a | Fl s
.Op Fl a | Fl d Ar depth | Fl s
.Op Fl cghkmnrx
.Op Ar file ...
.Sh DESCRIPTION
@ -63,6 +63,10 @@ No symbolic links are followed.
Display an entry for each file in the file hierarchy.
.It Fl c
Display the grand total after all the arguments have been processed.
.It Fl d
Display an entry files and directories
.Ar depth
directories deep.
.It Fl g
If the
.Fl g

View File

@ -1,4 +1,4 @@
/* $NetBSD: du.c,v 1.29 2006/05/10 23:35:03 lukem Exp $ */
/* $NetBSD: du.c,v 1.30 2006/09/23 23:20:20 elad Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
#if 0
static char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
#else
__RCSID("$NetBSD: du.c,v 1.29 2006/05/10 23:35:03 lukem Exp $");
__RCSID("$NetBSD: du.c,v 1.30 2006/09/23 23:20:20 elad Exp $");
#endif
#endif /* not lint */
@ -74,13 +74,15 @@ main(int argc, char *argv[])
FTSENT *p;
int64_t totalblocks;
int ftsoptions, listdirs, listfiles;
int Hflag, Lflag, aflag, ch, cflag, gkmflag, nflag, rval, sflag;
int depth;
int Hflag, Lflag, aflag, ch, cflag, dflag, gkmflag, nflag, rval, sflag;
const char *noargv[2];
Hflag = Lflag = aflag = cflag = gkmflag = nflag = sflag = 0;
Hflag = Lflag = aflag = cflag = dflag = gkmflag = nflag = sflag = 0;
totalblocks = 0;
ftsoptions = FTS_PHYSICAL;
while ((ch = getopt(argc, argv, "HLPacghkmnrsx")) != -1)
depth = INT_MAX;
while ((ch = getopt(argc, argv, "HLPacd:ghkmnrsx")) != -1)
switch (ch) {
case 'H':
Hflag = 1;
@ -99,6 +101,15 @@ main(int argc, char *argv[])
case 'c':
cflag = 1;
break;
case 'd':
dflag = 1;
depth = atoi(optarg);
if (depth < 0 || depth > SHRT_MAX) {
warnx("invalid argument to option d: %s",
optarg);
usage();
}
break;
case 'g':
blocksize = 1024 * 1024 * 1024;
gkmflag = 1;
@ -152,12 +163,14 @@ main(int argc, char *argv[])
}
if (aflag) {
if (sflag)
if (sflag || dflag)
usage();
listdirs = listfiles = 1;
} else if (sflag)
listdirs = listfiles = 0;
else {
} else if (sflag) {
if (dflag)
usage();
listdirs = listfiles = depth = 0;
} else {
listfiles = 0;
listdirs = 1;
}
@ -195,6 +208,12 @@ main(int argc, char *argv[])
case FTS_DP:
p->fts_parent->fts_number +=
p->fts_number += p->fts_statp->st_blocks;
if (p->fts_level > depth) {
fts_set(fts, p, FTS_SKIP);
continue;
}
if (cflag)
totalblocks += p->fts_statp->st_blocks;
/*
@ -332,6 +351,6 @@ usage(void)
{
(void)fprintf(stderr,
"usage: du [-H | -L | -P] [-a | -s] [-cghkmnrx] [file ...]\n");
"usage: du [-H | -L | -P] [-a | | -d depth | -s] [-cghkmnrx] [file ...]\n");
exit(1);
}