Subject: bin/2805: add '-c' (show grand total) to du
To: None <gnats-bugs@gnats.netbsd.org>
From: None <lukem@telstra.com.au>
List: netbsd-bugs
Date: 10/04/1996 18:45:24
>Number:         2805
>Category:       bin
>Synopsis:       du needs a '-c' (grand total) option
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          change-request
>Submitter-Id:   lm
>Arrival-Date:   Fri Oct  4 01:50:01 1996
>Last-Modified:
>Originator:     Luke Mewburn
>Organization:
	werj
>Release:        1.2
>Environment:
System: NetBSD melanoma 1.1 NetBSD 1.1 (MELANOMA) #1: Thu Dec 7 12:45:12 EST 1995 root@melanoma:/usr/src/sys/arch/i386/compile/MELANOMA i386

>Description:
	GNU du supports '-c', which shows a grand total at the end. This is
	very useful when you do 'gdu -c -s *', amongst other things.
	This patch gives netbsd the same functionality.

>How-To-Repeat:
	run du. notice lack of grand total option.

>Fix:
	apply this simple yet effective patch.

diff -c /ftp/pub/NetBSD/NetBSD-current/src/usr.bin/du/du.1 ./du.1
*** /ftp/pub/NetBSD/NetBSD-current/src/usr.bin/du/du.1	Fri Jan  5 23:27:54 1996
--- ./du.1	Fri Oct  4 18:32:35 1996
***************
*** 33,39 ****
  .\"
  .\"	@(#)du.1	8.2 (Berkeley) 4/1/94
  .\"
! .Dd April 1, 1994
  .Dt DU 1
  .Os
  .Sh NAME
--- 33,39 ----
  .\"
  .\"	@(#)du.1	8.2 (Berkeley) 4/1/94
  .\"
! .Dd October 4, 1996
  .Dt DU 1
  .Os
  .Sh NAME
***************
*** 43,49 ****
  .Nm du
  .Op Fl H | Fl L | Fl P
  .Op Fl a | Fl s
! .Op Fl kx
  .Op Ar file ...
  .Sh DESCRIPTION
  The
--- 43,49 ----
  .Nm du
  .Op Fl H | Fl L | Fl P
  .Op Fl a | Fl s
! .Op Fl ckx
  .Op Ar file ...
  .Sh DESCRIPTION
  The
***************
*** 76,81 ****
--- 76,83 ----
  flag is specified, the number displayed is the number of 1024-byte
  blocks.
  Partial numbers of blocks are rounded up.
+ .It Fl c
+ Display the grand total after all the arguments have been processed.
  .It Fl s
  Display only the grand total for the specified files.
  .It Fl x
diff -c /ftp/pub/NetBSD/NetBSD-current/src/usr.bin/du/du.c ./du.c
*** /ftp/pub/NetBSD/NetBSD-current/src/usr.bin/du/du.c	Sat Oct 14 13:05:22 1995
--- ./du.c	Fri Oct  4 18:37:48 1996
***************
*** 72,86 ****
  {
  	FTS *fts;
  	FTSENT *p;
! 	long blocksize;
  	int ftsoptions, listdirs, listfiles;
! 	int Hflag, Lflag, Pflag, aflag, ch, kflag, notused, rval, sflag;
  	char **save;
  
  	save = argv;
! 	Hflag = Lflag = Pflag = aflag = kflag = sflag = 0;
  	ftsoptions = FTS_PHYSICAL;
! 	while ((ch = getopt(argc, argv, "HLPaksx")) != EOF)
  		switch (ch) {
  		case 'H':
  			Hflag = 1;
--- 72,87 ----
  {
  	FTS *fts;
  	FTSENT *p;
! 	long blocksize, totalblocks;
  	int ftsoptions, listdirs, listfiles;
! 	int Hflag, Lflag, Pflag, aflag, ch, cflag, kflag, notused, rval, sflag;
  	char **save;
  
  	save = argv;
! 	Hflag = Lflag = Pflag = aflag = cflag = kflag = sflag = 0;
! 	totalblocks = 0;
  	ftsoptions = FTS_PHYSICAL;
! 	while ((ch = getopt(argc, argv, "HLPacksx")) != EOF)
  		switch (ch) {
  		case 'H':
  			Hflag = 1;
***************
*** 97,102 ****
--- 98,106 ----
  		case 'a':
  			aflag = 1;
  			break;
+ 		case 'c':
+ 			cflag = 1;
+ 			break;
  		case 'k':
  			blocksize = 1024;
  			kflag = 1;
***************
*** 164,169 ****
--- 168,175 ----
  		case FTS_DP:
  			p->fts_parent->fts_number += 
  			    p->fts_number += p->fts_statp->st_blocks;
+ 			if (cflag)
+ 				totalblocks += p->fts_statp->st_blocks;
  			/*
  			 * If listing each directory, or not listing files
  			 * or directories and this is post-order of the
***************
*** 194,202 ****
--- 200,213 ----
  				    howmany(p->fts_statp->st_blocks, blocksize),
  				    p->fts_path);
  			p->fts_parent->fts_number += p->fts_statp->st_blocks;
+ 			if (cflag)
+ 				totalblocks += p->fts_statp->st_blocks;
  		}
  	if (errno)
  		err(1, "fts_read");
+ 	if (cflag)
+ 		(void)printf("%ld\ttotal\n",
+ 		    howmany(totalblocks, blocksize));
  	exit(0);
  }
  
***************
*** 236,241 ****
  {
  
  	(void)fprintf(stderr,
! 		"usage: du [-H | -L | -P] [-a | -s] [-kx] [file ...]\n");
  	exit(1);
  }
--- 247,252 ----
  {
  
  	(void)fprintf(stderr,
! 		"usage: du [-H | -L | -P] [-a | -s] [-ckx] [file ...]\n");
  	exit(1);
  }
>Audit-Trail:
>Unformatted: