diff mbox series

[iproute2-next,6/6] lib: print_rate(): Fix formatting small rates in IEC mode

Message ID 33d80a1bc452700952c56b21b1f4418632ba792d.1606774951.git.me@pmachata.org
State New
Headers show
Series Move rate and size parsing and output to lib | expand

Commit Message

Petr Machata Nov. 30, 2020, 10:59 p.m. UTC
ISO/IEC units are distinguished from the decadic ones by using a prefixes
like "Ki", "Mi" instead of "K" and "M". The current code inserts the letter
"i" after the decadic unit when in IEC mode. However it does so even when
the prefix is an empty string, formatting 1Kbit in IEC mode as "1000ibit".
Fix by omitting the letter if there is no prefix.

Signed-off-by: Petr Machata <me@pmachata.org>
---
 lib/json_print.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/lib/json_print.c b/lib/json_print.c
index c1df637642fd..625a98e35591 100644
--- a/lib/json_print.c
+++ b/lib/json_print.c
@@ -333,7 +333,8 @@  int print_color_rate(bool use_iec, enum output_type type, enum color_attr color,
 		rate /= kilo;
 	}
 
-	rc = asprintf(&buf, "%.0f%s%sbit", (double)rate, units[i], str);
+	rc = asprintf(&buf, "%.0f%s%sbit", (double)rate, units[i],
+		 i > 0 ? str : "");
 	if (rc < 0)
 		return -1;