[mw-devel] MW3 r1383 - trunk/src
arthur at sucs.org
arthur at sucs.org
Thu Jul 30 15:44:29 BST 2015
Author: arthur
Date: 2015-07-30 15:44:29 +0100 (Thu, 30 Jul 2015)
New Revision: 1383
Modified:
trunk/src/iconv.c
Log:
Attempt to fixup locale when it doesnt give a charset
Modified: trunk/src/iconv.c
===================================================================
--- trunk/src/iconv.c 2015-07-17 16:26:10 UTC (rev 1382)
+++ trunk/src/iconv.c 2015-07-30 14:44:29 UTC (rev 1383)
@@ -295,14 +295,29 @@
int set_locale(const char *locale)
{
const char *set = strchr(locale, '.');
- char * oldset;
- if (!set || !strcmp(".", set)) { /* no charset? or "C" locale? */
- if (!strcmp(locale, "C"))
+ char * oldset = NULL;
+ const char * lcc = locale;
+
+ if (set==NULL || strcmp(".", set)==0) { /* no charset? */
+
+ /* C Locale is just ASCII */
+ if (strcmp(locale, "C")==0) {
set = "ASCII";
- else
- return 1;
+ } else {
+ // locale without charset, default to utf-8
+ char * newlc = alloca(strlen(locale)+6+1);
+ strcpy(newlc, locale);
+ if (set == NULL) {
+ strcat(newlc, ".");
+ }
+ strcat(newlc, "UTF-8");
+ lcc = newlc;
+
+ set = "UTF-8";
+ }
} else
set++;
+
/* dunno what strdup(NULL) does so be conservative */
oldset = get_local_charset();
if (oldset) oldset = strdup(oldset);
@@ -311,8 +326,9 @@
return 2;
}
- if (setlocale(LC_ALL, locale) == NULL) {
+ if (setlocale(LC_ALL, lcc) == NULL) {
if (oldset) set_local_charset(oldset);
+ printf("Set locale '%s' failed, resetting charset=%s\n", lcc, oldset);
return 3;
}
return 0;
@@ -337,13 +353,15 @@
void init_locale(void)
{
- char * locale;
+ char * locale = NULL;
+ char * test;
if (local_charset) return; /* already init'd */
- if ( (!(locale = getenv("LC_CTYPE")) &&
- !(locale = getenv("LC_ALL")) &&
- !(locale = getenv("LANG"))) ||
- !(*locale))
+ if ((test=getenv("LC_CTYPE"))!=NULL) locale = test;
+ if ((test=getenv("LC_ALL"))!=NULL) locale = test;
+ if ((test=getenv("LANG"))!=NULL) locale = test;
+
+ if (locale == NULL)
/* fall back to ASCII */
set_local_charset("ASCII");
else
More information about the mw-devel
mailing list