aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2020-09-12 07:46:44 -0500
committerRob Landley <rob@landley.net>2020-09-12 07:46:44 -0500
commit5b7cc6d6c2a549188ce652d44b2583c01fb48188 (patch)
treeff8b6c96e311b3b27809eb66b758f288d47b5342 /lib
parent2450930d8bc51f961ac1d2b92cccecd8a8e64a1c (diff)
downloadtoybox-5b7cc6d6c2a549188ce652d44b2583c01fb48188.tar.gz
Replace HR_COMMAS with HR_NODOT
The comma thing turned into an internationalization can of worms, don't go there. Keep the "show megabytes on systems with >10G" logic which includes not showing 0.0 for single digit values.
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.c29
-rw-r--r--lib/lib.h2
2 files changed, 5 insertions, 26 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 319b6af4..752fd0a1 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -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) {
diff --git a/lib/lib.h b/lib/lib.h
index c793ba3f..db851631 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -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);