micro-utils/include/libmu/human.h

25 lines
392 B
C
Raw Normal View History

2023-11-21 19:18:42 +00:00
#ifndef _HUMAN_H
#define _HUMAN_H
#include <stdint.h>
2023-11-21 19:18:42 +00:00
char *mu_humansize(off_t n) {
static char buf[16];
char *postfixes = "BKMGTPE";
double size = n;
size_t i;
for (i = 0; i < strlen(postfixes) && size >= 1024; i++)
size /= 1024;
if (i)
snprintf(buf, sizeof(buf), "%.1f%c", size, postfixes[i]);
else
snprintf(buf, sizeof(buf), "%ju", (uintmax_t)n);
return buf;
}
#endif