micro-utils/include/libmu/human.h

27 lines
456 B
C
Raw Normal View History

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