From owner-svn-src-all@FreeBSD.ORG Tue Oct 18 08:18:26 2011 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 5E3AD106564A; Tue, 18 Oct 2011 08:18:26 +0000 (UTC) (envelope-from des@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 4D8A38FC08; Tue, 18 Oct 2011 08:18:26 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id p9I8IQH8063445; Tue, 18 Oct 2011 08:18:26 GMT (envelope-from des@svn.freebsd.org) Received: (from des@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id p9I8IQlb063443; Tue, 18 Oct 2011 08:18:26 GMT (envelope-from des@svn.freebsd.org) Message-Id: <201110180818.p9I8IQlb063443@svn.freebsd.org> From: Dag-Erling Smorgrav Date: Tue, 18 Oct 2011 08:18:26 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r226502 - head/bin/df X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Oct 2011 08:18:26 -0000 Author: des Date: Tue Oct 18 08:18:26 2011 New Revision: 226502 URL: http://svn.freebsd.org/changeset/base/226502 Log: Simplify df(1) by factoring out most of the common code: - In the argc == 0 case, just populate the mount list as before, but do not calculate widths, update totals or print anything. - In the argv > 0 case, collect information about the requested file systems and store it in the mount list, but do not calculate widths, update totals or print anything. - In either case, once all the information has been collected, iterate once through the mount list to calculate widths and totals, then once more to print everything. This also fixes two bugs: firstly, column widths were not calculated correctly if more than one file system was specified on the command line; and secondly, file systems with MNT_IGNORE were included in the totals even if -a was not specified. Noticed by: Paul Schenkeveld MFC after: 3 weeks Modified: head/bin/df/df.c Modified: head/bin/df/df.c ============================================================================== --- head/bin/df/df.c Tue Oct 18 08:10:23 2011 (r226501) +++ head/bin/df/df.c Tue Oct 18 08:18:26 2011 (r226502) @@ -107,7 +107,7 @@ main(int argc, char *argv[]) const char *fstype; char *mntpath, *mntpt; const char **vfslist; - size_t i, mntsize; + int i, mntsize; int ch, rv; fstype = "ufs"; @@ -187,30 +187,21 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); - bzero(&maxwidths, sizeof(maxwidths)); - for (i = 0; i < mntsize; i++) - update_maxwidths(&maxwidths, &mntbuf[i]); - rv = 0; if (!*argv) { + /* everything (modulo -t) */ + mntsize = getmntinfo(&mntbuf, MNT_NOWAIT); mntsize = regetmntinfo(&mntbuf, mntsize, vfslist); - bzero(&maxwidths, sizeof(maxwidths)); - 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); + } else { + /* just the filesystems specified on the command line */ + mntbuf = malloc(argc * sizeof(*mntbuf)); + if (mntbuf == 0) + err(1, "malloc()"); + mntsize = 0; + /* continued in for loop below */ } + /* iterate through specified filesystems */ for (; *argv; argv++) { if (stat(*argv, &stbuf) < 0) { if ((mntpt = getmntpt(*argv)) == 0) { @@ -279,14 +270,24 @@ main(int argc, char *argv[]) continue; } - if (argc == 1) { - bzero(&maxwidths, sizeof(maxwidths)); - update_maxwidths(&maxwidths, &statfsbuf); + /* the user asked for it, so ignore the ignore flag */ + statfsbuf.f_flags &= ~MNT_IGNORE; + + /* add to list */ + mntbuf[mntsize++] = statfsbuf; + } + + bzero(&maxwidths, sizeof(maxwidths)); + for (i = 0; i < mntsize; i++) { + if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) { + update_maxwidths(&maxwidths, &mntbuf[i]); + if (cflag) + addstat(&totalbuf, &mntbuf[i]); } - prtstat(&statfsbuf, &maxwidths); - if (cflag) - addstat(&totalbuf, &statfsbuf); } + for (i = 0; i < mntsize; i++) + if (aflag || (mntbuf[i].f_flags & MNT_IGNORE) == 0) + prtstat(&mntbuf[i], &maxwidths); if (cflag) prtstat(&totalbuf, &maxwidths); return (rv);