From 6b31b9d5d2b513523939b659e7009e543cc243a6 Mon Sep 17 00:00:00 2001 From: bapt Date: Thu, 9 Feb 2012 09:20:34 +0000 Subject: [PATCH] MFH r226502,r226503: factor out the common code and fix two display bugs Approved by: des (mentor) git-svn-id: svn://svn.freebsd.org/base/stable/9@231260 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- bin/df/df.1 | 3 ++- bin/df/df.c | 53 +++++++++++++++++++++++++++-------------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/bin/df/df.1 b/bin/df/df.1 index 8ed6fc6d1..a08aef6a1 100644 --- a/bin/df/df.1 +++ b/bin/df/df.1 @@ -29,7 +29,7 @@ .\" @(#)df.1 8.3 (Berkeley) 5/8/95 .\" $FreeBSD$ .\" -.Dd November 23, 2008 +.Dd October 18, 2011 .Dt DF 1 .Os .Sh NAME @@ -63,6 +63,7 @@ The following options are available: Show all mount points, including those that were mounted with the .Dv MNT_IGNORE flag. +This is implied for file systems specified on the command line. .It Fl b Use 512-byte blocks rather than the default. Note that diff --git a/bin/df/df.c b/bin/df/df.c index 73f6acd75..f865b8f1e 100644 --- a/bin/df/df.c +++ b/bin/df/df.c @@ -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); -- 2.45.0