[hackers] [sbase] Convert humansize() to accept a size_t instead of a double || FRIGN

From: <git_AT_suckless.org>
Date: Sat, 25 Apr 2015 12:43:21 +0200 (CEST)

commit 5595af5742d6d9eb193a563d126c50626ee0fd71
Author: FRIGN <dev_AT_frign.de>
Date: Sat Apr 25 00:27:20 2015 +0200

    Convert humansize() to accept a size_t instead of a double
    
    General convention is to use size_t to store sizes of all kinds.
    Internally, the function uses double anyway, but at least this
    doesn't clobber up the API any more and there's a chance in the
    future to make this function a bit cleaner and not use this dirty
    static buffer hack any more.

diff --git a/libutil/human.c b/libutil/human.c
index 5527bce..eb5d0b7 100644
--- a/libutil/human.c
+++ b/libutil/human.c
_AT_@ -5,18 +5,20 @@
 #include "../util.h"
 
 char *
-humansize(double n)
+humansize(size_t n)
 {
         static char buf[16];
         const char postfixes[] = "BKMGTPE";
- size_t i;
+ double size;
+ int i;
 
- for (i = 0; n >= 1024 && i < strlen(postfixes); i++)
- n /= 1024;
+ for (size = n, i = 0; size >= 1024 && i < strlen(postfixes); i++)
+ size /= 1024;
 
         if (!i)
- snprintf(buf, sizeof(buf), "%lu", (unsigned long)n);
+ snprintf(buf, sizeof(buf), "%zu", n);
         else
- snprintf(buf, sizeof(buf), "%.1f%c", n, postfixes[i]);
+ snprintf(buf, sizeof(buf), "%.1f%c", size, postfixes[i]);
+
         return buf;
 }
diff --git a/ls.c b/ls.c
index 91c055d..bb6403f 100644
--- a/ls.c
+++ b/ls.c
_AT_@ -162,7 +162,7 @@ output(const struct entry *ent)
         printf("%s %4ld %-8.8s %-8.8s ", mode, (long)ent->nlink, pwname, grname);
 
         if (hflag)
- printf("%10s ", humansize((unsigned long)ent->size));
+ printf("%10s ", humansize(ent->size));
         else
                 printf("%10lu ", (unsigned long)ent->size);
         printf("%s %s%s", buf, ent->name, indicator(ent->mode));
diff --git a/util.h b/util.h
index 6bc37ee..bd8ddc0 100644
--- a/util.h
+++ b/util.h
_AT_@ -66,7 +66,7 @@ int eregcomp(regex_t *, const char *, int);
 void enmasse(int, char **, int (*)(const char *, const char *, int));
 void fnck(const char *, const char *, int (*)(const char *, const char *, int), int);
 mode_t getumask(void);
-char *humansize(double);
+char *humansize(size_t);
 mode_t parsemode(const char *, mode_t, mode_t);
 void putword(FILE *, const char *);
 #undef strtonum
Received on Sat Apr 25 2015 - 12:43:21 CEST

This archive was generated by hypermail 2.3.0 : Sat Apr 25 2015 - 12:48:12 CEST