diff options
author | Francesco Cosoleto | 2011-05-26 15:17:25 +0200 |
---|---|---|
committer | Karel Zak | 2011-05-26 15:32:33 +0200 |
commit | 5d2a98490e87872214e6acdeac1031c0eb8cfc92 (patch) | |
tree | d2eaea544a6d8b662612e71395196c92bb6b5833 | |
parent | build-sys: use AUTOMAKE_OPTIONS = gnu (diff) | |
download | kernel-qcow2-util-linux-5d2a98490e87872214e6acdeac1031c0eb8cfc92.tar.gz kernel-qcow2-util-linux-5d2a98490e87872214e6acdeac1031c0eb8cfc92.tar.xz kernel-qcow2-util-linux-5d2a98490e87872214e6acdeac1031c0eb8cfc92.zip |
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 <cosoleto@gmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r-- | include/strutils.h | 11 | ||||
-rw-r--r-- | lib/strutils.c | 33 | ||||
-rw-r--r-- | misc-utils/lsblk.c | 6 | ||||
-rw-r--r-- | partx/partx.c | 2 | ||||
-rw-r--r-- | tests/expected/misc/strtosize | 52 |
5 files changed, 65 insertions, 39 deletions
diff --git a/include/strutils.h b/include/strutils.h index 99d8acd2c..bbe299385 100644 --- a/include/strutils.h +++ b/include/strutils.h @@ -27,6 +27,15 @@ static inline void xstrncpy(char *dest, const char *src, size_t n) } extern void strmode(mode_t mode, char *str); -extern char *size_to_human_string(uint64_t bytes); + +/* Options for size_to_human_string() */ +enum +{ + SIZE_SUFFIX_1LETTER = 0, + SIZE_SUFFIX_3LETTER = 1, + SIZE_SUFFIX_SPACE = 2 +}; + +extern char *size_to_human_string(int options, uint64_t bytes); #endif 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 <locale.h> #include <string.h> #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 <number>[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; } diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 271999fa4..b6bc28b7d 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -517,7 +517,7 @@ static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line if (asprintf(&p, "%jd", cxt->size) < 0) p = NULL; } else - p = size_to_human_string(cxt->size); + p = size_to_human_string(SIZE_SUFFIX_1LETTER, cxt->size); if (p) tt_line_set_data(ln, col, p); } @@ -572,7 +572,7 @@ static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line if (sysfs_read_u64(&cxt->sysfs, "queue/discard_granularity", &x) == 0) - p = size_to_human_string(x); + p = size_to_human_string(SIZE_SUFFIX_1LETTER, x); } if (p) tt_line_set_data(ln, col, p); @@ -585,7 +585,7 @@ static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line if (sysfs_read_u64(&cxt->sysfs, "queue/discard_max_bytes", &x) == 0) - p = size_to_human_string(x); + p = size_to_human_string(SIZE_SUFFIX_1LETTER, x); } if (p) tt_line_set_data(ln, col, p); diff --git a/partx/partx.c b/partx/partx.c index 5979701c5..8cb35703a 100644 --- a/partx/partx.c +++ b/partx/partx.c @@ -426,7 +426,7 @@ static void add_tt_line(struct tt *tt, blkid_partition par) rc = asprintf(&str, "%ju", (uintmax_t) blkid_partition_get_size(par) << 9); else - str = size_to_human_string( + str = size_to_human_string(SIZE_SUFFIX_1LETTER, blkid_partition_get_size(par) << 9); break; case COL_NAME: diff --git a/tests/expected/misc/strtosize b/tests/expected/misc/strtosize index 0dd5e3e76..94fd69a1f 100644 --- a/tests/expected/misc/strtosize +++ b/tests/expected/misc/strtosize @@ -1,30 +1,30 @@ test_strutils: invalid size '-1' value - 0 : 0 : 0B - 1 : 1 : 1B - 123 : 123 : 123B - 18446744073709551615 : 18446744073709551615 : 16E - 1K : 1024 : 1K - 1KiB : 1024 : 1K - 1M : 1048576 : 1M - 1MiB : 1048576 : 1M - 1G : 1073741824 : 1G - 1GiB : 1073741824 : 1G - 1T : 1099511627776 : 1T - 1TiB : 1099511627776 : 1T - 1P : 1125899906842624 : 1P - 1PiB : 1125899906842624 : 1P - 1E : 1152921504606846976 : 1E - 1EiB : 1152921504606846976 : 1E - 1KB : 1000 : 1000B - 1MB : 1000000 : 976.6K - 1GB : 1000000000 : 953.7M - 1TB : 1000000000000 : 931.3G - 1PB : 1000000000000000 : 909.5T - 1EB : 1000000000000000000 : 888.2P + 0 : 0 : 0B : 0 B + 1 : 1 : 1B : 1 B + 123 : 123 : 123B : 123 B + 18446744073709551615 : 18446744073709551615 : 16E : 16 EiB + 1K : 1024 : 1K : 1 KiB + 1KiB : 1024 : 1K : 1 KiB + 1M : 1048576 : 1M : 1 MiB + 1MiB : 1048576 : 1M : 1 MiB + 1G : 1073741824 : 1G : 1 GiB + 1GiB : 1073741824 : 1G : 1 GiB + 1T : 1099511627776 : 1T : 1 TiB + 1TiB : 1099511627776 : 1T : 1 TiB + 1P : 1125899906842624 : 1P : 1 PiB + 1PiB : 1125899906842624 : 1P : 1 PiB + 1E : 1152921504606846976 : 1E : 1 EiB + 1EiB : 1152921504606846976 : 1E : 1 EiB + 1KB : 1000 : 1000B : 1000 B + 1MB : 1000000 : 976.6K : 976.6 KiB + 1GB : 1000000000 : 953.7M : 953.7 MiB + 1TB : 1000000000000 : 931.3G : 931.3 GiB + 1PB : 1000000000000000 : 909.5T : 909.5 TiB + 1EB : 1000000000000000000 : 888.2P : 888.2 PiB test_strutils: invalid size '' value test_strutils: invalid size ' ' value - 1 : 1 : 1B + 1 : 1 : 1B : 1 B test_strutils: invalid size '1 ' value - 0x0a : 10 : 10B - 0xff00 : 65280 : 63.8K - 0x80000000 : 2147483648 : 2G + 0x0a : 10 : 10B : 10 B + 0xff00 : 65280 : 63.8K : 63.8 KiB + 0x80000000 : 2147483648 : 2G : 2 GiB |