Subject: bin/5330: new feature in du
To: None <gnats-bugs@gnats.netbsd.org>
From: None <jnilsson@ludd.luth.se>
List: netbsd-bugs
Date: 04/20/1998 11:28:29
>Number:         5330
>Category:       bin
>Synopsis:       new feature in du
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Mon Apr 20 02:35:01 1998
>Last-Modified:
>Originator:     Jens A Nilsson
>Organization:
>Release:        NetBSD-1.3
>Environment:
	
System: NetBSD subzero 1.3 NetBSD 1.3 (SUBZERO) #9: Wed Mar 18 14:38:45 CET 1998 jens@subzero:/usr/src/sys/arch/i386/compile/SUBZERO i386


>Description:
FreeBSD has a -d option to du which is rather nice and tells how deep
du will display directories. 
>How-To-Repeat:
	
>Fix:
Here is the patch to implement this feature and patch for man page to.
Most of the new code is stolen from FreeBSD-2.2.6
If you want patches in another way than this please tell me.

diff -u /usr/src/usr.bin/du/du.1 ./du.1
--- /usr/src/usr.bin/du/du.1    Sat Oct 18 21:30:10 1997
+++ ./du.1      Mon Apr 20 11:19:04 1998
@@ -42,7 +42,7 @@
 .Sh SYNOPSIS
 .Nm
 .Op Fl H | Fl L | Fl P
-.Op Fl a | Fl s
+.Op Fl a | s | d Ar depth
 .Op Fl ckx
 .Op Ar file ...
 .Sh DESCRIPTION
@@ -65,6 +65,10 @@
 No symbolic links are followed.
 .It Fl a
 Display an entry for each file in the file hierarchy.
+.It Fl d Ar depth
+Displays all directories only
+.Ar depth
+directories deep.
 .It Fl k
 By default,
 .Nm
diff -u /usr/src/usr.bin/du/du.c ./du.c
--- /usr/src/usr.bin/du/du.c    Sun Oct 19 16:16:52 1997
+++ ./du.c      Mon Apr 20 10:50:40 1998
@@ -53,6 +53,8 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include <machine/limits.h>
+
 #include <dirent.h>
 #include <err.h>
 #include <errno.h>
@@ -74,15 +76,16 @@
        FTS *fts;
        FTSENT *p;
        long blocksize, totalblocks;
-       int ftsoptions, listdirs, listfiles;
-       int Hflag, Lflag, Pflag, aflag, ch, cflag, kflag, notused, rval, sflag;
+       int ftsoptions, listdirs, listfiles, depth;
+       int Hflag, Lflag, Pflag, aflag, ch, cflag, kflag, notused, rval, sflag, 
dflag;
        char **save;
 
        save = argv;
-       Hflag = Lflag = Pflag = aflag = cflag = kflag = sflag = 0;
+       Hflag = Lflag = Pflag = aflag = cflag = kflag = sflag = dflag = 0;
+       depth = INT_MAX;
        totalblocks = 0;
        ftsoptions = FTS_PHYSICAL;
-       while ((ch = getopt(argc, argv, "HLPacksx")) != -1)
+       while ((ch = getopt(argc, argv, "HLPacd:ksx")) != -1)
                switch (ch) {
                case 'H':
                        Hflag = 1;
@@ -112,6 +115,14 @@
                case 'x':
                        ftsoptions |= FTS_XDEV;
                        break;
+               case 'd':
+                               dflag = 1;
+                               depth = atoi(optarg);
+                               if (errno == ERANGE) {
+                                       fprintf(stderr, "Invalid argument to opt
ion d: %s", optarg);
+                                       usage();
+                               }
+                               break;
                case '?':
                default:
                        usage();
@@ -139,12 +150,17 @@
        }
 
        if (aflag) {
-               if (sflag)
+               if (sflag || dflag)
                        usage();
                listdirs = listfiles = 1;
-       } else if (sflag)
+       } else if (sflag) {
+               if (dflag)
+                       usage();
                listdirs = listfiles = 0;
-       else {
+       } else if (dflag) {
+               listfiles = 0;
+               listdirs = 1;
+       } else {
                listfiles = 0;
                listdirs = 1;
        }
@@ -176,7 +192,8 @@
                         * or directories and this is post-order of the
                         * root of a traversal, display the total.
                         */
-                       if (listdirs || (!listfiles && !p->fts_level))
+                       if ((p->fts_level <= depth && listdirs) ||
+                               (!listfiles && !p->fts_level))
                                (void)printf("%ld\t%s\n",
                                    howmany(p->fts_number, blocksize),
                                    p->fts_path);
@@ -248,6 +265,6 @@
 {
 
        (void)fprintf(stderr,
-               "usage: du [-H | -L | -P] [-a | -s] [-ckx] [file ...]\n");
+               "usage: du [-H | -L | -P] [-a | -s | -d depth] [-ckx] [file ...]
\n");
        exit(1);
 }
Only in /usr/src/usr.bin/du: du.cat1

>Audit-Trail:
>Unformatted: