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).
This commit is contained in:
ginsbach 2021-01-03 01:43:12 +00:00
parent 4c353c3815
commit f40edf74af
2 changed files with 42 additions and 7 deletions

View File

@ -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 @@ Show all mount points,
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 .

View File

@ -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 @@ __COPYRIGHT(
#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 @@ __RCSID("$NetBSD: df.c,v 1.97 2020/08/21 16:41:06 ryo Exp $");
#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 @@ __dead static void usage(void);
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 @@ int
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 @@ main(int argc, char *argv[])
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 @@ main(int argc, char *argv[])
}
}
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 @@ prthuman(const struct statvfs *sfsp, int64_t used, int64_t bavail)
(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.
*/