25 lines
438 B
C
25 lines
438 B
C
#include "human.h"
|
|
|
|
char mu_hs_buf[MU_HUMAN_BUF_SIZE + 1];
|
|
|
|
char *mu_humansize(off_t n, int block) {
|
|
memset(mu_hs_buf, '\0', sizeof(mu_hs_buf));
|
|
|
|
char *postfixes = "BKMGTPE";
|
|
size_t i = 0;
|
|
double size = n;
|
|
|
|
while (size > (off_t)block) {
|
|
size /= (off_t)block;
|
|
i++;
|
|
}
|
|
|
|
if (i)
|
|
snprintf(mu_hs_buf, sizeof(mu_hs_buf), "%.1f%c", size, postfixes[i]);
|
|
|
|
else
|
|
snprintf(mu_hs_buf, sizeof(mu_hs_buf), "%jd", n);
|
|
|
|
return mu_hs_buf;
|
|
}
|