bin/19635: add -c for grand total to df(1), like du(1) does

Cyrille Lefevre cyrille.lefevre at laposte.net
Thu Mar 4 07:30:21 PST 2004


The following reply was made to PR bin/19635; it has been noted by GNATS.

From: Cyrille Lefevre <cyrille.lefevre at laposte.net>
To: freebsd gnats <freebsd-gnats-submit at freebsd.org>,
	Will Andrews <will at freebsd.org>
Cc:  
Subject: Re: bin/19635: add -c for grand total to df(1), like du(1) does
Date: Thu, 4 Mar 2004 16:22:41 +0100

 cvs diff against -current (FreeBSD 5.2-CURRENT #1: Sat Jan 31 15:17:05 CET 2004)
 I'm using this patch for 2 years right now w/o any problems.
 could someone commit this PR ?
 thanks in advance.
 
 Index: df.1
 ===================================================================
 RCS file: /home/ncvs/src/bin/df/df.1,v
 retrieving revision 1.30
 diff -u -I$Id.*$ -I$.+BSD.*$ -r1.30 df.1
 --- df.1	3 Jun 2003 12:00:35 -0000	1.30
 +++ df.1	21 Jun 2003 23:04:20 -0000
 @@ -44,6 +44,7 @@
  .Fl b | h | H | k |
  .Fl m | P
  .Oc
 +.Op Fl aciln
  .Op Fl ailn
  .Op Fl t Ar type
  .Op Ar file | filesystem ...
 @@ -73,6 +74,8 @@
  this overrides the
  .Ev BLOCKSIZE
  specification from the environment.
 +.It Fl c
 +Display a grand total.
  .It Fl g
  Use 1073741824-byte (1-Gbyte) blocks rather than the default.  Note that
  this overrides the
 Index: df.c
 ===================================================================
 RCS file: /home/ncvs/src/bin/df/df.c,v
 retrieving revision 1.54
 diff -u -I$Id.*$ -I$.+BSD.*$ -r1.54 df.c
 --- df.c	8 Feb 2004 23:42:09 -0000	1.54
 +++ df.c	16 Feb 2004 15:43:28 -0000
 @@ -125,6 +125,7 @@
  static void	  prthuman(const struct statfs *, int64_t);
  static void	  prthumanval(double);
  static void	  prtstat(struct statfs *, struct maxwidths *);
 +static void	  addstat(struct statfs *, struct statfs *);
  static size_t	  regetmntinfo(struct statfs **, long, const char **);
  static unit_t	  unit_adjust(double *);
  static void	  update_maxwidths(struct maxwidths *, const struct statfs *);
 @@ -136,14 +137,14 @@
  	return (a > b ? a : b);
  }
  
 -static int	aflag = 0, hflag, iflag, nflag;
 +static int	aflag = 0, cflag = 0, hflag, iflag, nflag;
  static struct	ufs_args mdev;
  
  int
  main(int argc, char *argv[])
  {
  	struct stat stbuf;
 -	struct statfs statfsbuf, *mntbuf;
 +	struct statfs statfsbuf, *mntbuf, totalbuf;
  	struct maxwidths maxwidths;
  	const char *fstype;
  	char *mntpath, *mntpt;
 @@ -153,12 +154,18 @@
  
  	fstype = "ufs";
  
 +	memset (&totalbuf, 0, sizeof (totalbuf));
 +	totalbuf.f_bsize = DEV_BSIZE;
 +	strncpy (totalbuf.f_mntfromname, "total", MNAMELEN);
  	vfslist = NULL;
 -	while ((ch = getopt(argc, argv, "abgHhiklmnPt:")) != -1)
 +	while ((ch = getopt(argc, argv, "abcgHhiklmnPt:")) != -1)
  		switch (ch) {
  		case 'a':
  			aflag = 1;
  			break;
 +		case 'c':
 +			cflag = 1;
 +			break;
  		case 'b':
  				/* FALLTHROUGH */
  		case 'P':
 @@ -218,12 +225,18 @@
  	if (!*argv) {
  		mntsize = regetmntinfo(&mntbuf, mntsize, vfslist);
  		bzero(&maxwidths, sizeof(maxwidths));
 -		for (i = 0; i < mntsize; i++)
 -			update_maxwidths(&maxwidths, &mntbuf[i]);
  		for (i = 0; i < mntsize; i++) {
 +			if (cflag)
 +				addstat(&totalbuf, &mntbuf[i]);
 +			update_maxwidths(&maxwidths, &mntbuf[i]);
 +		}
 +		if (cflag)
 +			update_maxwidths(&maxwidths, &totalbuf);
 +		for (i = 0; i < mntsize; i++)
  			if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
  				prtstat(&mntbuf[i], &maxwidths);
 -		}
 +		if (cflag)
 +			prtstat(&totalbuf, &maxwidths);
  		exit(rv);
  	}
  
 @@ -260,6 +273,8 @@
  				} else if (statfs(mntpt, &statfsbuf) == 0) {
  					statfsbuf.f_mntonname[0] = '\0';
  					prtstat(&statfsbuf, &maxwidths);
 +					if (cflag)
 +						addstat(&totalbuf, &statfsbuf);
  				} else {
  					warn("%s", *argv);
  					rv = 1;
 @@ -298,7 +313,11 @@
  			update_maxwidths(&maxwidths, &statfsbuf);
  		}
  		prtstat(&statfsbuf, &maxwidths);
 +		if (cflag)
 +			addstat(&totalbuf, &statfsbuf);
  	}
 +	if (cflag)
 +		prtstat(&totalbuf, &maxwidths);
  	return (rv);
  }
  
 @@ -387,11 +406,11 @@
  	unit = unit_adjust(&bytes);
  
  	if (bytes == 0)
 -		(void)printf("     0B");
 +		(void)printf("      0B");
  	else if (bytes > 10)
 -		(void)printf(" %5.0f%c", bytes, "BKMGTPE"[unit]);
 +		(void)printf(" % 6.0f%c", bytes, "BKMGTPE"[unit]);
  	else
 -		(void)printf(" %5.1f%c", bytes, "BKMGTPE"[unit]);
 +		(void)printf(" % 6.1f%c", bytes, "BKMGTPE"[unit]);
  }
  
  /*
 @@ -412,6 +431,7 @@
  	static int headerlen, timesthrough = 0;
  	static const char *header;
  	int64_t used, availblks, inodes;
 +	int total;
  
  	if (++timesthrough == 1) {
  		mwp->mntfrom = imax(mwp->mntfrom, (int)strlen("Filesystem"));
 @@ -453,15 +473,31 @@
  	}
  	(void)printf(" %5.0f%%",
  	    availblks == 0 ? 100.0 : (double)used / (double)availblks * 100.0);
 +	total = !*sfsp->f_mntonname &&
 +	    !strncmp(sfsp->f_mntfromname, "total", MNAMELEN);
  	if (iflag) {
  		inodes = sfsp->f_files;
  		used = inodes - sfsp->f_ffree;
 -		(void)printf(" %*jd %*jd %4.0f%% ", mwp->iused, (intmax_t)used,
 -		    mwp->ifree, (intmax_t)sfsp->f_ffree, inodes == 0 ? 100.0 :
 -		    (double)used / (double)inodes * 100.0);
 -	} else
 -		(void)printf("  ");
 -	(void)printf("  %s\n", sfsp->f_mntonname);
 +		(void)printf(" %*jd %*jd %4.0f%% ",
 +		    mwp->iused, (intmax_t)used,
 +		    mwp->ifree, (intmax_t)sfsp->f_ffree,
 +		    inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
 +	} else if (!total)
 +		(void)printf("  %s", sfsp->f_mntonname);
 +	(void)printf("\n");
 +}
 +
 +void
 +addstat(totalfsp, statfsp)
 +	struct statfs *totalfsp, *statfsp;
 +{
 +	double bsize = statfsp->f_bsize / totalfsp->f_bsize;
 +
 +	totalfsp->f_blocks += statfsp->f_blocks * bsize;
 +	totalfsp->f_bfree += statfsp->f_bfree * bsize;
 +	totalfsp->f_bavail += statfsp->f_bavail * bsize;
 +	totalfsp->f_files += statfsp->f_files;
 +	totalfsp->f_ffree += statfsp->f_ffree;
  }
  
  /*
 @@ -515,7 +551,7 @@
  {
  
  	(void)fprintf(stderr,
 -	    "usage: df [-b | -H | -h | -k | -m | -P] [-ailn] [-t type] [file | filesystem ...]\n");
 +	    "usage: df [-b | -H | -h | -k | -m | -P] [-aciln] [-t type] [file | filesystem ...]\n");
  	exit(EX_USAGE);
  }
  
 Cyrille Lefevre
 -- 
 mailto:cyrille.lefevre at laposte.net


More information about the freebsd-bugs mailing list