Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/df df: add grand total option



details:   https://anonhg.NetBSD.org/src/rev/29204a6b0730
branches:  trunk
changeset: 1017647:29204a6b0730
user:      ginsbach <ginsbach%NetBSD.org@localhost>
date:      Sun Jan 03 01:43:12 2021 +0000

description:
df: add grand total option

Add a grand total option, -c, similar to the du(1) -c option. Adapted from
the same option (-c) in FreeBSD df(1).

diffstat:

 bin/df/df.1 |   6 ++++--
 bin/df/df.c |  43 ++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 42 insertions(+), 7 deletions(-)

diffs (139 lines):

diff -r ae428ed50fad -r 29204a6b0730 bin/df/df.1
--- a/bin/df/df.1       Sat Jan 02 22:02:27 2021 +0000
+++ b/bin/df/df.1       Sun Jan 03 01:43:12 2021 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: df.1,v 1.54 2019/09/23 15:24:44 christos Exp $
+.\"    $NetBSD: df.1,v 1.55 2021/01/03 01:43:12 ginsbach Exp $
 .\"
 .\" Copyright (c) 1989, 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -37,7 +37,7 @@
 .Nd display free disk space
 .Sh SYNOPSIS
 .Nm
-.Op Fl aglnW
+.Op Fl acglnW
 .Op Fl Ghkm | Fl ihkm | Fl Pk
 .Op Fl t Ar type
 .Op Ar file | Ar file_system ...
@@ -72,6 +72,8 @@
 including those that were mounted with the
 .Dv MNT_IGNORE
 flag.
+.It Fl c
+Display a grand total for all shown mount points.
 .It Fl G
 Display all the fields of the structure(s) returned by
 .Xr statvfs 2 .
diff -r ae428ed50fad -r 29204a6b0730 bin/df/df.c
--- a/bin/df/df.c       Sat Jan 02 22:02:27 2021 +0000
+++ b/bin/df/df.c       Sun Jan 03 01:43:12 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: df.c,v 1.97 2020/08/21 16:41:06 ryo Exp $ */
+/*     $NetBSD: df.c,v 1.98 2021/01/03 01:43:12 ginsbach Exp $ */
 
 /*
  * Copyright (c) 1980, 1990, 1993, 1994
@@ -45,7 +45,7 @@
 #if 0
 static char sccsid[] = "@(#)df.c       8.7 (Berkeley) 4/2/94";
 #else
-__RCSID("$NetBSD: df.c,v 1.97 2020/08/21 16:41:06 ryo Exp $");
+__RCSID("$NetBSD: df.c,v 1.98 2021/01/03 01:43:12 ginsbach Exp $");
 #endif
 #endif /* not lint */
 
@@ -67,6 +67,7 @@
 #include <util.h>
 
 static char    *getmntpt(const char *);
+static void     addstat(struct statvfs *, const struct statvfs *);
 static void     prtstat(const struct statvfs *, int);
 static int      selected(const char *, size_t);
 static void     maketypelist(char *);
@@ -75,7 +76,7 @@
 static void     prthumanval(int64_t, int);
 static void     prthuman(const struct statvfs *, int64_t, int64_t);
 
-static int      aflag, gflag, hflag, iflag, lflag, nflag, Pflag, Wflag;
+static int      aflag, cflag, gflag, hflag, iflag, lflag, nflag, Pflag, Wflag;
 static long     usize;
 static char    **typelist;
 
@@ -87,7 +88,7 @@
 main(int argc, char *argv[])
 {
        struct stat stbuf;
-       struct statvfs *mntbuf;
+       struct statvfs *mntbuf, totals;
        int ch, maxwidth, width;
        size_t i, mntcount;
        char *mntpt;
@@ -95,11 +96,14 @@
        setprogname(argv[0]);
        (void)setlocale(LC_ALL, "");
 
-       while ((ch = getopt(argc, argv, "aGghiklmnPt:W")) != -1)
+       while ((ch = getopt(argc, argv, "acGghiklmnPt:W")) != -1)
                switch (ch) {
                case 'a':
                        aflag = 1;
                        break;
+               case 'c':
+                       cflag = 1;
+                       break;
                case 'g':
                        hflag = 0;
                        usize = 1024 * 1024 * 1024;
@@ -207,15 +211,28 @@
                }
        }
 
+       if (cflag) {
+               memset(&totals, 0, sizeof(totals));
+               totals.f_bsize = DEV_BSIZE;
+               strlcpy(totals.f_mntfromname, "total",
+                       sizeof(totals.f_mntfromname));
+       }
+
        maxwidth = 0;
        for (i = 0; i < mntcount; i++) {
                width = (int)strlen(Wflag && mntbuf[i].f_mntfromlabel[0] ?
                    mntbuf[i].f_mntfromlabel : mntbuf[i].f_mntfromname);
                if (width > maxwidth)
                        maxwidth = width;
+               if (cflag)
+                       addstat(&totals, &mntbuf[i]);
        }
        for (i = 0; i < mntcount; i++)
                prtstat(&mntbuf[i], maxwidth);
+
+       if (cflag)
+               prtstat(&totals, maxwidth);
+
        return 0;
 }
 
@@ -361,6 +378,22 @@
            (int64_t)(num) / (int64_t)((bs) / (fsbs)) :         \
            (int64_t)(num) * (int64_t)((fsbs) / (bs)))
 
+static void
+addstat(struct statvfs *totalfsp, const struct statvfs *sfsp)
+{
+       uint64_t frsize;
+
+       frsize = sfsp->f_frsize / totalfsp->f_frsize;
+       totalfsp->f_blocks += sfsp->f_blocks * frsize;
+       totalfsp->f_bfree += sfsp->f_bfree * frsize;
+       totalfsp->f_bavail += sfsp->f_bavail * frsize;
+       totalfsp->f_bresvd += sfsp->f_bresvd * frsize;
+       totalfsp->f_files += sfsp->f_files;
+       totalfsp->f_ffree += sfsp->f_ffree;
+       totalfsp->f_favail += sfsp->f_favail;
+       totalfsp->f_fresvd += sfsp->f_fresvd;
+}
+
 /*
  * Print out status about a filesystem.
  */



Home | Main Index | Thread Index | Old Index