Cleanup symbols that should be static
[fio.git] / lib / num2str.c
index 559cbeb590e4926b2d34f3b34cf6ac4cc0eadf10..12d6f39aa5dbca892e9d03af9150aa28a3a47861 100644 (file)
@@ -2,15 +2,18 @@
 #include <stdio.h>
 #include <string.h>
 
+#include "../fio.h"
+
 /*
  * Cheesy number->string conversion, complete with carry rounding error.
  */
-char *num2str(unsigned long num, int maxlen, int base, int pow2)
+char *num2str(unsigned long num, int maxlen, int base, int pow2, int unit_base)
 {
-       char postfix[] = { ' ', 'K', 'M', 'G', 'P', 'E' };
-       unsigned int thousand[] = { 1000, 1024 };
+       const char *postfix[] = { "", "K", "M", "G", "P", "E" };
+       const char *byte_postfix[] = { "", "B", "bit" };
+       const unsigned int thousand[] = { 1000, 1024 };
        unsigned int modulo, decimals;
-       int post_index, carry = 0;
+       int byte_post_index = 0, post_index, carry = 0;
        char tmp[32];
        char *buf;
 
@@ -19,6 +22,16 @@ char *num2str(unsigned long num, int maxlen, int base, int pow2)
        for (post_index = 0; base > 1; post_index++)
                base /= thousand[!!pow2];
 
+       switch (unit_base) {
+       case 1:
+               byte_post_index = 2;
+               num *= 8;
+               break;
+       case 8:
+               byte_post_index = 1;
+               break;
+       }
+
        modulo = -1U;
        while (post_index < sizeof(postfix)) {
                sprintf(tmp, "%lu", num);
@@ -33,7 +46,8 @@ char *num2str(unsigned long num, int maxlen, int base, int pow2)
 
        if (modulo == -1U) {
 done:
-               sprintf(buf, "%lu%c", num, postfix[post_index]);
+               sprintf(buf, "%lu%s%s", num, postfix[post_index],
+                       byte_postfix[byte_post_index]);
                return buf;
        }
 
@@ -53,6 +67,7 @@ done:
                modulo = (modulo + 9) / 10;
        } while (1);
 
-       sprintf(buf, "%lu.%u%c", num, modulo, postfix[post_index]);
+       sprintf(buf, "%lu.%u%s%s", num, modulo, postfix[post_index],
+               byte_postfix[byte_post_index]);
        return buf;
 }