diff options
-rw-r--r-- | utility.c | 52 |
1 files changed, 16 insertions, 36 deletions
@@ -1797,43 +1797,23 @@ ssize_t safe_read(int fd, void *buf, size_t count) | |||
1797 | #ifdef BB_FEATURE_HUMAN_READABLE | 1797 | #ifdef BB_FEATURE_HUMAN_READABLE |
1798 | const char *format(unsigned long val, unsigned long hr) | 1798 | const char *format(unsigned long val, unsigned long hr) |
1799 | { | 1799 | { |
1800 | static const char strings[] = { '0', 0, 'k', 0, 'M', 0, 'G', 0 }; | 1800 | int i=0; |
1801 | static const char fmt[] = "%lu"; | 1801 | static char str[10] = "\0"; |
1802 | static const char fmt_u[] = "%lu.%lu%s"; | 1802 | static const char strings[] = { 'k', 'M', 'G', 'T', 0 }; |
1803 | 1803 | unsigned long divisor = 1; | |
1804 | static char str[10]; | 1804 | |
1805 | 1805 | if(val == 0) | |
1806 | unsigned long frac __attribute__ ((unused)); /* 'may be uninitialized' warning is ok */ | 1806 | return("0"); |
1807 | const char *u; | 1807 | if(hr) |
1808 | const char *f; | 1808 | snprintf(str, 9, "%ld", val/hr); |
1809 | 1809 | else { | |
1810 | #if 1 | 1810 | while(val >= divisor && i <= 4) { |
1811 | if(val == 0) { /* This may be omitted to reduce size */ | 1811 | divisor=divisor<<10, i++; |
1812 | return strings; /* at the cost of speed. */ | 1812 | } |
1813 | } | 1813 | divisor=divisor>>10, i--; |
1814 | #endif | 1814 | snprintf(str, 9, "%.1Lf%c", (long double)(val)/divisor, strings[i]); |
1815 | |||
1816 | u = strings; | ||
1817 | f = fmt; | ||
1818 | if (hr) { | ||
1819 | val /= hr; | ||
1820 | } else { | ||
1821 | while ((val >= KILOBYTE) && (*u != 'G')) { | ||
1822 | f = fmt_u; | ||
1823 | u += 2; | ||
1824 | frac = (((val % KILOBYTE) * 10) + (KILOBYTE/2)) / KILOBYTE; | ||
1825 | val /= KILOBYTE; | ||
1826 | if (frac >= 10) { /* We need to round up here. */ | ||
1827 | ++val; | ||
1828 | frac = 0; | ||
1829 | } | ||
1830 | } | ||
1831 | } | 1815 | } |
1832 | 1816 | return(str); | |
1833 | /* If f==fmt then 'frac' and 'u' are ignored and need not be set. */ | ||
1834 | snprintf(str, sizeof(str), f, val, frac, u); | ||
1835 | |||
1836 | return str; | ||
1837 | } | 1817 | } |
1838 | #endif | 1818 | #endif |
1839 | 1819 | ||