- if (TERMENC_ASCII != enc) {
- v = TERMENC_LOCALE == enc ?
- setlocale(LC_ALL, "") :
- setlocale(LC_CTYPE, "en_US.UTF-8");
- if (NULL != v && MB_CUR_MAX > 1) {
- p->enc = enc;
+ if (enc != TERMENC_ASCII) {
+
+ /*
+ * Do not change any of this to LC_ALL. It might break
+ * the formatting by subtly changing the behaviour of
+ * various functions, for example strftime(3). As a
+ * worst case, it might even cause buffer overflows.
+ */
+
+ v = enc == TERMENC_LOCALE ?
+ setlocale(LC_CTYPE, "") :
+ setlocale(LC_CTYPE, UTF8_LOCALE);
+
+ /*
+ * We only support UTF-8,
+ * so revert to ASCII for anything else.
+ */
+
+ if (v != NULL &&
+ strcmp(nl_langinfo(CODESET), "UTF-8") != 0)
+ v = setlocale(LC_CTYPE, "C");
+
+ if (v != NULL && MB_CUR_MAX > 1) {
+ p->enc = TERMENC_UTF8;