From owner-freebsd-bugs@FreeBSD.ORG Thu Mar 4 07:30:21 2004 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3FF0A16A4CE for ; Thu, 4 Mar 2004 07:30:21 -0800 (PST) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 37DEC43D31 for ; Thu, 4 Mar 2004 07:30:21 -0800 (PST) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) i24FUKbv098508 for ; Thu, 4 Mar 2004 07:30:20 -0800 (PST) (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.12.10/8.12.10/Submit) id i24FUKFQ098502; Thu, 4 Mar 2004 07:30:20 -0800 (PST) (envelope-from gnats) Date: Thu, 4 Mar 2004 07:30:20 -0800 (PST) Message-Id: <200403041530.i24FUKFQ098502@freefall.freebsd.org> To: freebsd-bugs@FreeBSD.org From: Cyrille Lefevre Subject: Re: bin/19635: add -c for grand total to df(1), like du(1) does X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: Cyrille Lefevre List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Mar 2004 15:30:21 -0000 The following reply was made to PR bin/19635; it has been noted by GNATS. From: Cyrille Lefevre To: freebsd gnats , Will Andrews 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@laposte.net