summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorAaron Marcher <me@drkhsh.at>2018-05-18 23:14:10 +0200
committerAaron Marcher <me@drkhsh.at>2018-05-18 23:14:10 +0200
commit35219d39caa5ab5e0b1c9e6d4a1cbb29a388842e (patch)
tree4cd9f7b326daded9a4f6a15c54986a92065bb7df /util.c
parentf3178199848911c8fb51b30e3993ae47120fcc6c (diff)
Add fmt_scaled util function
Diffstat (limited to 'util.c')
-rw-r--r--util.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/util.c b/util.c
index 08f14ff..a7576b4 100644
--- a/util.c
+++ b/util.c
@@ -65,6 +65,22 @@ bprintf(const char *fmt, ...)
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
pscanf(const char *path, const char *fmt, ...)
{