summaryrefslogtreecommitdiffstats
path: root/bin/df
diff options
context:
space:
mode:
authordes <des@FreeBSD.org>2011-10-18 08:18:26 +0000
committerdes <des@FreeBSD.org>2011-10-18 08:18:26 +0000
commit3d1c49fd59d9d45142c32375a2a86432c2dfe95e (patch)
treedaf2277f0b90bba96c77b7d0e82896b2c6cc9e38 /bin/df
parentf4b874b34dd76a541a045f8a723c6d38c18aa2ef (diff)
downloadFreeBSD-src-3d1c49fd59d9d45142c32375a2a86432c2dfe95e.zip
FreeBSD-src-3d1c49fd59d9d45142c32375a2a86432c2dfe95e.tar.gz
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
Diffstat (limited to 'bin/df')
-rw-r--r--bin/df/df.c53
1 files changed, 27 insertions, 26 deletions
diff --git a/bin/df/df.c b/bin/df/df.c
index 73f6acd..f865b8f1 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);
OpenPOWER on IntegriCloud