1 /* Test of determination whether a locale is different from the "C" locale.
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2019. */
21 #include "hard-locale.h"
28 /* True if all locale names are accepted and all locales are trivial.
29 This is the case e.g. on OpenBSD 3.8. */
30 static bool all_trivial
;
33 test_one (const char *name
, int failure_bitmask
)
35 if (setlocale (LC_ALL
, name
) != NULL
)
39 /* musl libc has special code for the C.UTF-8 locale; other than that,
40 all locale names are accepted and all locales are trivial.
41 OpenBSD returns the locale name that was set, but we don't know how it
42 behaves under the hood. Likewise for Haiku. */
43 #if defined MUSL_LIBC || defined __OpenBSD__ || defined __HAIKU__
46 expected
= !all_trivial
;
48 if (hard_locale (LC_CTYPE
) != expected
)
51 fprintf (stderr
, "Unexpected: The category LC_CTYPE of the locale '%s' is not equivalent to C or POSIX.\n",
54 fprintf (stderr
, "Unexpected: The category LC_CTYPE of the locale '%s' is equivalent to C or POSIX.\n",
56 return failure_bitmask
;
59 /* On NetBSD 7.0, some locales such as de_DE.ISO8859-1 and de_DE.UTF-8
60 have the LC_COLLATE category set to "C".
61 Similarly, on musl libc, with the C.UTF-8 locale. */
62 #if defined __NetBSD__
64 #elif defined MUSL_LIBC
65 expected
= strcmp (name
, "C.UTF-8") != 0;
66 #elif (defined __OpenBSD__ && HAVE_DUPLOCALE) || defined __HAIKU__ /* OpenBSD >= 6.2, Haiku */
69 expected
= !all_trivial
;
71 if (hard_locale (LC_COLLATE
) != expected
)
74 fprintf (stderr
, "Unexpected: The category LC_COLLATE of the locale '%s' is not equivalent to C or POSIX.\n",
77 fprintf (stderr
, "Unexpected: The category LC_COLLATE of the locale '%s' is equivalent to C or POSIX.\n",
79 return failure_bitmask
;
90 /* The initial locale is the "C" or "POSIX" locale. */
91 if (hard_locale (LC_CTYPE
) || hard_locale (LC_COLLATE
))
93 fprintf (stderr
, "The initial locale should not be hard!\n");
97 all_trivial
= (setlocale (LC_ALL
, "foobar") != NULL
);
99 fail
|= test_one ("de", 2);
100 fail
|= test_one ("de_DE", 4);
101 fail
|= test_one ("de_DE.ISO8859-1", 8);
102 fail
|= test_one ("de_DE.iso88591", 8);
103 fail
|= test_one ("de_DE.UTF-8", 16);
104 fail
|= test_one ("de_DE.utf8", 16);
105 fail
|= test_one ("german", 32);
106 fail
|= test_one ("C.UTF-8", 64);