1 /* Test of determination whether a locale is different from the "C" locale.
2 Copyright (C) 2019-2021 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 <stdbool.h>
25 #include <stdio.h>
26 #include <string.h>
27
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;
31
32 static int
test_one(const char * name,int failure_bitmask)33 test_one (const char *name, int failure_bitmask)
34 {
35 if (setlocale (LC_ALL, name) != NULL)
36 {
37 bool expected;
38
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__
44 expected = true;
45 #else
46 expected = !all_trivial;
47 #endif
48 if (hard_locale (LC_CTYPE) != expected)
49 {
50 if (expected)
51 fprintf (stderr, "Unexpected: The category LC_CTYPE of the locale '%s' is not equivalent to C or POSIX.\n",
52 name);
53 else
54 fprintf (stderr, "Unexpected: The category LC_CTYPE of the locale '%s' is equivalent to C or POSIX.\n",
55 name);
56 return failure_bitmask;
57 }
58
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__
63 expected = false;
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 */
67 expected = true;
68 #else
69 expected = !all_trivial;
70 #endif
71 if (hard_locale (LC_COLLATE) != expected)
72 {
73 if (expected)
74 fprintf (stderr, "Unexpected: The category LC_COLLATE of the locale '%s' is not equivalent to C or POSIX.\n",
75 name);
76 else
77 fprintf (stderr, "Unexpected: The category LC_COLLATE of the locale '%s' is equivalent to C or POSIX.\n",
78 name);
79 return failure_bitmask;
80 }
81 }
82 return 0;
83 }
84
85 int
main()86 main ()
87 {
88 int fail = 0;
89
90 /* The initial locale is the "C" or "POSIX" locale. */
91 if (hard_locale (LC_CTYPE) || hard_locale (LC_COLLATE))
92 {
93 fprintf (stderr, "The initial locale should not be hard!\n");
94 fail |= 1;
95 }
96
97 all_trivial = (setlocale (LC_ALL, "foobar") != NULL);
98
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);
107
108 return fail;
109 }
110