[dev] [PATCH] [sbase] Adding human readable output to du

From: Jeffrey Picard <jeff_AT_jeffreypicard.com>
Date: Wed, 15 Oct 2014 20:35:46 -0400

Hey all,

I was poking around at du recently and noticed it doesn�t support human readable output. This is pretty easy to add however, at most in a simplistic form which just rounds the number of bytes down to the nearest increment of the highest power of 1024 that�s appropriate. If there�s interest in this, I�d also be happy to implement it in for df in ubase.

�Jeffrey Picard

diff --dropbox a/du.c b/du.c
index 4e94cc5..d27ca1f 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 - 02:35:46 CEST

This archive was generated by hypermail 2.3.0 : Thu Oct 16 2014 - 02:48:03 CEST