Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 17 Nov 2000 16:30:02 -0800 (PST)
From:      clefevre@citeweb.net
To:        freebsd-bugs@FreeBSD.org
Subject:   Re: bin/19635: add -c for grand total to df(1), like du(1) does
Message-ID:  <200011180030.QAA62980@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/19635; it has been noted by GNATS.

From: clefevre@citeweb.net
To: freebsd-gnats-submit@FreeBSD.org
Cc: will@freebsd.org
Subject: Re: bin/19635: add -c for grand total to df(1), like du(1) does
Date: Wed, 1 Nov 2000 03:13:14 +0100 (CET)

 Hi Will,
 
 I've found a -current machine where I could reproduce "the bug".
 in fact, I didn't have this bug, because I compile w/ -funroll-loops !
 so, I fix the addstat() function to bypass the long overflow using an
 intermediate variable as double in case of DEV_BSIZE > 512 (who knows :).
 
 here is a complete patch to df(1) w/ -c for grand total which seems to
 work whatever the system version and compiler options.
 
 ----------==========---------- cut here ----------==========----------
 Index: df.1
 ===================================================================
 RCS file: /home/ncvs/src/bin/df/df.1,v
 retrieving revision 1.18.2.2
 diff -u -r1.18.2.2 df.1
 --- df.1	2000/07/01 03:02:08	1.18.2.2
 +++ df.1	2000/07/02 05:38:45
 @@ -44,7 +44,7 @@
  .Fl b | h | H | k |
  .Fl m | P
  .Oc
 -.Op Fl ain
 +.Op Fl acin
  .Op Fl t Ar type
  .Op Ar file | Ar filesystem ...
  .Sh DESCRIPTION
 @@ -71,6 +71,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.23.2.1
 diff -u -r1.23.2.1 df.c
 --- df.c	2000/06/13 03:19:40	1.23.2.1
 +++ df.c	2000/11/01 01:46:27
 @@ -103,11 +103,12 @@
  void	  prthuman __P((struct statfs *, long));
  void	  prthumanval __P((double));
  void	  prtstat __P((struct statfs *, int));
 +void	  addstat __P((struct statfs *, struct statfs *));
  int	  ufs_df __P((char *, int));
  unit_t	  unit_adjust __P((double *));
  void	  usage __P((void));
  
 -int	aflag = 0, hflag, iflag, nflag;
 +int	aflag = 0, cflag = 0, hflag, iflag, nflag;
  struct	ufs_args mdev;
  
  int
 @@ -116,17 +117,22 @@
  	char *argv[];
  {
  	struct stat stbuf;
 -	struct statfs statfsbuf, *mntbuf;
 +	struct statfs statfsbuf, *mntbuf, totalbuf = { 0 };
  	long mntsize;
  	int ch, err, i, maxwidth, rv, width;
  	char *mntpt, *mntpath, **vfslist;
  
 +	totalbuf.f_bsize = DEV_BSIZE;
 +	strncpy (totalbuf.f_mntfromname, "total", MNAMELEN);
  	vfslist = NULL;
 -	while ((ch = getopt(argc, argv, "abgHhikmnPt:")) != -1)
 +	while ((ch = getopt(argc, argv, "abcgHhikmnPt:")) != -1)
  		switch (ch) {
  		case 'a':
  			aflag = 1;
  			break;
 +		case 'c':
 +			cflag = 1;
 +			break;
  		case 'b':
  				/* FALLTHROUGH */
  		case 'P':
 @@ -191,9 +197,14 @@
  			}
  		}
  		for (i = 0; i < mntsize; i++) {
 -			if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0)
 +			if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) {
  				prtstat(&mntbuf[i], maxwidth);
 +				if (cflag)
 +					addstat(&totalbuf, &mntbuf[i]);
 +			}
  		}
 +		if (cflag)
 +			prtstat(&totalbuf, maxwidth);
  		exit(rv);
  	}
  
 @@ -256,7 +267,11 @@
  		if (argc == 1)
  			maxwidth = strlen(statfsbuf.f_mntfromname) + 1;
  		prtstat(&statfsbuf, maxwidth);
 +		if (cflag)
 +			addstat(&totalbuf, &statfsbuf);
  	}
 +	if (cflag)
 +		prtstat(&totalbuf, maxwidth);
  	return (rv);
  }
  
 @@ -380,6 +395,7 @@
  	static int headerlen, timesthrough;
  	static char *header;
  	long used, availblks, inodes;
 +	int total;
  
  	if (maxwidth < 11)
  		maxwidth = 11;
 @@ -411,14 +427,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(" %7ld %7ld %5.0f%% ", used, sfsp->f_ffree,
  		   inodes == 0 ? 100.0 : (double)used / (double)inodes * 100.0);
 -	} else
 +	} else if (!total)
  		(void)printf("  ");
 -	(void)printf("  %s\n", sfsp->f_mntonname);
 +	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;
  }
  
  /*
 @@ -506,6 +539,6 @@
  {
  
  	(void)fprintf(stderr,
 -	    "usage: df [-b | -H | -h | -k | -m | -P] [-ain] [-t type] [file | filesystem ...]\n");
 +	    "usage: df [-b | -H | -h | -k | -m | -P] [-acin] [-t type] [file | filesystem ...]\n");
  	exit(EX_USAGE);
  }
 ----------==========---------- cut here ----------==========----------
 
 Cyrille.
 --
 home: mailto:clefevre@citeweb.net work: mailto:Cyrille.Lefevre@edf.fr
 


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




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