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