summaryrefslogtreecommitdiff
path: root/usr.bin/du/du.c
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>1996-10-18 18:16:08 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>1996-10-18 18:16:08 +0000
commit0b938f26ec601b6370d41ca43033674c360a2f36 (patch)
tree7478fd35826bd331594a5e40b1da3a92dcb6233a /usr.bin/du/du.c
parentfdcaeeb8a32ca664530ddfef5db66722ca9ed6d0 (diff)
Implement a -c (Grand Total) option, a'la GNU du(1).
From Luke Mewburn <lukem@telstra.com.au>, NetBSD PR #2805. Also -Wall happiness.
Diffstat (limited to 'usr.bin/du/du.c')
-rw-r--r--usr.bin/du/du.c33
1 files changed, 22 insertions, 11 deletions
diff --git a/usr.bin/du/du.c b/usr.bin/du/du.c
index 559cb409a6c..31771abb7a9 100644
--- a/usr.bin/du/du.c
+++ b/usr.bin/du/du.c
@@ -1,5 +1,5 @@
-/* $OpenBSD: du.c,v 1.2 1996/06/26 05:32:38 deraadt Exp $ */
-/* $NetBSD: du.c,v 1.10 1995/09/28 06:19:56 perry Exp $ */
+/* $OpenBSD: du.c,v 1.3 1996/10/18 18:16:07 millert Exp $ */
+/* $NetBSD: du.c,v 1.11 1996/10/18 07:20:35 thorpej Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
@@ -47,7 +47,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)du.c 8.5 (Berkeley) 5/4/95";
#else
-static char rcsid[] = "$OpenBSD: du.c,v 1.2 1996/06/26 05:32:38 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: du.c,v 1.3 1996/10/18 18:16:07 millert Exp $";
#endif
#endif /* not lint */
@@ -73,15 +73,16 @@ main(argc, argv)
{
FTS *fts;
FTSENT *p;
- long blocksize;
+ long blocksize, totalblocks;
int ftsoptions, listdirs, listfiles;
- int Hflag, Lflag, Pflag, aflag, ch, kflag, notused, rval, sflag;
+ int Hflag, Lflag, Pflag, aflag, ch, cflag, kflag, notused, rval, sflag;
char **save;
save = argv;
- Hflag = Lflag = Pflag = aflag = kflag = sflag = 0;
+ Hflag = Lflag = Pflag = aflag = cflag = kflag = sflag = 0;
+ totalblocks = 0;
ftsoptions = FTS_PHYSICAL;
- while ((ch = getopt(argc, argv, "HLPaksx")) != EOF)
+ while ((ch = getopt(argc, argv, "HLPacksx")) != EOF)
switch (ch) {
case 'H':
Hflag = 1;
@@ -98,6 +99,9 @@ main(argc, argv)
case 'a':
aflag = 1;
break;
+ case 'c':
+ cflag = 1;
+ break;
case 'k':
blocksize = 1024;
kflag = 1;
@@ -156,7 +160,7 @@ main(argc, argv)
blocksize /= 512;
if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
- err(1, NULL);
+ err(1, "fts_open");
for (rval = 0; (p = fts_read(fts)) != NULL;)
switch (p->fts_info) {
@@ -165,12 +169,14 @@ main(argc, argv)
case FTS_DP:
p->fts_parent->fts_number +=
p->fts_number += p->fts_statp->st_blocks;
+ if (cflag)
+ totalblocks += p->fts_statp->st_blocks;
/*
* If listing each directory, or not listing files
* or directories and this is post-order of the
* root of a traversal, display the total.
*/
- if (listdirs || !listfiles && !p->fts_level)
+ if (listdirs || (!listfiles && !p->fts_level))
(void)printf("%ld\t%s\n",
howmany(p->fts_number, blocksize),
p->fts_path);
@@ -195,9 +201,14 @@ main(argc, argv)
howmany(p->fts_statp->st_blocks, blocksize),
p->fts_path);
p->fts_parent->fts_number += p->fts_statp->st_blocks;
+ if (cflag)
+ totalblocks += p->fts_statp->st_blocks;
}
if (errno)
err(1, "fts_read");
+ if (cflag)
+ (void)printf("%ld\ttotal\n",
+ howmany(totalblocks, blocksize));
exit(0);
}
@@ -225,7 +236,7 @@ linkchk(p)
if (nfiles == maxfiles && (files = realloc((char *)files,
(u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
- err(1, "");
+ err(1, "can't allocate memory");
files[nfiles].inode = ino;
files[nfiles].dev = dev;
++nfiles;
@@ -237,6 +248,6 @@ usage()
{
(void)fprintf(stderr,
- "usage: du [-H | -L | -P] [-a | -s] [-kx] [file ...]\n");
+ "usage: du [-H | -L | -P] [-a | -s] [-ckx] [file ...]\n");
exit(1);
}