aboutsummaryrefslogtreecommitdiff
path: root/utils.h
diff options
context:
space:
mode:
Diffstat (limited to 'utils.h')
-rw-r--r--utils.h19
1 files changed, 19 insertions, 0 deletions
diff --git a/utils.h b/utils.h
index b4ed2a2..b9fff6e 100644
--- a/utils.h
+++ b/utils.h
@@ -67,4 +67,23 @@ static inline void parse_cmdline(struct cmd_options * const opts, int argc, char
}
}
+static inline char * prettify_bytes_with_units(char * const out, size_t out_size,
+ unsigned long long bytes)
+{
+ static char const * const unit_prefixes[] = {"","Kilo","Mega","Giga","Tera"};
+ size_t const unit_prefixes_length = sizeof(unit_prefixes)/sizeof(unit_prefixes[0]);
+ unsigned char unit_prefixes_index = 0;
+ size_t const convert_bytes_every = 1024;
+
+ while (bytes / convert_bytes_every > 0 && unit_prefixes_index < unit_prefixes_length)
+ {
+ bytes /= convert_bytes_every;
+ unit_prefixes_index++;
+ }
+
+ snprintf(out, out_size, "%llu %sBytes", bytes, unit_prefixes[unit_prefixes_index]);
+
+ return out;
+}
+
#endif