aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2021-01-05 14:14:39 -0800
committerRob Landley <rob@landley.net>2021-01-06 00:09:44 -0600
commit4786fd610362501c2f8c70fef1d8fb66efd0996c (patch)
tree02b4661e3c749ad59cd4eeec0bbf5dc0fc1d188c
parent32eed40a14c1f550e366741f0a2e7aa69093dc3d (diff)
downloadtoybox-4786fd610362501c2f8c70fef1d8fb66efd0996c.tar.gz
main.c: construct a combination locale to add UTF-8.
We need <xlocale.h> for newlocale(3) on macOS, despite it being in POSIX 2008's <locale.h>. musl apparently doesn't have <xlocale.h>, so that's another trick to teach to portability.h...
-rw-r--r--lib/portability.h6
-rw-r--r--main.c8
2 files changed, 10 insertions, 4 deletions
diff --git a/lib/portability.h b/lib/portability.h
index 5886f63f..bbba12d8 100644
--- a/lib/portability.h
+++ b/lib/portability.h
@@ -208,12 +208,16 @@ ssize_t xattr_lset(const char*, const char*, const void*, size_t, int);
ssize_t xattr_fset(int, const char*, const void*, size_t, int);
#endif
-// macOS doesn't have these functions, but we can fake them.
#ifdef __APPLE__
+// macOS doesn't have these functions, but we can fake them.
int mknodat(int, const char*, mode_t, dev_t);
int posix_fallocate(int, off_t, off_t);
+
+// macOS keeps newlocale(3) in the non-POSIX <xlocale.h> rather than <locale.h>.
+#include <xlocale.h>
#endif
+
// Android is missing some headers and functions
// "generated/config.h" is included first
#if CFG_TOYBOX_SHADOW
diff --git a/main.c b/main.c
index 93822686..a7a8a694 100644
--- a/main.c
+++ b/main.c
@@ -99,11 +99,13 @@ void toy_singleinit(struct toy_list *which, char *argv[])
toys.old_umask = umask(0);
if (!(which->flags & TOYFLAG_UMASK)) umask(toys.old_umask);
- // Try user's locale, falling back to C.UTF-8 for Linux or UTF-8 for Mac.
- // (Neither locale name works on both OSes.)
+ // Try user's locale, but merge in the en_US.UTF-8 locale's character
+ // type data if the user's locale isn't UTF-8. (We can't merge in C.UTF-8
+ // because that locale doesn't exist on macOS.)
setlocale(LC_CTYPE, "");
if (strcmp("UTF-8", nl_langinfo(CODESET)))
- if (!setlocale(LC_CTYPE, "C.UTF-8")) setlocale(LC_CTYPE, "UTF-8");
+ uselocale(newlocale(LC_CTYPE_MASK, "en_US.UTF-8", NULL));
+
setvbuf(stdout, 0, (which->flags & TOYFLAG_LINEBUF) ? _IOLBF : _IONBF, 0);
}
}