micro-utils/libmu/human.c

25 lines
438 B
C
Raw Normal View History

2024-07-09 22:33:45 +03:00
#include "human.h"
2024-07-01 13:23:00 +03:00
2024-07-09 22:54:12 +03:00
char mu_hs_buf[MU_HUMAN_BUF_SIZE + 1];
2024-07-10 22:59:28 +03:00
char *mu_humansize(off_t n, int block) {
2024-07-01 13:23:00 +03:00
memset(mu_hs_buf, '\0', sizeof(mu_hs_buf));
char *postfixes = "BKMGTPE";
2024-07-10 22:59:28 +03:00
size_t i = 0;
2024-07-01 13:23:00 +03:00
double size = n;
2024-07-10 22:59:28 +03:00
while (size > (off_t)block) {
size /= (off_t)block;
i++;
}
2024-07-01 13:23:00 +03:00
if (i)
snprintf(mu_hs_buf, sizeof(mu_hs_buf), "%.1f%c", size, postfixes[i]);
else
2024-07-10 22:59:28 +03:00
snprintf(mu_hs_buf, sizeof(mu_hs_buf), "%jd", n);
2024-07-01 13:23:00 +03:00
return mu_hs_buf;
}