X-Git-Url: https://git.kernel.dk/?p=fio.git;a=blobdiff_plain;f=lib%2Fnum2str.c;h=0ed05f33ea8039b52f5c18a7574ef78adb4caac6;hp=12d6f39aa5dbca892e9d03af9150aa28a3a47861;hb=29dbd1e5f1f7e3465acaa26a55cc29259f0eb61b;hpb=10aa136bddbaa7c845ab4eacb4a9a4a88d6657a3 diff --git a/lib/num2str.c b/lib/num2str.c index 12d6f39a..0ed05f33 100644 --- a/lib/num2str.c +++ b/lib/num2str.c @@ -4,10 +4,12 @@ #include "../fio.h" +#define ARRAY_LENGTH(arr) sizeof(arr) / sizeof((arr)[0]) + /* * Cheesy number->string conversion, complete with carry rounding error. */ -char *num2str(unsigned long num, int maxlen, int base, int pow2, int unit_base) +char *num2str(uint64_t num, int maxlen, int base, int pow2, int unit_base) { const char *postfix[] = { "", "K", "M", "G", "P", "E" }; const char *byte_postfix[] = { "", "B", "bit" }; @@ -34,7 +36,7 @@ char *num2str(unsigned long num, int maxlen, int base, int pow2, int unit_base) modulo = -1U; while (post_index < sizeof(postfix)) { - sprintf(tmp, "%lu", num); + sprintf(tmp, "%llu", (unsigned long long) num); if (strlen(tmp) <= maxlen) break; @@ -46,12 +48,15 @@ char *num2str(unsigned long num, int maxlen, int base, int pow2, int unit_base) if (modulo == -1U) { done: - sprintf(buf, "%lu%s%s", num, postfix[post_index], - byte_postfix[byte_post_index]); + if (post_index >= ARRAY_LENGTH(postfix)) + post_index = 0; + + sprintf(buf, "%llu%s%s", (unsigned long long) num, + postfix[post_index], byte_postfix[byte_post_index]); return buf; } - sprintf(tmp, "%lu", num); + sprintf(tmp, "%llu", (unsigned long long) num); decimals = maxlen - strlen(tmp); if (decimals <= 1) { if (carry) @@ -67,7 +72,7 @@ done: modulo = (modulo + 9) / 10; } while (1); - sprintf(buf, "%lu.%u%s%s", num, modulo, postfix[post_index], - byte_postfix[byte_post_index]); + sprintf(buf, "%llu.%u%s%s", (unsigned long long) num, modulo, + postfix[post_index], byte_postfix[byte_post_index]); return buf; }