From 5d2a98490e87872214e6acdeac1031c0eb8cfc92 Mon Sep 17 00:00:00 2001 From: Francesco Cosoleto Date: Thu, 26 May 2011 15:17:25 +0200 Subject: This adds a second parameter to size_to_human_string() to return a string with a different format based on the following flags: SIZE_SUFFIX_1LETTER = "1K" SIZE_SUFFIX_3LETTER = "1KiB", SIZE_SUFFIX_SPACE = "1 KiB" or "1 K" [kzak@redhat.com: - rename flags to SIZE_SUFFIX_* format, - fix suffix[] buffer size - add 3 letter version to the test] Signed-off-by: Francesco Cosoleto Signed-off-by: Karel Zak --- lib/strutils.c | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'lib/strutils.c') diff --git a/lib/strutils.c b/lib/strutils.c index 21c58daea..07010f32c 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -12,6 +12,7 @@ #include #include #include "c.h" +#include "strutils.h" static int do_scale_by_power (uintmax_t *x, int base, int power) { @@ -266,21 +267,34 @@ static int get_exp(uint64_t n) return shft - 10; } -char *size_to_human_string(uint64_t bytes) +char *size_to_human_string(int options, uint64_t bytes) { char buf[32]; int dec, exp; uint64_t frac; const char *letters = "BKMGTPE"; + char suffix[sizeof(" KiB")], *psuf = suffix; char c; + if (options & SIZE_SUFFIX_SPACE) + *psuf++ = ' '; + exp = get_exp(bytes); c = *(letters + (exp ? exp / 10 : 0)); dec = exp ? bytes / (1ULL << exp) : bytes; frac = exp ? bytes % (1ULL << exp) : 0; - /* fprintf(stderr, "exp: %d, c: %c, dec: %d, frac: %jd\n", - * exp, c, dec, frac); + *psuf++ = c; + + if ((options & SIZE_SUFFIX_3LETTER) && (c != 'B')) { + *psuf++ = 'i'; + *psuf++ = 'B'; + } + + *psuf = '\0'; + + /* fprintf(stderr, "exp: %d, unit: %c, dec: %d, frac: %jd\n", + * exp, suffix[0], dec, frac); */ if (frac) { @@ -296,9 +310,9 @@ char *size_to_human_string(uint64_t bytes) if (!dp || !*dp) dp = "."; - snprintf(buf, sizeof(buf), "%d%s%jd%c", dec, dp, frac, c); + snprintf(buf, sizeof(buf), "%d%s%jd%s", dec, dp, frac, suffix); } else - snprintf(buf, sizeof(buf), "%d%c", dec, c); + snprintf(buf, sizeof(buf), "%d%s", dec, suffix); return strdup(buf); } @@ -309,7 +323,7 @@ char *size_to_human_string(uint64_t bytes) int main(int argc, char *argv[]) { uintmax_t size = 0; - char *hum; + char *hum, *hum2; if (argc < 2) { fprintf(stderr, "usage: %s [suffix]\n", argv[0]); @@ -319,10 +333,13 @@ int main(int argc, char *argv[]) if (strtosize(argv[1], &size)) errx(EXIT_FAILURE, "invalid size '%s' value", argv[1]); - hum = size_to_human_string(size); + hum = size_to_human_string(SIZE_SUFFIX_1LETTER, size); + hum2 = size_to_human_string(SIZE_SUFFIX_3LETTER | + SIZE_SUFFIX_SPACE, size); - printf("%25s : %20ju : %8s\n", argv[1], size, hum); + printf("%25s : %20ju : %8s : %12s\n", argv[1], size, hum, hum2); free(hum); + free(hum2); return EXIT_FAILURE; } -- cgit v1.2.3-55-g7522