31 lines
500 B
C
31 lines
500 B
C
#ifndef _HUMAN_H
|
|
#define _HUMAN_H
|
|
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
|
|
#define MU_HUMAN_BUF_SIZE 16
|
|
|
|
char *mu_humansize(off_t n, off_t block) {
|
|
static char buf[MU_HUMAN_BUF_SIZE + 1];
|
|
memset(buf, '\0', sizeof(buf));
|
|
|
|
char *postfixes = "BKMGTPE";
|
|
|
|
double size = n;
|
|
size_t i;
|
|
|
|
for (i = 0; i < strlen(postfixes) && size >= block; i++)
|
|
size /= block;
|
|
|
|
if (i)
|
|
snprintf(buf, sizeof(buf), "%.1f%c", size, postfixes[i]);
|
|
|
|
else
|
|
snprintf(buf, sizeof(buf), "%ju", n);
|
|
|
|
return buf;
|
|
}
|
|
|
|
#endif
|