Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 10 Mar 1997 22:09:39 -0500 (EST)
From:      Tim.Vanderhoek@X2296
To:        FreeBSD-gnats-submit@freebsd.org
Subject:   bin/2938: GNU du(1) is better than FreeBSD du(1)
Message-ID:  <199703110309.WAA01275@X2296>
Resent-Message-ID: <199703110320.TAA21675@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>Number:         2938
>Category:       bin
>Synopsis:       Add -b, -l, and -f options to du(1)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Mar 10 19:20:02 PST 1997
>Last-Modified:
>Originator:     Tim Vanderhoek
>Organization:
tIM...HOEk
>Release:        FreeBSD 2.2-961006-SNAP i386
>Environment:

Any FreeBSD version upto, but not including, the one where 
this fix is applied.

>Description:

GNU du(1) has a nice option (-b) to report values in bytes, instead of 
in blocks.  This can sometimes be useful.

GNU du(1) also has an option (-l) to count hardlinked once for every
link (instead of only once).  This can be useful, for example, if
planning to copy files to a media which does not support hardlinks
(such as a FAT fs), or in any situation where hardlinked files are
treated as separate files.

Finally, both GNU du(1) and du(1) are missing a third option (-f)
which prevents the space used by directory entries from being
counted.  As with the -b option, this can be useful to gain a better
idea of the true SIZE of the files (as opposed to the disk used by them
(and Yes, I know what "du" stands for :)).

Since we're on the subject, GNU du(1) also has a number of other
options which could be easily added, but they don't strike me as
being particularly worthwhile.  They are -c (similar to -s), 
-h (use silly G, M, etc. abbreviations), -m (BLOCKSIZE=2^20),
-S (similar or equivalent to no recurssing).


>How-To-Repeat:

Reread the description.

>Fix:
	
Okay, this fix adds all three options to du.  Updates the manpage.
Fixes the synopsis in the manpage to include the -k added in Rev 1.2
of the manpage by wollman.  Clarifies the description of the -d
option added in Rev 1.6 of the manpage by scrappy (submitted by
John-Mark Gurney <jmg@nike.efn.org>).  Fixes a (char *) cast to be
(void *), since realloc() expects a (void *) and if we're going to
cast it, we might as well do it rightly...  (Or does this violate
the so-called decision to pretend to support K&R compilers?).

If you so desire, I can even resubmit the patches without any one
or more or the above fixes.


*** du.c.orig	Mon Mar 10 21:17:40 1997
--- du.c	Mon Mar 10 21:03:03 1997
***************
*** 68,80 ****
  	long blocksize;
  	int ftsoptions, listdirs, listfiles, depth;
  	int Hflag, Lflag, Pflag, aflag, ch, notused, rval, sflag, dflag;
  	char **save;
  
  	save = argv;
! 	Hflag = Lflag = Pflag = aflag = sflag = dflag = 0;
  	depth = INT_MAX;
  	ftsoptions = FTS_PHYSICAL;
! 	while ((ch = getopt(argc, argv, "HLPad:ksx")) != EOF)
  		switch (ch) {
  		case 'H':
  			Hflag = 1;
--- 68,82 ----
  	long blocksize;
  	int ftsoptions, listdirs, listfiles, depth;
  	int Hflag, Lflag, Pflag, aflag, ch, notused, rval, sflag, dflag;
+ 	int bflag, kflag, lflag, fflag;
  	char **save;
  
  	save = argv;
! 	Hflag = Lflag = Pflag = aflag = bflag = sflag = dflag = kflag = 0;
! 	lflag = fflag = 0;
  	depth = INT_MAX;
  	ftsoptions = FTS_PHYSICAL;
! 	while ((ch = getopt(argc, argv, "HLPabd:ksxlf")) != EOF)
  		switch (ch) {
  		case 'H':
  			Hflag = 1;
***************
*** 88,97 ****
--- 90,110 ----
  			Pflag = 1;
  			Hflag = Lflag = 0;
  			break;
+ 		case 'l':
+ 			lflag = 1;
+ 			break;
  		case 'a':
  			aflag = 1;
  			break;
+ 		case 'b':
+ 			bflag = 1;
+ 			break;
+ 		case 'f':
+ 			fflag = 1;
+ 			break;
  		case 'k':
+ 			/* We flag -k only to check conflicts with -b */
+ 			kflag = 1;
  			putenv("BLOCKSIZE=1024");
  			break;
  		case 's':
***************
*** 159,164 ****
--- 172,183 ----
  	(void)getbsize(&notused, &blocksize);
  	blocksize /= 512;
  
+ 	if (bflag) {
+ 		if (kflag)
+ 			usage();
+ 		blocksize = 1;
+ 	}
+ 			
  	if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
  		err(1, NULL);
  
***************
*** 168,174 ****
  			break;
  		case FTS_DP:
  			p->fts_parent->fts_number +=
! 			    p->fts_number += p->fts_statp->st_blocks;
  			/*
  			 * If listing each directory, or not listing files
  			 * or directories and this is post-order of the
--- 187,196 ----
  			break;
  		case FTS_DP:
  			p->fts_parent->fts_number +=
! 			    p->fts_number +=
! 			    (fflag ? 0 :
! 			    (bflag ? p->fts_statp->st_size :
! 			             p->fts_statp->st_blocks));
  			/*
  			 * If listing each directory, or not listing files
  			 * or directories and this is post-order of the
***************
*** 189,205 ****
  			rval = 1;
  			break;
  		default:
! 			if (p->fts_statp->st_nlink > 1 && linkchk(p))
  				break;
  			/*
  			 * If listing each file, or a non-directory file was
  			 * the root of a traversal, display the total.
  			 */
  			if (listfiles || !p->fts_level)
! 				(void)printf("%qd\t%s\n",
! 				    howmany(p->fts_statp->st_blocks, blocksize),
! 				    p->fts_path);
! 			p->fts_parent->fts_number += p->fts_statp->st_blocks;
  		}
  	if (errno)
  		err(1, "fts_read");
--- 211,235 ----
  			rval = 1;
  			break;
  		default:
! 			if (!lflag && p->fts_statp->st_nlink > 1 && linkchk(p))
  				break;
  			/*
  			 * If listing each file, or a non-directory file was
  			 * the root of a traversal, display the total.
  			 */
  			if (listfiles || !p->fts_level)
! 				if (bflag)
! 					(void)printf("%qd\t%s\n",
! 					    (long long) p->fts_statp->st_size,
! 					    p->fts_path);
! 				else
! 					(void)printf("%qd\t%s\n",
! 					    howmany(p->fts_statp->st_blocks,
! 					            blocksize),
! 					    p->fts_path);
! 			p->fts_parent->fts_number +=
! 			    (bflag ? p->fts_statp->st_size :
! 			             p->fts_statp->st_blocks);
  		}
  	if (errno)
  		err(1, "fts_read");
***************
*** 228,234 ****
  			if (ino == fp->inode && dev == fp->dev)
  				return (1);
  
! 	if (nfiles == maxfiles && (files = realloc((char *)files,
  	    (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
  		err(1, "");
  	files[nfiles].inode = ino;
--- 258,264 ----
  			if (ino == fp->inode && dev == fp->dev)
  				return (1);
  
! 	if (nfiles == maxfiles && (files = realloc((void *)files,
  	    (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
  		err(1, "");
  	files[nfiles].inode = ino;
***************
*** 242,247 ****
  {
  
  	(void)fprintf(stderr,
! 		"usage: du [-H | -L | -P] [-a | -s | -d depth] [-k] [-x] [file ...]\n");
  	exit(1);
  }
--- 272,277 ----
  {
  
  	(void)fprintf(stderr,
! 		"usage: du [-H | -L | -P] [-a | -s | -d depth] [-k | -b] [-x] [-l] [-f] [file ...]\n");
  	exit(1);
  }
*** du.1.orig	Mon Mar 10 21:17:33 1997
--- du.1	Mon Mar 10 21:07:48 1997
***************
*** 42,48 ****
--- 42,51 ----
  .Nm du
  .Op Fl H | Fl L | Fl P
  .Op Fl a | s | d Ar depth
+ .Op Fl k | b
  .Op Fl x
+ .Op Fl l
+ .Op Fl f
  .Op Ar file ...
  .Sh DESCRIPTION
  The
***************
*** 66,86 ****
  All symbolic links are followed.
  .It Fl P
  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
  Report in 1024-byte (1-Kbyte) blocks rather than the default.  Note that
  this overrides the
  .Ev BLOCKSIZE
  setting from the environment.
  .It Fl s
  Display only the grand total for the specified files.
  .It Fl x
  Filesystem mount points are not traversed.
  .El
  .Pp
  .Nm Du
--- 69,103 ----
  All symbolic links are followed.
  .It Fl P
  No symbolic links are followed.
  .It Fl k
  Report in 1024-byte (1-Kbyte) blocks rather than the default.  Note that
  this overrides the
  .Ev BLOCKSIZE
  setting from the environment.
+ .It Fl b
+ Report the number of bytes, rather than the number of blocks.  Since
+ even 1-byte files occupy one block, the number reported by this option 
+ will not equal the number of blocks * the blocksize (except in the case
+ where every file is an even multiple of the blocksize).  This
+ option will silently fail for sizes over two gigabytes.
+ .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.  This has no effect on the depth to which directories
+ are counted; it merely signifies that directories beyond
+ .Ar depth
+ are not to be displayed.
  .It Fl s
  Display only the grand total for the specified files.
  .It Fl x
  Filesystem mount points are not traversed.
+ .It Fl l
+ Files are not checked to see if they are hardlinked.
+ .It Fl f
+ Count only files, not the directory structure that contains them.
+ That is, do not count the space used by each directory entry.
  .El
  .Pp
  .Nm Du
***************
*** 107,113 ****
  Files having multiple hard links are counted (and displayed) a single
  time per
  .Nm du
! execution.
  .Sh ENVIRONMENTAL VARIABLES
  .Bl -tag -width BLOCKSIZE
  .It Ev BLOCKSIZE
--- 124,132 ----
  Files having multiple hard links are counted (and displayed) a single
  time per
  .Nm du
! execution unless the 
! .Fl l
! option is specified.
  .Sh ENVIRONMENTAL VARIABLES
  .Bl -tag -width BLOCKSIZE
  .It Ev BLOCKSIZE
>Audit-Trail:
>Unformatted:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199703110309.WAA01275>