[hackers] [sbase] Add human readable output to du(1) || sin

From: <git_AT_suckless.org>
Date: Thu, 16 Oct 2014 11:07:26 +0200

commit 4608d91c6d21fafe95bd167d754044c09aadb071
Author: sin <sin_AT_2f30.org>
Date: Thu Oct 16 10:06:27 2014 +0100

    Add human readable output to du(1)
    
    Thanks Jeffrey Picard!

diff --git a/LICENSE b/LICENSE
index ac9bee2..b58ca2f 100644
--- a/LICENSE
+++ b/LICENSE
_AT_@ -27,6 +27,7 @@ MIT/X Consortium License
 © 2014 Laslo Hunhold <dev_AT_frign.de>
 © 2014 Daniel Bainton <dpb_AT_driftaway.org>
 © 2014 Tuukka Kataja <stuge_AT_xor.fi>
+© 2014 Jeffrey Picard <jeff_AT_jeffreypicard.com>
 
 Permission is hereby granted, free of charge, to any person obtaining a
 copy of this software and associated documentation files (the "Software"),
diff --git a/du.1 b/du.1
index f4a0e70..7f2451d 100644
--- a/du.1
+++ b/du.1
_AT_@ -8,6 +8,7 @@ du \- display disk usage statistics
 .B \-s
 .RB ]
 .RB [ \-k ]
+.RB [ \-h ]
 .RI [ file ...]
 .SH DESCRIPTION
 .B du
_AT_@ -27,3 +28,6 @@ Display only the grand total for the specified files.
 .BI \-k
 By default all sizes are reported in 512-byte block counts.
 The -k option causes the numbers to be reported in kilobyte counts.
+.TP
+.BI \-h
+Enable human readable output.
diff --git a/du.c b/du.c
index 4e94cc5..98c81cd 100644
--- a/du.c
+++ b/du.c
_AT_@ -16,6 +16,7 @@ static char file[PATH_MAX];
 static bool aflag = false;
 static bool sflag = false;
 static bool kflag = false;
+static bool hflag = false;
 
 static long du(const char *);
 static void print(long n, char *path);
_AT_@ -53,6 +54,9 @@ main(int argc, char *argv[])
         case 'k':
                 kflag = true;
                 break;
+ case 'h':
+ hflag = true;
+ break;
         default:
                 usage();
         } ARGEND;
_AT_@ -82,9 +86,30 @@ main(int argc, char *argv[])
 }
 
 static void
+print_human(long n, char *path)
+{
+ long base = 1024;
+ long power = base;
+ char postfixes[] = {'B', 'K', 'M', 'G', 'T', 'P', 'E'};
+ int i = 0;
+
+ n = n * blksize;
+ while (n > power) {
+ power = power*base;
+ i++;
+ }
+
+ n = i ? n / (power / base) : n;
+ printf("%lu%c\t%s\n", n, postfixes[i], path);
+}
+
+static void
 print(long n, char *path)
 {
- printf("%lu\t%s\n", n, path);
+ if (hflag)
+ print_human(n, path);
+ else
+ printf("%lu\t%s\n", n, path);
 }
 
 static char *
Received on Thu Oct 16 2014 - 11:07:26 CEST

This archive was generated by hypermail 2.3.0 : Thu Oct 16 2014 - 11:12:13 CEST