diff options
author | Rob Landley <rob@landley.net> | 2020-09-09 03:11:37 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2020-09-09 03:11:37 -0500 |
commit | 54965cadfe8f1aef663600cb07d3edbb0acf8769 (patch) | |
tree | 3854cd6fd7f13efcaca953bee4c5dc40ecbbc4b8 | |
parent | 6dedeb79511b4bbdcd2e1fbac10e2aa5d74930c5 (diff) | |
download | toybox-54965cadfe8f1aef663600cb07d3edbb0acf8769.tar.gz |
Attempt internationalization of HR_COMMAS, 9 digit memory sizes for top,
and use the comma format when selected even if <3 digits (no 0.0M)
-rw-r--r-- | lib/lib.c | 20 | ||||
-rw-r--r-- | toys/posix/ps.c | 4 |
2 files changed, 18 insertions, 6 deletions
@@ -1151,6 +1151,7 @@ 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; @@ -1160,18 +1161,29 @@ int human_readable_long(char *buf, unsigned long long num, int dgt, int unit, // Zettabyte and Yottabyte in case "unit" starts above zero. for (;;unit++) { len = snprintf(0, 0, "%llu", num); - if (style&HR_COMMAS) commas = (len>4)*((len-1)/3); + if (style&HR_COMMAS) commas = (len-1)/3; if (len<=(dgt-commas)) 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 (commas) { + 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] = ','; + buf[off+commas-ii-1] = cc; } len += commas; } else if (unit && len == 1) { @@ -1180,7 +1192,7 @@ int human_readable_long(char *buf, unsigned long long num, int dgt, int unit, snap -= num*divisor; snap = ((snap*100)+50)/divisor; snap /= 10; - len = sprintf(buf, "%llu.%llu", num, snap); + len = sprintf(buf, "%llu%c%llu", num, dot, snap); } if (style & HR_SPACE) buf[len++] = ' '; if (unit) { diff --git a/toys/posix/ps.c b/toys/posix/ps.c index cadc060b..6a7afda6 100644 --- a/toys/posix/ps.c +++ b/toys/posix/ps.c @@ -1604,11 +1604,11 @@ 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), 8, k+1, HR_COMMAS); + human_readable_long(hr[j+!!j], run[i]>>(10*k), 9, k+1, HR_COMMAS); if (j==1) human_readable_long(hr[1], (run[i-1]-run[i])>>(10*k), 8, k+1, HR_COMMAS); else if (j==2) { - sprintf(toybuf, " %s: %9s total, %9s used, %9s free, %9s %s", + sprintf(toybuf, " %s:%10s total,%10s used,%10s free,%10s %s", (i<3) ? " Mem" : "Swap", hr[0], hr[1], hr[2], hr[3], (i<3) ? "buffers" : "cached"); lines = header_line(lines, 0); |