sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

commit 1a524b405b329460c9f990fe748baade17985566
parent 0c991c000fae69f54bf714d7bf4942567152e271
Author: FRIGN <dev@frign.de>
Date:   Sat Apr 25 00:27:20 +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.

Diffstat:
libutil/human.c | 14++++++++------
ls.c | 2+-
util.h | 2+-
3 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/libutil/human.c b/libutil/human.c @@ -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 @@ -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 @@ -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