separate humansize into a util function

also show 1 decimal of human size string like: 4M -> 4.4M
This commit is contained in:
Hiltjo Posthuma
2014-10-18 21:25:00 +00:00
committed by sin
parent ff93350289
commit b6b8fe9591
4 changed files with 24 additions and 19 deletions

21
util/human.c Normal file
View File

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