| 1 | /* Test of determination whether a locale is different from the "C" locale. | 
|---|
| 2 | Copyright (C) 2019-2022 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 <bruno@clisp.org>, 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 | #if defined MUSL_LIBC || defined __OpenBSD__ || defined __HAIKU__ | 
|---|
| 43 | expected = true; | 
|---|
| 44 | #else | 
|---|
| 45 | expected = !all_trivial; | 
|---|
| 46 | #endif | 
|---|
| 47 | if (hard_locale (LC_CTYPE) != expected) | 
|---|
| 48 | { | 
|---|
| 49 | if (expected) | 
|---|
| 50 | fprintf (stderr, "Unexpected: The category LC_CTYPE of the locale '%s' is not equivalent to C or POSIX.\n", | 
|---|
| 51 | name); | 
|---|
| 52 | else | 
|---|
| 53 | fprintf (stderr, "Unexpected: The category LC_CTYPE of the locale '%s' is equivalent to C or POSIX.\n", | 
|---|
| 54 | name); | 
|---|
| 55 | return failure_bitmask; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | /* On NetBSD 7.0, some locales such as de_DE.ISO8859-1 and de_DE.UTF-8 | 
|---|
| 59 | have the LC_COLLATE category set to "C". | 
|---|
| 60 | Similarly, on musl libc, with the C.UTF-8 locale.  */ | 
|---|
| 61 | #if defined __NetBSD__ | 
|---|
| 62 | expected = false; | 
|---|
| 63 | #elif defined MUSL_LIBC | 
|---|
| 64 | expected = strcmp (name, "C.UTF-8") != 0; | 
|---|
| 65 | #elif (defined __OpenBSD__ && HAVE_DUPLOCALE) || defined __HAIKU__ /* OpenBSD >= 6.2, Haiku */ | 
|---|
| 66 | expected = true; | 
|---|
| 67 | #else | 
|---|
| 68 | expected = !all_trivial; | 
|---|
| 69 | #endif | 
|---|
| 70 | if (hard_locale (LC_COLLATE) != expected) | 
|---|
| 71 | { | 
|---|
| 72 | if (expected) | 
|---|
| 73 | fprintf (stderr, "Unexpected: The category LC_COLLATE of the locale '%s' is not equivalent to C or POSIX.\n", | 
|---|
| 74 | name); | 
|---|
| 75 | else | 
|---|
| 76 | fprintf (stderr, "Unexpected: The category LC_COLLATE of the locale '%s' is equivalent to C or POSIX.\n", | 
|---|
| 77 | name); | 
|---|
| 78 | return failure_bitmask; | 
|---|
| 79 | } | 
|---|
| 80 | } | 
|---|
| 81 | return 0; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | int | 
|---|
| 85 | main () | 
|---|
| 86 | { | 
|---|
| 87 | int fail = 0; | 
|---|
| 88 |  | 
|---|
| 89 | /* The initial locale is the "C" or "POSIX" locale.  */ | 
|---|
| 90 | if (hard_locale (LC_CTYPE) || hard_locale (LC_COLLATE)) | 
|---|
| 91 | { | 
|---|
| 92 | fprintf (stderr, "The initial locale should not be hard!\n"); | 
|---|
| 93 | fail |= 1; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | all_trivial = (setlocale (LC_ALL, "foobar") != NULL); | 
|---|
| 97 |  | 
|---|
| 98 | fail |= test_one ("de", 2); | 
|---|
| 99 | fail |= test_one ("de_DE", 4); | 
|---|
| 100 | fail |= test_one ("de_DE.ISO8859-1", 8); | 
|---|
| 101 | fail |= test_one ("de_DE.iso88591", 8); | 
|---|
| 102 | fail |= test_one ("de_DE.UTF-8", 16); | 
|---|
| 103 | fail |= test_one ("de_DE.utf8", 16); | 
|---|
| 104 | fail |= test_one ("german", 32); | 
|---|
| 105 | fail |= test_one ("C.UTF-8", 64); | 
|---|
| 106 |  | 
|---|
| 107 | return fail; | 
|---|
| 108 | } | 
|---|