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