1
0
Fork 0

Add fmt_scaled util function

This commit is contained in:
Aaron Marcher 2018-05-18 23:14:10 +02:00
parent f317819984
commit 35219d39ca
2 changed files with 17 additions and 0 deletions

16
util.c
View file

@ -65,6 +65,22 @@ bprintf(const char *fmt, ...)
return buf; return buf;
} }
const char *
fmt_scaled(size_t bytes)
{
unsigned int i;
float scaled;
const char *units[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB",
"ZiB", "YiB" };
scaled = bytes;
for (i = 0; i < LEN(units) && scaled >= 1024; i++) {
scaled /= 1024.0;
}
return bprintf("%.1f%s", scaled, units[i]);
}
int int
pscanf(const char *path, const char *fmt, ...) pscanf(const char *path, const char *fmt, ...)
{ {

1
util.h
View file

@ -9,4 +9,5 @@ void warn(const char *, ...);
void die(const char *, ...); void die(const char *, ...);
const char *bprintf(const char *fmt, ...); const char *bprintf(const char *fmt, ...);
const char *fmt_scaled(size_t);
int pscanf(const char *path, const char *fmt, ...); int pscanf(const char *path, const char *fmt, ...);