From: Bruno Haible Date: Sat, 12 Feb 2011 17:16:10 +0000 (+0100) Subject: setlocale: Workaround native Windows bug. X-Git-Tag: v0.1~3190 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=94c9eb9cdefca1ca06c5159be5ed52491e27535b;p=gnulib.git setlocale: Workaround native Windows bug. * lib/setlocale.c (rpl_setlocale): On native Windows, when setlocale succeeds but sets LC_CTYPE to "C", report a failure. * tests/test-setlocale2.sh: New file. * tests/test-setlocale2.c: New file. * modules/setlocale-tests (Files): Add the new files. (Makefile.am): Enable test-setlocale2.sh test. * doc/posix-functions/setlocale.texi: Mention workaround. --- diff --git a/ChangeLog b/ChangeLog index b57fbddc3..05f5e9e04 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2011-02-11 Bruno Haible + setlocale: Workaround native Windows bug. + * lib/setlocale.c (rpl_setlocale): On native Windows, when setlocale + succeeds but sets LC_CTYPE to "C", report a failure. + * tests/test-setlocale2.sh: New file. + * tests/test-setlocale2.c: New file. + * modules/setlocale-tests (Files): Add the new files. + (Makefile.am): Enable test-setlocale2.sh test. + * doc/posix-functions/setlocale.texi: Mention workaround. + +2011-02-11 Bruno Haible + Tests for module 'setlocale'. * modules/setlocale-tests: New file. * tests/test-setlocale1.sh: New file. diff --git a/doc/posix-functions/setlocale.texi b/doc/posix-functions/setlocale.texi index 6e64584cf..c54018e15 100644 --- a/doc/posix-functions/setlocale.texi +++ b/doc/posix-functions/setlocale.texi @@ -13,6 +13,10 @@ On Windows platforms (excluding Cygwin), @code{setlocale(@var{category},NULL)} ignores the environment variables @code{LC_ALL}, @code{@var{category}}, and @code{LANG}. @item +On Windows platforms (excluding Cygwin), @code{setlocale(LC_ALL,@var{name})} +succeeds and sets the LC_CTYPE category to @samp{C} when it does not support +the encoding, instead of failing. +@item On Windows platforms (excluding Cygwin), @code{setlocale} understands different locale names, that are not based on ISO 639 language names and ISO 3166 country names. @@ -25,8 +29,4 @@ On Cygwin 1.5.x, which doesn't have locales, @code{setlocale(LC_ALL,NULL)} always returns @code{"C"}. @item On Cygwin 1.7.0, only the charset portion of a locale designation is honored. -@item -On Windows platforms (excluding Cygwin), @code{setlocale(LC_ALL,@var{name})} -succeeds and sets the LC_CTYPE category to @samp{C} when it does not support -the encoding, instead of failing. @end itemize diff --git a/lib/setlocale.c b/lib/setlocale.c index 9fe1aa9fa..0cef00fa6 100644 --- a/lib/setlocale.c +++ b/lib/setlocale.c @@ -844,6 +844,14 @@ rpl_setlocale (int category, const char *locale) if (setlocale_unixlike (LC_ALL, base_name) == NULL) goto fail; +# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + /* On native Windows, setlocale(LC_ALL,...) may succeed but set the + LC_CTYPE category to an invalid value ("C") when it does not + support the specified encoding. Report a failure instead. */ + if (strchr (base_name, '.') != NULL + && strcmp (setlocale (LC_CTYPE, NULL), "C") == 0) + goto fail; +# endif for (i = 0; i < sizeof (categories) / sizeof (categories[0]); i++) { @@ -886,7 +894,45 @@ rpl_setlocale (int category, const char *locale) } } else - return setlocale_single (category, locale); + { +# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ + if (category == LC_ALL && locale != NULL && strchr (locale, '.') != NULL) + { + char *saved_locale; + + /* Back up the old locale. */ + saved_locale = setlocale (LC_ALL, NULL); + if (saved_locale == NULL) + return NULL; + saved_locale = strdup (saved_locale); + if (saved_locale == NULL) + return NULL; + + if (setlocale_unixlike (LC_ALL, locale) == NULL) + { + free (saved_locale); + return NULL; + } + + /* On native Windows, setlocale(LC_ALL,...) may succeed but set the + LC_CTYPE category to an invalid value ("C") when it does not + support the specified encoding. Report a failure instead. */ + if (strcmp (setlocale (LC_CTYPE, NULL), "C") == 0) + { + if (saved_locale[0] != '\0') /* don't risk an endless recursion */ + setlocale (LC_ALL, saved_locale); + free (saved_locale); + return NULL; + } + + /* It was really successful. */ + free (saved_locale); + return setlocale (LC_ALL, NULL); + } + else +# endif + return setlocale_single (category, locale); + } } #endif diff --git a/modules/setlocale-tests b/modules/setlocale-tests index e326fdcc5..8c35c9d82 100644 --- a/modules/setlocale-tests +++ b/modules/setlocale-tests @@ -1,6 +1,8 @@ Files: tests/test-setlocale1.sh tests/test-setlocale1.c +tests/test-setlocale2.sh +tests/test-setlocale2.c tests/signature.h tests/macros.h m4/locale-fr.m4 @@ -18,10 +20,10 @@ gt_LOCALE_JA gt_LOCALE_ZH_CN Makefile.am: -TESTS += test-setlocale1.sh +TESTS += test-setlocale1.sh test-setlocale2.sh TESTS_ENVIRONMENT += \ LOCALE_FR='@LOCALE_FR@' \ LOCALE_FR_UTF8='@LOCALE_FR_UTF8@' \ LOCALE_JA='@LOCALE_JA@' \ LOCALE_ZH_CN='@LOCALE_ZH_CN@' -check_PROGRAMS += test-setlocale1 +check_PROGRAMS += test-setlocale1 test-setlocale2 diff --git a/tests/test-setlocale2.c b/tests/test-setlocale2.c new file mode 100644 index 000000000..7bfc42abe --- /dev/null +++ b/tests/test-setlocale2.c @@ -0,0 +1,55 @@ +/* Test of setting the current locale. + Copyright (C) 2011 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +#include + +#include +#include +#include + +int +main () +{ + /* Try to set the locale by implicitly looking at the LC_ALL environment + variable. */ + if (setlocale (LC_ALL, "") != NULL) + /* It was successful. Check whether LC_CTYPE is non-trivial. */ + if (strcmp (setlocale (LC_CTYPE, NULL), "C") == 0) + { + fprintf (stderr, "setlocale did not fail for implicit %s\n", + getenv ("LC_ALL")); + return 1; + } + + /* Reset the locale. */ + if (setlocale (LC_ALL, "C") == NULL) + return 1; + + /* Try to set the locale by explicitly looking at the LC_ALL environment + variable. */ + if (setlocale (LC_ALL, getenv ("LC_ALL")) != NULL) + /* It was successful. Check whether LC_CTYPE is non-trivial. */ + if (strcmp (setlocale (LC_CTYPE, NULL), "C") == 0) + { + fprintf (stderr, "setlocale did not fail for explicit %s\n", + getenv ("LC_ALL")); + return 1; + } + + return 0; +} diff --git a/tests/test-setlocale2.sh b/tests/test-setlocale2.sh new file mode 100755 index 000000000..723e74e76 --- /dev/null +++ b/tests/test-setlocale2.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +# Test locale names with likely unsupported encoding in Unix syntax. +for name in ar_SA.ISO-8859-1 fr_FR.CP1251 zh_TW.GB18030 zh_CN.BIG5; do + LC_ALL=$name ./test-setlocale2${EXEEXT} 1 || exit 1 +done + +# Test locale names with likely unsupported encoding in native Windows syntax. +for name in "Arabic_Saudi Arabia.1252" "Arabic_Saudi Arabia.65001" \ + French_France.65001 Japanese_Japan.65001 Turkish_Turkey.65001 \ + Chinese_Taiwan.65001 Chinese_China.54936 Chinese_China.65001; do + LC_ALL=$name ./test-setlocale2${EXEEXT} 1 || exit 1 +done + +exit 0