From 0b938f26ec601b6370d41ca43033674c360a2f36 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Fri, 18 Oct 1996 18:16:08 +0000 Subject: Implement a -c (Grand Total) option, a'la GNU du(1). From Luke Mewburn , NetBSD PR #2805. Also -Wall happiness. --- usr.bin/du/du.1 | 10 ++++++---- usr.bin/du/du.c | 33 ++++++++++++++++++++++----------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/usr.bin/du/du.1 b/usr.bin/du/du.1 index 64445bc7d43..7be3636fe38 100644 --- a/usr.bin/du/du.1 +++ b/usr.bin/du/du.1 @@ -1,5 +1,5 @@ -.\" $OpenBSD: du.1,v 1.3 1996/06/26 05:32:37 deraadt Exp $ -.\" $NetBSD: du.1,v 1.4.2.1 1995/12/05 02:45:41 jtc Exp $ +.\" $OpenBSD: du.1,v 1.4 1996/10/18 18:16:06 millert Exp $ +.\" $NetBSD: du.1,v 1.6 1996/10/18 07:20:31 thorpej Exp $ .\" .\" Copyright (c) 1990, 1993 .\" The Regents of the University of California. All rights reserved. @@ -34,7 +34,7 @@ .\" .\" @(#)du.1 8.2 (Berkeley) 4/1/94 .\" -.Dd April 1, 1994 +.Dd October 4, 1996 .Dt DU 1 .Os .Sh NAME @@ -44,7 +44,7 @@ .Nm du .Op Fl H | Fl L | Fl P .Op Fl a | Fl s -.Op Fl kx +.Op Fl ckx .Op Ar file ... .Sh DESCRIPTION The @@ -77,6 +77,8 @@ If the flag is specified, the number displayed is the number of 1024-byte blocks. Partial numbers of blocks are rounded up. +.It Fl c +Display the grand total after all the arguments have been processed. .It Fl s Display only the grand total for the specified files. .It Fl x 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); } -- cgit v1.2.3