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.
This commit is contained in:
FRIGN 2015-04-25 00:27:20 +02:00 committed by sin
parent 9016d288f1
commit 5595af5742
3 changed files with 10 additions and 8 deletions

View File

@ -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;
}

2
ls.c
View File

@ -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));

2
util.h
View File

@ -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