sbase/util/human.c
Hiltjo Posthuma 4d4e2608c1 ls: add -h flag
for util/human don't show "B" for bytes.
2014-10-19 12:54:19 +01:00

22 lines
370 B
C

#include <stdio.h>
#include <string.h>
#include "../util.h"
char *
humansize(double n)
{
static char buf[16];
const char postfixes[] = "BKMGTPE";
size_t i;
for(i = 0; n >= 1024 && i < strlen(postfixes); i++)
n /= 1024;
if(!i)
snprintf(buf, sizeof(buf), "%lu", (unsigned long)n);
else
snprintf(buf, sizeof(buf), "%.1f%c", n, postfixes[i]);
return buf;
}