summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'util.c')
-rw-r--r--util.c32
1 files changed, 12 insertions, 20 deletions
diff --git a/util.c b/util.c
index 6dd08d7..fc8b72a 100644
--- a/util.c
+++ b/util.c
@@ -87,32 +87,24 @@ bprintf(const char *fmt, ...)
}
const char *
-fmt_human_2(size_t num)
+fmt_human(size_t num, int base)
{
size_t i;
double scaled;
- const char *prefix[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei",
- "Zi", "Yi" };
-
- scaled = num;
- for (i = 0; i < LEN(prefix) && scaled >= 1024; i++) {
- scaled /= 1024.0;
+ const char *siprefix[] = { "", "k", "M", "G", "T", "P", "E", "Z", "Y" };
+ const char *iecprefix[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei",
+ "Zi", "Yi" };
+ char *prefix[9];
+
+ if (base == 1000) {
+ memcpy(prefix, siprefix, sizeof(prefix));
+ } else if (base == 1024) {
+ memcpy(prefix, iecprefix, sizeof(prefix));
}
- return bprintf("%.1f%s", scaled, prefix[i]);
-}
-
-const char *
-fmt_human_10(size_t num)
-{
- size_t i;
- double scaled;
- const char *prefix[] = { "", "K", "M", "G", "T", "P", "E",
- "Z", "Y" };
-
scaled = num;
- for (i = 0; i < LEN(prefix) && scaled >= 1000; i++) {
- scaled /= 1000.0;
+ for (i = 0; i < LEN(prefix) && scaled >= 1024; i++) {
+ scaled /= base;
}
return bprintf("%.1f%s", scaled, prefix[i]);