-d support for du

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

-d support for du

David Chisnall-3
Hi,

The enclosed modifications allow du to support the -d option to  
specify a maximum depth, and includes a corresponding change to the  
documentation.

The -d option is present in FreeBSD's du.  GNU du has similar  
functionality provided by --max-depth.


David

--- /usr/src/usr.bin/du/du.1    Sun Apr 17 13:27:23 2005
+++ du/du.1     Fri Aug 11 14:27:22 2006
@@ -61,6 +61,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     Fri Aug 11 14:53:05 2006
@@ -75,6 +75,8 @@ main(int argc, char *argv[])
         quad_t totalblocks;
         int ftsoptions, listdirs, listfiles;
         int Hflag, Lflag, aflag, cflag, hflag, kflag, sflag;
+       unsigned int maxdepth = -1;
+       extern char *optarg;
         int ch, notused, rval;
         char **save;
@@ -82,7 +84,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, "HLPachksxrd:")) != -1)
                 switch (ch) {
                 case 'H':
                         Hflag = 1;
@@ -115,6 +117,9 @@ main(int argc, char *argv[])
                 case 'x':
                         ftsoptions |= FTS_XDEV;
                         break;
+               case 'd':
+                       maxdepth = (int)strtol(optarg, (char**)NULL,  
10);
+                       break;
                 case '?':
                 default:
                         usage();
@@ -183,9 +188,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 +334,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);
}

Reply | Threaded
Open this post in threaded view
|

Re: -d support for du

Han Boetes
David Chisnall wrote:
> -                       if (listdirs || (!listfiles && !p-> fts_level))
> -                               prtout((quad_t)howmany(p-> fts_number,  
> blocksize),

Your patch is wrapped, please resend it with a decent mta



# Han

Reply | Threaded
Open this post in threaded view
|

Re: -d support for du

Ray Lai
In reply to this post by David Chisnall-3
On Sat, Aug 12, 2006 at 12:12:25PM +0100, David Chisnall wrote:

> Hi,
>
> The enclosed modifications allow du to support the -d option to  
> specify a maximum depth, and includes a corresponding change to the  
> documentation.
>
> The -d option is present in FreeBSD's du.  GNU du has similar  
> functionality provided by --max-depth.
>
>
> David
>
> --- /usr/src/usr.bin/du/du.1    Sun Apr 17 13:27:23 2005
> +++ du/du.1     Fri Aug 11 14:27:22 2006
> @@ -61,6 +61,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     Fri Aug 11 14:53:05 2006
> @@ -75,6 +75,8 @@ main(int argc, char *argv[])
>         quad_t totalblocks;
>         int ftsoptions, listdirs, listfiles;
>         int Hflag, Lflag, aflag, cflag, hflag, kflag, sflag;
> +       unsigned int maxdepth = -1;

Use UINT_MAX.  Also, since fts_level is a short, matching types should
be better (which means you should use SHRT_MAX).

> +       extern char *optarg;
>         int ch, notused, rval;
>         char **save;
> @@ -82,7 +84,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, "HLPachksxrd:")) != -1)
>                 switch (ch) {
>                 case 'H':
>                         Hflag = 1;
> @@ -115,6 +117,9 @@ main(int argc, char *argv[])
>                 case 'x':
>                         ftsoptions |= FTS_XDEV;
>                         break;
> +               case 'd':
> +                       maxdepth = (int)strtol(optarg, (char**)NULL,  

Use strtonum(3).

> 10);
> +                       break;
>                 case '?':
>                 default:
>                         usage();
> @@ -183,9 +188,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 +334,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);
> }