sbase/libutil/human.c
FRIGN 5595af5742 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.
2015-04-25 11:43:14 +01:00

25 lines
446 B
C

/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <string.h>
#include "../util.h"
char *
humansize(size_t n)
{
static char buf[16];
const char postfixes[] = "BKMGTPE";
double size;
int i;
for (size = n, i = 0; size >= 1024 && i < strlen(postfixes); i++)
size /= 1024;
if (!i)
snprintf(buf, sizeof(buf), "%zu", n);
else
snprintf(buf, sizeof(buf), "%.1f%c", size, postfixes[i]);
return buf;
}