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:
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user