]> git.ipfire.org Git - thirdparty/glibc.git/blame - intl/localealias.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / intl / localealias.c
CommitLineData
c84142e8 1/* Handle aliases for locale names.
04277e02 2 Copyright (C) 1995-2019 Free Software Foundation, Inc.
24906b43 3
6d248857
WN
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
0393dfd6 8
6d248857 9 This program is distributed in the hope that it will be useful,
c84142e8 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
6d248857
WN
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
24906b43 13
6d248857 14 You should have received a copy of the GNU Lesser General Public License
5a82c748 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
24906b43 16
17c389fc
UD
17/* Tell glibc's <string.h> to provide a prototype for mempcpy().
18 This must come before <config.h> because <config.h> may include
19 <features.h>, and once <features.h> has been included, it's too late. */
20#ifndef _GNU_SOURCE
21# define _GNU_SOURCE 1
22#endif
23
24906b43
RM
24#ifdef HAVE_CONFIG_H
25# include <config.h>
26#endif
27
28#include <ctype.h>
29#include <stdio.h>
2706ee38
UD
30#if defined _LIBC || defined HAVE___FSETLOCKING
31# include <stdio_ext.h>
32#endif
24906b43
RM
33#include <sys/types.h>
34
35#ifdef __GNUC__
0f283ffc 36# undef alloca
24906b43 37# define alloca __builtin_alloca
fa00327f 38# define HAVE_ALLOCA 1
24906b43 39#else
6d248857
WN
40# ifdef _MSC_VER
41# include <malloc.h>
42# define alloca _alloca
24906b43 43# else
6d248857
WN
44# if defined HAVE_ALLOCA_H || defined _LIBC
45# include <alloca.h>
24906b43 46# else
6d248857
WN
47# ifdef _AIX
48 #pragma alloca
49# else
50# ifndef alloca
24906b43 51char *alloca ();
6d248857 52# endif
24906b43
RM
53# endif
54# endif
55# endif
56#endif
57
0555fcce
UD
58#include <stdlib.h>
59#include <string.h>
24906b43 60
24906b43
RM
61#include "gettextP.h"
62
6d248857
WN
63#ifdef ENABLE_RELOCATABLE
64# include "relocatable.h"
65#else
66# define relocate(pathname) (pathname)
67#endif
68
24906b43
RM
69/* @@ end of prolog @@ */
70
71#ifdef _LIBC
72/* Rename the non ANSI C functions. This is required by the standard
73 because some ANSI C functions will require linking with this object
74 file and the name space must not be polluted. */
5f078c32 75# define strcasecmp(s1, s2) __strcasecmp_l (s1, s2, _nl_C_locobj_ptr)
40a55d20 76
e7c5513d
UD
77# ifndef mempcpy
78# define mempcpy __mempcpy
79# endif
1618c590 80# define HAVE_MEMPCPY 1
2706ee38 81# define HAVE___FSETLOCKING 1
6d248857 82#endif
1618c590 83
6d248857
WN
84/* Handle multi-threaded applications. */
85#ifdef _LIBC
ec999b8e 86# include <libc-lock.h>
6d248857
WN
87#else
88# include "lock.h"
24906b43
RM
89#endif
90
2706ee38
UD
91/* Some optimizations for glibc. */
92#ifdef _LIBC
7fc03cf3 93# define FEOF(fp) __feof_unlocked (fp)
cc67478e 94# define FGETS(buf, n, fp) __fgets_unlocked (buf, n, fp)
2706ee38
UD
95#else
96# define FEOF(fp) feof (fp)
97# define FGETS(buf, n, fp) fgets (buf, n, fp)
98#endif
99
4a4d50f3 100/* For those losing systems which don't have `alloca' we have to add
5f2eab42
RM
101 some additional code emulating it. */
102#ifdef HAVE_ALLOCA
4a4d50f3 103# define freea(p) /* nothing */
5f2eab42 104#else
4a4d50f3
UD
105# define alloca(n) malloc (n)
106# define freea(p) free (p)
107#endif
5f2eab42 108
6d248857 109#if defined _LIBC_REENTRANT || defined HAVE_DECL_FGETS_UNLOCKED
77ccaba1
UD
110# undef fgets
111# define fgets(buf, len, s) fgets_unlocked (buf, len, s)
112#endif
6d248857 113#if defined _LIBC_REENTRANT || defined HAVE_DECL_FEOF_UNLOCKED
77ccaba1
UD
114# undef feof
115# define feof(s) feof_unlocked (s)
116#endif
117
5f2eab42 118
6d248857
WN
119__libc_lock_define_initialized (static, lock)
120
121
24906b43
RM
122struct alias_map
123{
124 const char *alias;
125 const char *value;
126};
127
128
c877418f
RM
129#ifndef _LIBC
130# define libc_freeres_ptr(decl) decl
131#endif
132
133libc_freeres_ptr (static char *string_space);
390500b1
UD
134static size_t string_space_act;
135static size_t string_space_max;
c877418f 136libc_freeres_ptr (static struct alias_map *map);
390500b1
UD
137static size_t nmap;
138static size_t maxmap;
24906b43
RM
139
140
141/* Prototypes for local functions. */
d7ccc6c9 142static size_t read_alias_file (const char *fname, int fname_len);
6d248857
WN
143static int extend_alias_table (void);
144static int alias_compare (const struct alias_map *map1,
145 const struct alias_map *map2);
24906b43
RM
146
147
148const char *
6d248857 149_nl_expand_alias (const char *name)
24906b43 150{
6d248857 151 static const char *locale_alias_path;
24906b43 152 struct alias_map *retval;
40a55d20 153 const char *result = NULL;
24906b43
RM
154 size_t added;
155
40a55d20 156 __libc_lock_lock (lock);
6d248857
WN
157
158 if (locale_alias_path == NULL)
159 locale_alias_path = LOCALE_ALIAS_PATH;
40a55d20 160
24906b43
RM
161 do
162 {
163 struct alias_map item;
164
165 item.alias = name;
166
167 if (nmap > 0)
168 retval = (struct alias_map *) bsearch (&item, map, nmap,
169 sizeof (struct alias_map),
6d248857
WN
170 (int (*) (const void *,
171 const void *)
be10a868 172 ) alias_compare);
24906b43
RM
173 else
174 retval = NULL;
175
176 /* We really found an alias. Return the value. */
177 if (retval != NULL)
40a55d20
UD
178 {
179 result = retval->value;
180 break;
181 }
24906b43
RM
182
183 /* Perhaps we can find another alias file. */
184 added = 0;
185 while (added == 0 && locale_alias_path[0] != '\0')
186 {
187 const char *start;
188
6d248857 189 while (locale_alias_path[0] == PATH_SEPARATOR)
24906b43
RM
190 ++locale_alias_path;
191 start = locale_alias_path;
192
6d248857
WN
193 while (locale_alias_path[0] != '\0'
194 && locale_alias_path[0] != PATH_SEPARATOR)
24906b43
RM
195 ++locale_alias_path;
196
197 if (start < locale_alias_path)
198 added = read_alias_file (start, locale_alias_path - start);
199 }
200 }
201 while (added != 0);
202
40a55d20 203 __libc_lock_unlock (lock);
40a55d20
UD
204
205 return result;
24906b43
RM
206}
207
208
209static size_t
6d248857 210read_alias_file (const char *fname, int fname_len)
24906b43
RM
211{
212 FILE *fp;
213 char *full_fname;
214 size_t added;
86d2c878 215 static const char aliasfile[] = "/locale.alias";
24906b43 216
86d2c878 217 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
1618c590
UD
218#ifdef HAVE_MEMPCPY
219 mempcpy (mempcpy (full_fname, fname, fname_len),
220 aliasfile, sizeof aliasfile);
221#else
86d2c878
RM
222 memcpy (full_fname, fname, fname_len);
223 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
1618c590 224#endif
24906b43 225
6d248857 226#ifdef _LIBC
ee8449f7
UD
227 /* Note the file is opened with cancellation in the I/O functions
228 disabled. */
6d248857
WN
229 fp = fopen (relocate (full_fname), "rce");
230#else
231 fp = fopen (relocate (full_fname), "r");
232#endif
4a4d50f3 233 freea (full_fname);
24906b43 234 if (fp == NULL)
4a4d50f3 235 return 0;
24906b43 236
2706ee38
UD
237#ifdef HAVE___FSETLOCKING
238 /* No threads present. */
239 __fsetlocking (fp, FSETLOCKING_BYCALLER);
240#endif
241
24906b43 242 added = 0;
2706ee38 243 while (!FEOF (fp))
24906b43
RM
244 {
245 /* It is a reasonable approach to use a fix buffer here because
246 a) we are only interested in the first two fields
247 b) these fields must be usable as file names and so must not
248 be that long
51b3c8f6
UD
249 We avoid a multi-kilobyte buffer here since this would use up
250 stack space which we might not have if the program ran out of
251 memory. */
252 char buf[400];
8cb569b7
UD
253 char *alias;
254 char *value;
255 char *cp;
fae49c62 256 int complete_line;
24906b43 257
2706ee38 258 if (FGETS (buf, sizeof buf, fp) == NULL)
24906b43
RM
259 /* EOF reached. */
260 break;
261
fae49c62
UD
262 /* Determine whether the line is complete. */
263 complete_line = strchr (buf, '\n') != NULL;
264
24906b43
RM
265 cp = buf;
266 /* Ignore leading white space. */
0555fcce 267 while (isspace ((unsigned char) cp[0]))
24906b43
RM
268 ++cp;
269
270 /* A leading '#' signals a comment line. */
271 if (cp[0] != '\0' && cp[0] != '#')
272 {
273 alias = cp++;
0555fcce 274 while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
24906b43
RM
275 ++cp;
276 /* Terminate alias name. */
277 if (cp[0] != '\0')
278 *cp++ = '\0';
279
280 /* Now look for the beginning of the value. */
0555fcce 281 while (isspace ((unsigned char) cp[0]))
24906b43
RM
282 ++cp;
283
284 if (cp[0] != '\0')
285 {
24906b43 286 value = cp++;
0555fcce 287 while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
24906b43
RM
288 ++cp;
289 /* Terminate value. */
290 if (cp[0] == '\n')
291 {
292 /* This has to be done to make the following test
293 for the end of line possible. We are looking for
294 the terminating '\n' which do not overwrite here. */
295 *cp++ = '\0';
296 *cp = '\n';
297 }
298 else if (cp[0] != '\0')
299 *cp++ = '\0';
300
6d248857
WN
301#ifdef IN_LIBGLOCALE
302 /* glibc's locale.alias contains entries for ja_JP and ko_KR
303 that make it impossible to use a Japanese or Korean UTF-8
304 locale under the name "ja_JP" or "ko_KR". Ignore these
305 entries. */
306 if (strchr (alias, '_') == NULL)
307#endif
308 {
309 size_t alias_len;
310 size_t value_len;
24906b43 311
6d248857
WN
312 if (nmap >= maxmap)
313 if (__builtin_expect (extend_alias_table (), 0))
314 goto out;
24906b43 315
6d248857
WN
316 alias_len = strlen (alias) + 1;
317 value_len = strlen (value) + 1;
42be70d4 318
6d248857
WN
319 if (string_space_act + alias_len + value_len > string_space_max)
320 {
321 /* Increase size of memory pool. */
322 size_t new_size = (string_space_max
323 + (alias_len + value_len > 1024
324 ? alias_len + value_len : 1024));
325 char *new_pool = (char *) realloc (string_space, new_size);
326 if (new_pool == NULL)
327 goto out;
328
329 if (__builtin_expect (string_space != new_pool, 0))
42be70d4 330 {
6d248857
WN
331 size_t i;
332
333 for (i = 0; i < nmap; i++)
334 {
335 map[i].alias += new_pool - string_space;
336 map[i].value += new_pool - string_space;
337 }
42be70d4 338 }
42be70d4 339
6d248857
WN
340 string_space = new_pool;
341 string_space_max = new_size;
342 }
a5a0310d 343
6d248857
WN
344 map[nmap].alias =
345 (const char *) memcpy (&string_space[string_space_act],
346 alias, alias_len);
347 string_space_act += alias_len;
a5a0310d 348
6d248857
WN
349 map[nmap].value =
350 (const char *) memcpy (&string_space[string_space_act],
351 value, value_len);
352 string_space_act += value_len;
24906b43 353
6d248857
WN
354 ++nmap;
355 ++added;
356 }
24906b43
RM
357 }
358 }
51b3c8f6
UD
359
360 /* Possibly not the whole line fits into the buffer. Ignore
361 the rest of the line. */
fae49c62
UD
362 if (! complete_line)
363 do
364 if (FGETS (buf, sizeof buf, fp) == NULL)
365 /* Make sure the inner loop will be left. The outer loop
366 will exit at the `feof' test. */
367 break;
f6c93bd9 368 while (strchr (buf, '\n') == NULL);
24906b43
RM
369 }
370
6d248857 371 out:
24906b43
RM
372 /* Should we test for ferror()? I think we have to silently ignore
373 errors. --drepper */
374 fclose (fp);
375
376 if (added > 0)
377 qsort (map, nmap, sizeof (struct alias_map),
6d248857 378 (int (*) (const void *, const void *)) alias_compare);
24906b43
RM
379
380 return added;
381}
382
383
17c389fc 384static int
60d2f8f3 385extend_alias_table (void)
24906b43
RM
386{
387 size_t new_size;
388 struct alias_map *new_map;
389
390 new_size = maxmap == 0 ? 100 : 2 * maxmap;
a5a0310d
UD
391 new_map = (struct alias_map *) realloc (map, (new_size
392 * sizeof (struct alias_map)));
24906b43
RM
393 if (new_map == NULL)
394 /* Simply don't extend: we don't have any more core. */
17c389fc 395 return -1;
24906b43 396
24906b43
RM
397 map = new_map;
398 maxmap = new_size;
17c389fc 399 return 0;
24906b43
RM
400}
401
402
403static int
6d248857 404alias_compare (const struct alias_map *map1, const struct alias_map *map2)
24906b43
RM
405{
406#if defined _LIBC || defined HAVE_STRCASECMP
407 return strcasecmp (map1->alias, map2->alias);
408#else
409 const unsigned char *p1 = (const unsigned char *) map1->alias;
410 const unsigned char *p2 = (const unsigned char *) map2->alias;
411 unsigned char c1, c2;
412
413 if (p1 == p2)
414 return 0;
415
416 do
417 {
418 /* I know this seems to be odd but the tolower() function in
419 some systems libc cannot handle nonalpha characters. */
75914335
RM
420 c1 = isupper (*p1) ? tolower (*p1) : *p1;
421 c2 = isupper (*p2) ? tolower (*p2) : *p2;
24906b43
RM
422 if (c1 == '\0')
423 break;
be10a868
RM
424 ++p1;
425 ++p2;
24906b43
RM
426 }
427 while (c1 == c2);
428
429 return c1 - c2;
430#endif
431}