1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-22 13:33:56 +03:00

util: make format_bytes() support e.g. 3.0E

This commit is contained in:
Yu Watanabe 2019-06-19 10:05:30 +09:00
parent aa89266900
commit b7de125bba

View File

@ -32,7 +32,7 @@ char *format_bytes_full(char *buf, size_t l, uint64_t t, FormatBytesFlag flag) {
{ "K", UINT64_C(1000) }, { "K", UINT64_C(1000) },
}; };
const suffix_table *table; const suffix_table *table;
size_t i; size_t n, i;
assert_cc(ELEMENTSOF(table_iec) == ELEMENTSOF(table_non_iec)); 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; return NULL;
table = flag & FORMAT_BYTES_USE_IEC ? table_iec : table_non_iec; 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 (t >= table[i].factor) {
if (flag & FORMAT_BYTES_BELOW_POINT) if (flag & FORMAT_BYTES_BELOW_POINT) {
snprintf(buf, l, snprintf(buf, l,
"%" PRIu64 ".%" PRIu64 "%s", "%" PRIu64 ".%" PRIu64 "%s",
t / table[i].factor, 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); table[i].suffix);
else } else
snprintf(buf, l, snprintf(buf, l,
"%" PRIu64 "%s", "%" PRIu64 "%s",
t / table[i].factor, t / table[i].factor,