[hackers] [sbase] ls: add -h flag || Hiltjo Posthuma

From: <git_AT_suckless.org>
Date: Sun, 19 Oct 2014 13:54:28 +0200

commit 4d4e2608c12962620cfc175528085b4f853a32a6
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Sun Oct 19 09:48:04 2014 +0000

    ls: add -h flag
    
    for util/human don't show "B" for bytes.

diff --git a/ls.1 b/ls.1
index 1c01d13..ec61bee 100644
--- a/ls.1
+++ b/ls.1
_AT_@ -20,6 +20,9 @@ lists directories themselves, not their contents.
 .B \-F
 append a file type indicator to files.
 .TP
+.B \-h
+show filesizes in human\-readable format.
+.TP
 .B \-i
 print the index number of each file.
 .TP
diff --git a/ls.c b/ls.c
index 32c0eb7..187a522 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -32,6 +32,7 @@ static void output(Entry *);
 static bool aflag = false;
 static bool dflag = false;
 static bool Fflag = false;
+static bool hflag = false;
 static bool iflag = false;
 static bool lflag = false;
 static bool rflag = false;
_AT_@ -43,7 +44,7 @@ static bool many;
 static void
 usage(void)
 {
- eprintf("usage: %s [-1adFilrtU] [FILE...]\n", argv0);
+ eprintf("usage: %s [-1adFhilrtU] [FILE...]\n", argv0);
 }
 
 int
_AT_@ -65,6 +66,9 @@ main(int argc, char *argv[])
         case 'F':
                 Fflag = true;
                 break;
+ case 'h':
+ hflag = true;
+ break;
         case 'i':
                 iflag = true;
                 break;
_AT_@ -282,8 +286,12 @@ output(Entry *ent)
                 fmt = "%b %d %H:%M";
 
         strftime(buf, sizeof buf, fmt, localtime(&ent->mtime));
- printf("%s %4ld %-8.8s %-8.8s %10lu %s %s%s", mode, (long)ent->nlink, pwname,
- grname, (unsigned long)ent->size, buf, ent->name, indicator(ent->mode));
+ printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname);
+ if(hflag)
+ printf("%10s ", humansize((unsigned long)ent->size));
+ else
+ printf("%10lu ", (unsigned long)ent->size);
+ printf("%s %s%s", buf, ent->name, indicator(ent->mode));
         if(S_ISLNK(ent->mode)) {
                 if((len = readlink(ent->name, buf, sizeof buf)) == -1)
                         eprintf("readlink %s:", ent->name);
diff --git a/util/human.c b/util/human.c
index e0800bf..422407b 100644
--- a/util/human.c
+++ b/util/human.c
_AT_@ -7,14 +7,14 @@ char *
 humansize(double n)
 {
         static char buf[16];
- const char postfixes[] = " KMGTPE";
+ const char postfixes[] = "BKMGTPE";
         size_t i;
 
         for(i = 0; n >= 1024 && i < strlen(postfixes); i++)
                 n /= 1024;
 
         if(!i)
- snprintf(buf, sizeof(buf), "%lu%c", (unsigned long)n, postfixes[i]);
+ snprintf(buf, sizeof(buf), "%lu", (unsigned long)n);
         else
                 snprintf(buf, sizeof(buf), "%.1f%c", n, postfixes[i]);
         return buf;
Received on Sun Oct 19 2014 - 13:54:28 CEST

This archive was generated by hypermail 2.3.0 : Sun Oct 19 2014 - 14:00:11 CEST