Fwd: -d support for du

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Fwd: -d support for du

David Chisnall-3
Changes made according to Jonathan Gray's comments.

Begin forwarded message:

> From: David Chisnall <[hidden email]>
> Date: 12 August 2006 15:24:09 BDT
> To: Jonathan Gray <[hidden email]>
> Subject: Re: -d support for du
>
>
> On 12 Aug 2006, at 14:15, Jonathan Gray wrote:
>
>> A few points having not actually tried the diff yet.
>>
>> You need to add to the synopsis of du.1 as well as the desc.
>
> Ooops.  I remembered to change the usage message, but not the  
> synopsis.  I wonder if it would be possible to have these generated  
> from the same place so that they always remained in sync...
>
>> Put the d getopt option in lexigraphical order.
>
> Done.
>
>> strtonum(3) is likely a nicer fit than strtol.
>
> Changed.  Also changed the initial default value of maxdepth to  
> SHRT_MAX.   This required the inclusion of sys/limits.h
>
>> I the like the idea behind the diff, keep in mind
>> you can't post attachments to tech however.

Hopefully my client won't mangle the diff (it doesn't wrap lines or  
anything, so I don't know what it did), but if it does then you can  
find a copy at:

http://www.cs.swan.ac.uk/~csdavec/du.diff

Common subdirectories: /usr/src/usr.bin/du/CVS and du/CVS
diff -uNp /usr/src/usr.bin/du/du.1 du/du.1
--- /usr/src/usr.bin/du/du.1 Sun Apr 17 13:27:23 2005
+++ du/du.1 Sat Aug 12 14:28:53 2006
@@ -40,6 +40,7 @@
.Nm du
.Op Fl a | s
.Op Fl chkrx
+.Op Fl d Ar depth
.Op Fl H | L | P
.Op Ar file ...
.Sh DESCRIPTION
@@ -61,6 +62,8 @@ The options are as follows:
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 Ar depth
+Display an entry for all files and directories up to the specified  
depth.
.It Fl H
Symbolic links on the command line are followed.
(Symbolic links encountered in the tree traversal are not followed.)
diff -uNp /usr/src/usr.bin/du/du.c du/du.c
--- /usr/src/usr.bin/du/du.c Wed Jan 25 06:20:03 2006
+++ du/du.c Sat Aug 12 15:21:00 2006
@@ -49,6 +49,7 @@ static const char rcsid[] = "$OpenBSD: d
#include <sys/types.h>
#include <sys/stat.h>
+#include <sys/limits.h>
#include <dirent.h>
#include <err.h>
@@ -75,6 +76,9 @@ main(int argc, char *argv[])
        quad_t totalblocks;
        int ftsoptions, listdirs, listfiles;
        int Hflag, Lflag, aflag, cflag, hflag, kflag, sflag;
+   short maxdepth = SHRT_MAX;
+ extern char *optarg;
+ const char *errstr;
        int ch, notused, rval;
        char **save;
@@ -82,7 +86,7 @@ main(int argc, char *argv[])
        Hflag = Lflag = aflag = cflag = hflag = kflag = sflag = 0;
        totalblocks = 0;
        ftsoptions = FTS_PHYSICAL;
- while ((ch = getopt(argc, argv, "HLPachksxr")) != -1)
+ while ((ch = getopt(argc, argv, "HLPacd:hksxr")) != -1)
                switch (ch) {
                case 'H':
                        Hflag = 1;
@@ -101,6 +105,11 @@ main(int argc, char *argv[])
                case 'c':
                        cflag = 1;
                        break;
+ case 'd':
+ maxdepth = (short)strtonum(optarg, 0, SHRT_MAX, &errstr);
+ if (errstr)
+ errx(1, "specified depth is %s: %s", errstr, optarg);
+ break;
                case 'h':
                        hflag = 1;
                        break;
@@ -183,9 +192,14 @@ 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))
- prtout((quad_t)howmany(p->fts_number, blocksize),
-    p->fts_path, hflag);
+ if (listdirs || (!listfiles && !p->fts_level))
+ /*
+ * If a maximum depth is specified do not display
+ * any directories deeper than the specified depth
+ */
+ if (p->fts_level <= maxdepth)
+ prtout((quad_t)howmany(p->fts_number, blocksize),
+ p->fts_path, hflag);
                        break;
                case FTS_DC: /* Ignore. */
                        break;
@@ -324,6 +338,6 @@ usage(void)
{
        (void)fprintf(stderr,
- "usage: du [-a | -s] [-chkrx] [-H | -L | -P] [file ...]\n");
+ "usage: du [-a | -s] [-chkrx] [-d depth] [-H | -L | -P] [file ...]
\n");
        exit(1);
}
Common subdirectories: /usr/src/usr.bin/du/obj and du/obj

Reply | Threaded
Open this post in threaded view
|

Re: Fwd: -d support for du

Jonathan Gray
A follow up on this, it is not being added as it
is not part of POSIX and this can already be done
by other methods, ie

find PATH -maxdepth N -print0 | xargs -0 du -hs

Adding non standard options results in people
creating nonportable scripts and then everyone
loses.