diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2019-06-19 10:05:30 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2019-06-19 23:15:19 +0900 |
commit | b7de125bba74b23cec0e49189302655371d3e982 (patch) | |
tree | 1dbef303fe80da846e9d00053bcdaa55c556c945 | |
parent | aa89266900cbc7f9a89cf8480d1178855524af60 (diff) | |
download | systemd-b7de125bba74b23cec0e49189302655371d3e982.tar.gz |
util: make format_bytes() support e.g. 3.0E
-rw-r--r-- | src/basic/format-util.c | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/src/basic/format-util.c b/src/basic/format-util.c index 66551f7526..4231fa8d3f 100644 --- a/src/basic/format-util.c +++ b/src/basic/format-util.c @@ -32,7 +32,7 @@ char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) { { "K", UINT64_C(1000) }, }; const suffix_table *table; - size_t i; + size_t n, i; assert_cc(ELEMENTSOF(table_iec) == ELEMENTSOF(table_non_iec)); @@ -40,16 +40,19 @@ char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) { return NULL; table = flag & FORMAT_BYTES_USE_IEC ? table_iec : table_non_iec; + n = ELEMENTSOF(table_iec); - for (i = 0; i < ELEMENTSOF(table_iec); i++) + for (i = 0; i < n; i++) if (t >= table[i].factor) { - if (flag & FORMAT_BYTES_BELOW_POINT) + if (flag & FORMAT_BYTES_BELOW_POINT) { snprintf(buf, l, "%" PRIu64 ".%" PRIu64 "%s", t / table[i].factor, - ((t*UINT64_C(10)) / table[i].factor) % UINT64_C(10), + i != n - 1 ? + (t / table[i + 1].factor * UINT64_C(10) / table[n - 1].factor) % UINT64_C(10): + (t * UINT64_C(10) / table[i].factor) % UINT64_C(10), table[i].suffix); - else + } else snprintf(buf, l, "%" PRIu64 "%s", t / table[i].factor, |