Fix another x86 sys/ucontext.h namespace issue (bug 21457).
[glibc.git] / localedata / tst-setlocale3.c
blob6a3bd1aea856eb6a8a9efee035d890e7ce3974b8
1 /* Regression test for setlocale invalid environment variable handling.
2 Copyright (C) 2014-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <locale.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 /* The result of setlocale may be overwritten by subsequent calls, so
25 this wrapper makes a copy. */
26 static char *
27 setlocale_copy (int category, const char *locale)
29 const char *result = setlocale (category, locale);
30 if (result == NULL)
31 return NULL;
32 return strdup (result);
35 static char *de_locale;
37 static void
38 setlocale_fail (const char *envstring)
40 setenv ("LC_CTYPE", envstring, 1);
41 if (setlocale (LC_CTYPE, "") != NULL)
43 printf ("unexpected setlocale success for \"%s\" locale\n", envstring);
44 exit (1);
46 const char *newloc = setlocale (LC_CTYPE, NULL);
47 if (strcmp (newloc, de_locale) != 0)
49 printf ("failed setlocale call \"%s\" changed locale to \"%s\"\n",
50 envstring, newloc);
51 exit (1);
55 static void
56 setlocale_success (const char *envstring)
58 setenv ("LC_CTYPE", envstring, 1);
59 char *newloc = setlocale_copy (LC_CTYPE, "");
60 if (newloc == NULL)
62 printf ("setlocale for \"%s\": %m\n", envstring);
63 exit (1);
65 if (strcmp (newloc, de_locale) == 0)
67 printf ("setlocale with LC_CTYPE=\"%s\" left locale at \"%s\"\n",
68 envstring, de_locale);
69 exit (1);
71 if (setlocale (LC_CTYPE, de_locale) == NULL)
73 printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
74 de_locale, envstring);
75 exit (1);
77 char *newloc2 = setlocale_copy (LC_CTYPE, newloc);
78 if (newloc2 == NULL)
80 printf ("restoring locale \"%s\" following \"%s\": %m\n",
81 newloc, envstring);
82 exit (1);
84 if (strcmp (newloc, newloc2) != 0)
86 printf ("representation of locale \"%s\" changed from \"%s\" to \"%s\"",
87 envstring, newloc, newloc2);
88 exit (1);
90 free (newloc);
91 free (newloc2);
93 if (setlocale (LC_CTYPE, de_locale) == NULL)
95 printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
96 de_locale, envstring);
97 exit (1);
101 /* Checks that a known-good locale still works if LC_ALL contains a
102 value which should be ignored. */
103 static void
104 setlocale_ignore (const char *to_ignore)
106 const char *fr_locale = "fr_FR.UTF-8";
107 setenv ("LC_CTYPE", fr_locale, 1);
108 char *expected_locale = setlocale_copy (LC_CTYPE, "");
109 if (expected_locale == NULL)
111 printf ("setlocale with LC_CTYPE=\"%s\" failed: %m\n", fr_locale);
112 exit (1);
114 if (setlocale (LC_CTYPE, de_locale) == NULL)
116 printf ("failed to restore locale: %m\n");
117 exit (1);
119 unsetenv ("LC_CTYPE");
121 setenv ("LC_ALL", to_ignore, 1);
122 setenv ("LC_CTYPE", fr_locale, 1);
123 const char *actual_locale = setlocale (LC_CTYPE, "");
124 if (actual_locale == NULL)
126 printf ("setlocale with LC_ALL, LC_CTYPE=\"%s\" failed: %m\n",
127 fr_locale);
128 exit (1);
130 if (strcmp (actual_locale, expected_locale) != 0)
132 printf ("setlocale under LC_ALL failed: got \"%s\", expected \"%s\"\n",
133 actual_locale, expected_locale);
134 exit (1);
136 unsetenv ("LC_CTYPE");
137 setlocale_success (fr_locale);
138 unsetenv ("LC_ALL");
139 free (expected_locale);
142 static int
143 do_test (void)
145 /* The glibc test harness sets this environment variable
146 uncondionally. */
147 unsetenv ("LC_ALL");
149 de_locale = setlocale_copy (LC_CTYPE, "de_DE.UTF-8");
150 if (de_locale == NULL)
152 printf ("setlocale (LC_CTYPE, \"de_DE.UTF-8\"): %m\n");
153 return 1;
155 setlocale_success ("C");
156 setlocale_success ("en_US.UTF-8");
157 setlocale_success ("/en_US.UTF-8");
158 setlocale_success ("//en_US.UTF-8");
159 setlocale_ignore ("");
161 setlocale_fail ("does-not-exist");
162 setlocale_fail ("/");
163 setlocale_fail ("/../localedata/en_US.UTF-8");
164 setlocale_fail ("en_US.UTF-8/");
165 setlocale_fail ("en_US.UTF-8/..");
166 setlocale_fail ("en_US.UTF-8/../en_US.UTF-8");
167 setlocale_fail ("../localedata/en_US.UTF-8");
169 size_t large_length = 1024;
170 char *large_name = malloc (large_length + 1);
171 if (large_name == NULL)
173 puts ("malloc failure");
174 return 1;
176 memset (large_name, '/', large_length);
177 const char *suffix = "en_US.UTF-8";
178 strcpy (large_name + large_length - strlen (suffix), suffix);
179 setlocale_fail (large_name);
180 free (large_name);
183 size_t huge_length = 64 * 1024 * 1024;
184 char *huge_name = malloc (huge_length + 1);
185 if (huge_name == NULL)
187 puts ("malloc failure");
188 return 1;
190 memset (huge_name, 'X', huge_length);
191 huge_name[huge_length] = '\0';
192 /* Construct a composite locale specification. */
193 const char *prefix = "LC_CTYPE=de_DE.UTF-8;LC_TIME=";
194 memcpy (huge_name, prefix, strlen (prefix));
195 setlocale_fail (huge_name);
196 free (huge_name);
199 return 0;
202 #define TEST_FUNCTION do_test ()
203 #include "../test-skeleton.c"