| 1 |
/* Test of determination whether a locale is different from the "C" locale. |
| 2 |
Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation, either version 3 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
/* Written by Bruno Haible <[email protected]>, 2019. */ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include "hard-locale.h" |
| 22 |
|
| 23 |
#include <locale.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
/* True if all locale names are accepted and all locales are trivial. |
| 28 |
This is the case e.g. on OpenBSD 3.8. */ |
| 29 |
static bool all_trivial; |
| 30 |
|
| 31 |
static int |
| 32 |
test_one (const char *name, int failure_bitmask) |
| 33 |
{ |
| 34 |
if (setlocale (LC_ALL, name) != NULL) |
| 35 |
{ |
| 36 |
bool expected; |
| 37 |
|
| 38 |
/* musl libc has special code for the C.UTF-8 locale; other than that, |
| 39 |
all locale names are accepted and all locales are trivial. |
| 40 |
OpenBSD returns the locale name that was set, but we don't know how it |
| 41 |
behaves under the hood. Likewise for Haiku. |
| 42 |
On Android >= 5.0, the "C" locale may have UTF-8 encoding, and we don't |
| 43 |
know how it will behave in the future. */ |
| 44 |
#if defined MUSL_LIBC || defined __OpenBSD__ || defined __HAIKU__ || defined __ANDROID__ |
| 45 |
expected = true; |
| 46 |
#else |
| 47 |
expected = !all_trivial; |
| 48 |
#endif |
| 49 |
if (hard_locale (LC_CTYPE) != expected) |
| 50 |
{ |
| 51 |
if (expected) |
| 52 |
fprintf (stderr, "Unexpected: The category LC_CTYPE of the locale '%s' is not equivalent to C or POSIX.\n", |
| 53 |
name); |
| 54 |
else |
| 55 |
fprintf (stderr, "Unexpected: The category LC_CTYPE of the locale '%s' is equivalent to C or POSIX.\n", |
| 56 |
name); |
| 57 |
return failure_bitmask; |
| 58 |
} |
| 59 |
|
| 60 |
/* On NetBSD 7.0, some locales such as de_DE.ISO8859-1 and de_DE.UTF-8 |
| 61 |
have the LC_COLLATE category set to "C". |
| 62 |
Similarly, on musl libc, with the C.UTF-8 locale. |
| 63 |
On Android >= 5.0, the "C" locale may have UTF-8 encoding, and we don't |
| 64 |
know how it will behave in the future. */ |
| 65 |
#if defined __NetBSD__ |
| 66 |
expected = false; |
| 67 |
#elif defined MUSL_LIBC |
| 68 |
expected = strcmp (name, "C.UTF-8") != 0; |
| 69 |
#elif (defined __OpenBSD__ && HAVE_DUPLOCALE) || defined __HAIKU__ || defined __ANDROID__ /* OpenBSD >= 6.2, Haiku, Android */ |
| 70 |
expected = true; |
| 71 |
#else |
| 72 |
expected = !all_trivial; |
| 73 |
#endif |
| 74 |
if (hard_locale (LC_COLLATE) != expected) |
| 75 |
{ |
| 76 |
if (expected) |
| 77 |
fprintf (stderr, "Unexpected: The category LC_COLLATE of the locale '%s' is not equivalent to C or POSIX.\n", |
| 78 |
name); |
| 79 |
else |
| 80 |
fprintf (stderr, "Unexpected: The category LC_COLLATE of the locale '%s' is equivalent to C or POSIX.\n", |
| 81 |
name); |
| 82 |
return failure_bitmask; |
| 83 |
} |
| 84 |
} |
| 85 |
return 0; |
| 86 |
} |
| 87 |
|
| 88 |
int |
| 89 |
main () |
| 90 |
{ |
| 91 |
int fail = 0; |
| 92 |
|
| 93 |
/* The initial locale is the "C" or "POSIX" locale. |
| 94 |
On Android >= 5.0, it is equivalent to the "C.UTF-8" locale, cf. |
| 95 |
<https://lists.gnu.org/archive/html/bug-gnulib/2023-01/msg00141.html>. */ |
| 96 |
#if ! defined __ANDROID__ |
| 97 |
if (hard_locale (LC_CTYPE) || hard_locale (LC_COLLATE)) |
| 98 |
{ |
| 99 |
fprintf (stderr, "The initial locale should not be hard!\n"); |
| 100 |
fail |= 1; |
| 101 |
} |
| 102 |
#endif |
| 103 |
|
| 104 |
all_trivial = (setlocale (LC_ALL, "foobar") != NULL); |
| 105 |
|
| 106 |
fail |= test_one ("de", 2); |
| 107 |
fail |= test_one ("de_DE", 4); |
| 108 |
fail |= test_one ("de_DE.ISO8859-1", 8); |
| 109 |
fail |= test_one ("de_DE.iso88591", 8); |
| 110 |
fail |= test_one ("de_DE.UTF-8", 16); |
| 111 |
fail |= test_one ("de_DE.utf8", 16); |
| 112 |
fail |= test_one ("german", 32); |
| 113 |
fail |= test_one ("C.UTF-8", 64); |
| 114 |
|
| 115 |
return fail; |
| 116 |
} |