diff options
-rw-r--r-- | lib/lib.c | 29 | ||||
-rw-r--r-- | lib/lib.h | 2 | ||||
-rw-r--r-- | toys/posix/ps.c | 4 |
3 files changed, 7 insertions, 28 deletions
@@ -1151,48 +1151,27 @@ match: int human_readable_long(char *buf, unsigned long long num, int dgt, int unit, int style) { - static char cc, dot; unsigned long long snap = 0; - int len, commas = 0, off, ii, divisor = (style&HR_1000) ? 1000 : 1024; + int len, divisor = (style&HR_1000) ? 1000 : 1024; // Divide rounding up until we have 3 or fewer digits. Since the part we // print is decimal, the test is 999 even when we divide by 1024. // The largest unit we can detect is 1<<64 = 18 Exabytes, but we added // Zettabyte and Yottabyte in case "unit" starts above zero. for (;;unit++) { - len = snprintf(0, 0, "%llu", num); - if (style&HR_COMMAS) commas = (len-1)/3; - if (len<=(dgt-commas)) break; + if ((len = snprintf(0, 0, "%llu", num))<=dgt) break; num = ((snap = num)+(divisor/2))/divisor; } if (CFG_TOYBOX_DEBUG && unit>8) return sprintf(buf, "%.*s", dgt, "TILT"); - if (!dot) { - if (CFG_TOYBOX_I18N) { - struct lconv *ll; - - setlocale(LC_NUMERIC, ""); - ll = localeconv(); - dot = *ll->decimal_point ? : '.'; - cc = *ll->thousands_sep ? : ','; - } else cc = ',', dot = '.'; - } - len = sprintf(buf, "%llu", num); - if (style&HR_COMMAS) { - for (ii = 0; ii<commas; ii++) { - off = len-3*(ii+1); - memmove(buf+off+commas-ii, buf+off, 3); - buf[off+commas-ii-1] = cc; - } - len += commas; - } else if (unit && len == 1) { + if (!(style & HR_NODOT) && unit && len == 1) { // Redo rounding for 1.2M case, this works with and without HR_1000. num = snap/divisor; snap -= num*divisor; snap = ((snap*100)+50)/divisor; snap /= 10; - len = sprintf(buf, "%llu%c%llu", num, dot, snap); + len = sprintf(buf, "%llu.%llu", num, snap); } if (style & HR_SPACE) buf[len++] = ' '; if (unit) { @@ -278,7 +278,7 @@ char *elf_arch_name(int type); #define HR_SPACE 1 // Space between number and units #define HR_B 2 // Use "B" for single byte units #define HR_1000 4 // Use decimal instead of binary units -#define HR_COMMAS 8 // Commas every 1000 +#define HR_NODOT 8 // No tenths for single digit units int human_readable_long(char *buf, unsigned long long num, int dgt, int unit, int style); int human_readable(char *buf, unsigned long long num, int style); diff --git a/toys/posix/ps.c b/toys/posix/ps.c index 6a7afda6..7013dc57 100644 --- a/toys/posix/ps.c +++ b/toys/posix/ps.c @@ -1604,9 +1604,9 @@ static void top_common( "\nBuffers:","\nSwapTotal:","\nSwapFree:","\nCached:"}[i]); run[i] = pos ? atol(pos) : 0; k = (*run>=10000000); - human_readable_long(hr[j+!!j], run[i]>>(10*k), 9, k+1, HR_COMMAS); + human_readable_long(hr[j+!!j], run[i]>>(10*k), 9, k+1, HR_NODOT); if (j==1) human_readable_long(hr[1], (run[i-1]-run[i])>>(10*k), - 8, k+1, HR_COMMAS); + 8, k+1, HR_NODOT); else if (j==2) { sprintf(toybuf, " %s:%10s total,%10s used,%10s free,%10s %s", (i<3) ? " Mem" : "Swap", hr[0], hr[1], hr[2], hr[3], |