Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/du human-readable output via -h flag. match output ...



details:   https://anonhg.NetBSD.org/src/rev/9fcf0b4d53f6
branches:  trunk
changeset: 537118:9fcf0b4d53f6
user:      provos <provos%NetBSD.org@localhost>
date:      Fri Sep 27 03:33:33 2002 +0000

description:
human-readable output via -h flag.  match output from Linux and other BSDs.
Partly by Benedikt Meurer <bmeurer%fwdn.de@localhost>.  approved by perry. closes
pr 14687 and 18402.

diffstat:

 usr.bin/du/du.1 |  10 ++++++++--
 usr.bin/du/du.c |  47 ++++++++++++++++++++++++++++++++++-------------
 2 files changed, 42 insertions(+), 15 deletions(-)

diffs (151 lines):

diff -r 715632835b8d -r 9fcf0b4d53f6 usr.bin/du/du.1
--- a/usr.bin/du/du.1   Fri Sep 27 03:17:40 2002 +0000
+++ b/usr.bin/du/du.1   Fri Sep 27 03:33:33 2002 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: du.1,v 1.12 2001/12/01 19:03:31 wiz Exp $
+.\"    $NetBSD: du.1,v 1.13 2002/09/27 03:33:33 provos Exp $
 .\"
 .\" Copyright (c) 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -43,7 +43,7 @@
 .Nm
 .Op Fl H | Fl L | Fl P
 .Op Fl a | Fl s
-.Op Fl ckmrx
+.Op Fl chkmrx
 .Op Ar file ...
 .Sh DESCRIPTION
 The
@@ -80,6 +80,12 @@
 .Fl m
 flag is specified, the number displayed is the number of megabyte
 (1024*1024 bytes) blocks.
+.It Fl h
+If the
+.Fl h
+flag is specified, the numbers will be displayed in "human-readable"
+format. Use unit suffixes: B (Byte), K (Kilobyte), M (Megabyte),
+G (Gigabyte), T (Terabyte) and P (Petabyte).
 .It Fl c
 Display the grand total after all the arguments have been processed.
 .It Fl r
diff -r 715632835b8d -r 9fcf0b4d53f6 usr.bin/du/du.c
--- a/usr.bin/du/du.c   Fri Sep 27 03:17:40 2002 +0000
+++ b/usr.bin/du/du.c   Fri Sep 27 03:33:33 2002 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: du.c,v 1.17 2001/01/04 23:05:54 lukem Exp $    */
+/*     $NetBSD: du.c,v 1.18 2002/09/27 03:33:33 provos Exp $   */
 
 /*
  * Copyright (c) 1989, 1993, 1994
@@ -46,7 +46,7 @@
 #if 0
 static char sccsid[] = "@(#)du.c       8.5 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: du.c,v 1.17 2001/01/04 23:05:54 lukem Exp $");
+__RCSID("$NetBSD: du.c,v 1.18 2002/09/27 03:33:33 provos Exp $");
 #endif
 #endif /* not lint */
 
@@ -57,6 +57,7 @@
 #include <err.h>
 #include <errno.h>
 #include <fts.h>
+#include <util.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -64,8 +65,12 @@
 
 int    linkchk __P((FTSENT *));
 int    main __P((int, char **));
+void   prstat __P((const char *, int64_t));
 void   usage __P((void));
 
+int hflag;
+long blocksize;
+
 int
 main(argc, argv)
        int argc;
@@ -82,7 +87,7 @@
        Hflag = Lflag = Pflag = aflag = cflag = kmflag = sflag = 0;
        totalblocks = 0;
        ftsoptions = FTS_PHYSICAL;
-       while ((ch = getopt(argc, argv, "HLPackmrsx")) != -1)
+       while ((ch = getopt(argc, argv, "HLPachkmrsx")) != -1)
                switch (ch) {
                case 'H':
                        Hflag = 1;
@@ -102,6 +107,9 @@
                case 'c':
                        cflag = 1;
                        break;
+               case 'h':
+                       hflag = 1;
+                       break;
                case 'k':
                        blocksize = 1024;
                        kmflag = 1;
@@ -183,9 +191,7 @@
                         * root of a traversal, display the total.
                         */
                        if (listdirs || (!listfiles && !p->fts_level))
-                               (void)printf("%ld\t%s\n",
-                                   howmany(p->fts_number, blocksize),
-                                   p->fts_path);
+                               prstat(p->fts_path, p->fts_number);
                        break;
                case FTS_DC:                    /* Ignore. */
                        break;
@@ -203,9 +209,7 @@
                         * the root of a traversal, display the total.
                         */
                        if (listfiles || !p->fts_level)
-                               (void)printf("%lld\t%s\n", (long long)
-                                   howmany(p->fts_statp->st_blocks, blocksize),
-                                   p->fts_path);
+                               prstat(p->fts_path, p->fts_statp->st_blocks);
                        p->fts_parent->fts_number += p->fts_statp->st_blocks;
                        if (cflag)
                                totalblocks += p->fts_statp->st_blocks;
@@ -213,11 +217,28 @@
        if (errno)
                err(1, "fts_read");
        if (cflag)
-               (void)printf("%ld\ttotal\n",
-                   howmany(totalblocks, blocksize));
-       exit(0);
+               prstat("total", totalblocks);
+       exit(rval);
 }
 
+void
+prstat(const char *fname, int64_t blocks)
+{
+       if (hflag) {
+               char buf[5];
+               int64_t sz = blocks * 512;
+
+               humanize_number(buf, sizeof(buf), sz, "", HN_AUTOSCALE,
+                   HN_B | HN_NOSPACE | HN_DECIMAL);
+
+               (void)printf("%s\t%s\n", buf, fname);
+       } else
+               (void)printf("%lld\t%s\n",
+                   (long long)howmany(blocks, (int64_t)blocksize),
+                   fname);
+}
+
+
 typedef struct _ID {
        dev_t   dev;
        ino_t   inode;
@@ -254,6 +275,6 @@
 {
 
        (void)fprintf(stderr,
-               "usage: du [-H | -L | -P] [-a | -s] [-ckmrx] [file ...]\n");
+               "usage: du [-H | -L | -P] [-a | -s] [-chkmrx] [file ...]\n");
        exit(1);
 }



Home | Main Index | Thread Index | Old Index