summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@cvs.openbsd.org>2014-10-17 14:46:55 +0000
committerIngo Schwarze <schwarze@cvs.openbsd.org>2014-10-17 14:46:55 +0000
commitc9d4d1543ecb0f2892773131524ec605c6a9c785 (patch)
tree6c8b5b0716a49d09f79aff4ec1b8d0382e8c7cca /usr.bin
parenta4ee629caad38367b057c82adf9726355ad4459b (diff)
Add the -d flag (limit display depth) for compatibility with FreeBSD,
DragonFly, NetBSD and GNU coreutils, even though it's not POSIX. Actually, this simplifies the code rather than complicating it. Because -a and -d need not be mutually exclusive (as observed by millert@) and -s is identical to -d 0, -a and -s are no longer mutually exclusive, but -as and -sa are now the same as -s. That is explicitly allowed by POSIX. Based on a patch from William Orr <will at worrbase dot com>, but extensively massaged and HISTORY added by me. feedback and ok millert@
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/du/du.180
-rw-r--r--usr.bin/du/du.c44
2 files changed, 92 insertions, 32 deletions
diff --git a/usr.bin/du/du.1 b/usr.bin/du/du.1
index 0097b28e15e..2626cdc7b49 100644
--- a/usr.bin/du/du.1
+++ b/usr.bin/du/du.1
@@ -1,4 +1,4 @@
-.\" $OpenBSD: du.1,v 1.31 2014/02/14 18:17:50 schwarze Exp $
+.\" $OpenBSD: du.1,v 1.32 2014/10/17 14:46:54 schwarze Exp $
.\" $NetBSD: du.1,v 1.6 1996/10/18 07:20:31 thorpej Exp $
.\"
.\" Copyright (c) 1990, 1993
@@ -30,7 +30,7 @@
.\"
.\" @(#)du.1 8.2 (Berkeley) 4/1/94
.\"
-.Dd $Mdocdate: February 14 2014 $
+.Dd $Mdocdate: October 17 2014 $
.Dt DU 1
.Os
.Sh NAME
@@ -38,8 +38,8 @@
.Nd display disk usage statistics
.Sh SYNOPSIS
.Nm du
-.Op Fl a | s
-.Op Fl chkrx
+.Op Fl d Ar depth
+.Op Fl achkrsx
.Op Fl H | L | P
.Op Ar
.Sh DESCRIPTION
@@ -58,9 +58,21 @@ the current directory is displayed.
The options are as follows:
.Bl -tag -width Ds
.It Fl a
-Display an entry for each file in the file hierarchy.
+Display entries for files in addition to entries for directories.
.It Fl c
Display the grand total after all the arguments have been processed.
+.It Fl d Ar depth
+Do not display entries for files and directories more than
+.Ar depth
+levels deep;
+.Fl d Cm 0
+has the same effect as
+.Fl s .
+Overrides earlier
+.Fl d
+and
+.Fl s
+options.
.It Fl H
Symbolic links on the command line are followed.
Symbolic links encountered in the tree traversal are not followed.
@@ -83,7 +95,10 @@ Generate messages about directories that cannot be read, files
that cannot be opened, and so on.
This is the default.
.It Fl s
-Display only the grand total for the specified files.
+Display only the total for each of the specified files and directories.
+Overrides earlier
+.Fl d
+options.
.It Fl x
File system mount points are not traversed.
.El
@@ -145,7 +160,7 @@ utility is compliant with the
specification.
.Pp
The flags
-.Op Fl chP ,
+.Op Fl cdhP ,
as well as the
.Ev BLOCKSIZE
environment variable,
@@ -158,10 +173,55 @@ the obsolete
.St -xcu5
standard.
.Sh HISTORY
-A
+The
.Nm
-command first appeared in
-.At v3 .
+utility and its
+.Fl a
+and
+.Fl s
+options first appeared in
+.At v1 .
+.Pp
+The
+.Fl r
+option first appeared in
+.At III
+and is available since
+.Ox 2.3 .
+The
+.Fl k
+and
+.Fl x
+options first appeared in
+.Bx 4.3 Reno
+and
+.Fl H
+in
+.Bx 4.4 .
+The
+.Fl c
+and
+.Fl L
+options first appeared in the GNU fileutils;
+.Fl L
+and
+.Fl P
+are available since
+.Bx 4.4 Lite1 ,
+.Fl c
+since
+.Ox 2.1 .
+The
+.Fl d
+option first appeared in
+.Fx 2.2
+and is available since
+.Ox 5.7 ,
+.Fl h
+first appeared in
+.Fx 4.0
+and is available since
+.Ox 2.9 .
.Sh AUTHORS
.An -nosplit
This version of
diff --git a/usr.bin/du/du.c b/usr.bin/du/du.c
index 550af7ec062..1764ab6d447 100644
--- a/usr.bin/du/du.c
+++ b/usr.bin/du/du.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: du.c,v 1.25 2014/05/20 01:25:23 guenther Exp $ */
+/* $OpenBSD: du.c,v 1.26 2014/10/17 14:46:54 schwarze Exp $ */
/* $NetBSD: du.c,v 1.11 1996/10/18 07:20:35 thorpej Exp $ */
/*
@@ -40,6 +40,7 @@
#include <err.h>
#include <errno.h>
#include <fts.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -59,16 +60,18 @@ main(int argc, char *argv[])
FTSENT *p;
long blocksize;
quad_t totalblocks;
- int ftsoptions, listdirs, listfiles;
- int Hflag, Lflag, aflag, cflag, hflag, kflag, sflag;
+ int ftsoptions, listfiles, maxdepth;
+ int Hflag, Lflag, cflag, hflag, kflag;
int ch, notused, rval;
char **save;
+ const char *errstr;
save = argv;
- Hflag = Lflag = aflag = cflag = hflag = kflag = sflag = 0;
+ Hflag = Lflag = cflag = hflag = kflag = listfiles = 0;
totalblocks = 0;
ftsoptions = FTS_PHYSICAL;
- while ((ch = getopt(argc, argv, "HLPachksxr")) != -1)
+ maxdepth = -1;
+ while ((ch = getopt(argc, argv, "HLPacd:hkrsx")) != -1)
switch (ch) {
case 'H':
Hflag = 1;
@@ -82,11 +85,18 @@ main(int argc, char *argv[])
Hflag = Lflag = 0;
break;
case 'a':
- aflag = 1;
+ listfiles = 1;
break;
case 'c':
cflag = 1;
break;
+ case 'd':
+ maxdepth = strtonum(optarg, 0, INT_MAX, &errstr);
+ if (errstr) {
+ warnx("max depth %s: %s", optarg, errstr);
+ usage();
+ }
+ break;
case 'h':
hflag = 1;
kflag = 0;
@@ -96,7 +106,7 @@ main(int argc, char *argv[])
hflag = 0;
break;
case 's':
- sflag = 1;
+ maxdepth = 0;
break;
case 'r':
break;
@@ -129,16 +139,8 @@ main(int argc, char *argv[])
ftsoptions |= FTS_LOGICAL;
}
- if (aflag) {
- if (sflag)
- usage();
- listdirs = listfiles = 1;
- } else if (sflag)
- listdirs = listfiles = 0;
- else {
- listfiles = 0;
- listdirs = 1;
- }
+ if (maxdepth == -1)
+ maxdepth = INT_MAX;
if (!*argv) {
argv = save;
@@ -171,12 +173,10 @@ main(int argc, char *argv[])
* or directories and this is post-order of the
* root of a traversal, display the total.
*/
- if (listdirs ||
- (!listfiles && p->fts_level == FTS_ROOTLEVEL)) {
+ if (p->fts_level <= maxdepth)
prtout((quad_t)howmany(p->fts_number,
(unsigned long)blocksize), p->fts_path,
hflag);
- }
break;
case FTS_DC: /* Ignore. */
break;
@@ -193,7 +193,7 @@ main(int argc, char *argv[])
* If listing each file, or a non-directory file was
* the root of a traversal, display the total.
*/
- if (listfiles || p->fts_level == FTS_ROOTLEVEL)
+ if (listfiles && p->fts_level <= maxdepth)
prtout(howmany(p->fts_statp->st_blocks,
blocksize), p->fts_path, hflag);
p->fts_parent->fts_number += p->fts_statp->st_blocks;
@@ -315,6 +315,6 @@ usage(void)
{
(void)fprintf(stderr,
- "usage: du [-a | -s] [-chkrx] [-H | -L | -P] [file ...]\n");
+ "usage: du [-d depth] [-achkrsx] [-H | -L | -P] [file ...]\n");
exit(1);
}