This adds a second parameter to size_to_human_string() to return a string with a different format based on the following flags: ONE_LETTER_SUFFIX (1K), THREE_LETTERS_SUFFIX (1KiB), SPACE_BEFORE_UNIT (1 KiB or 1 K). Signed-off-by: Francesco Cosoleto <cosoleto@xxxxxxxxx> --- include/strutils.h | 11 ++++++++++- lib/strutils.c | 27 +++++++++++++++++++-------- misc-utils/lsblk.c | 6 +++--- partx/partx.c | 2 +- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/include/strutils.h b/include/strutils.h index 99d8acd..d153be9 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 +{ + ONE_LETTER_SUFFIX = 0, + THREE_LETTERS_SUFFIX = 1, + SPACE_BEFORE_UNIT = 2 +}; + +extern char *size_to_human_string(int options, uint64_t bytes); #endif diff --git a/lib/strutils.c b/lib/strutils.c index 21c58da..a06da3c 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,31 @@ 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 c; + char suffix[4], *psuf = suffix; + + if (options & SPACE_BEFORE_UNIT) + *psuf++ = ' '; exp = get_exp(bytes); - c = *(letters + (exp ? exp / 10 : 0)); + *psuf++ = *(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); + if (options & THREE_LETTERS_SUFFIX) { + *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 +307,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); } @@ -319,7 +330,7 @@ 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(ONE_LETTER_SUFFIX, size); printf("%25s : %20ju : %8s\n", argv[1], size, hum); free(hum); diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 271999f..764ecbc 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(ONE_LETTER_SUFFIX, 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(ONE_LETTER_SUFFIX, 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(ONE_LETTER_SUFFIX, x); } if (p) tt_line_set_data(ln, col, p); diff --git a/partx/partx.c b/partx/partx.c index 5979701..71edba2 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(ONE_LETTER_SUFFIX, blkid_partition_get_size(par) << 9); break; case COL_NAME: -- 1.7.3.4 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html