Make 'grep -l' work
[oweals/busybox.git] / libbb / human_readable.c
index 1d7a90e55f925e7f6f7872ac5e5f65e88fd6e81c..ff2175175bb365f038a11f25a9de79d758b556a5 100644 (file)
 #include <stdio.h>
 #include "libbb.h"
 
-static char buffer[10];
-static const char *suffixes[] = { "", "k", "M", "G", "T" };
+
 
 const char *make_human_readable_str(unsigned long val, unsigned long hr)
 {
-       int suffix, base;
+       int i=0;
+       static char str[10] = "\0";
+       static const char strings[] = { 'k', 'M', 'G', 'T', 0 };
+       unsigned long divisor = 1;
 
-       for (suffix = 0, base = 1; suffix < 5; suffix++, base <<= 10) {
-               if (val < (base << 10)) {
-                       if (suffix && val < 10 * base)
-                               sprintf(buffer, "%lu.%lu%s", val / base,
-                                               (val % base) * 10 / base, suffixes[suffix]);
-                       else
-                               sprintf(buffer, "%lu%s", val / base, suffixes[suffix]);
-                       break;
-               }
+       if(val == 0)
+               return("0");
+       if(hr)
+               snprintf(str, 9, "%ld", val/hr);
+       else {
+               while(val >= divisor && i <= 4) {
+                       divisor=divisor<<10, i++;
+               } 
+               divisor=divisor>>10, i--;
+               snprintf(str, 9, "%.1Lf%c", (long double)(val)/divisor, strings[i]);
        }
-
-       return buffer;
+       return(str);
 }
+
+
+/* END CODE */
+/*
+Local Variables:
+c-file-style: "linux"
+c-basic-offset: 4
+tab-width: 4
+End:
+*/