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 least in a simple 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 --git a/du.c b/du.c
index 4e94cc5..d27ca1f 100644
--- a/du.c
+++ b/du.c
@@ -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);
@@ -53,6 +54,9 @@ main(int argc, char *argv[])
        case 'k':
                kflag = true;
                break;
+       case 'h':
+               hflag = true;
+               break;
        default:
                usage();
        } ARGEND;
@@ -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 *

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to