1 /* Handle aliases for locale names.
2 Copyright (C) 1995-2018 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 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.
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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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. */
21 # define _GNU_SOURCE 1
30 #if defined _LIBC || defined HAVE___FSETLOCKING
31 # include <stdio_ext.h>
33 #include <sys/types.h>
37 # define alloca __builtin_alloca
38 # define HAVE_ALLOCA 1
42 # define alloca _alloca
44 # if defined HAVE_ALLOCA_H || defined _LIBC
63 #ifdef ENABLE_RELOCATABLE
64 # include "relocatable.h"
66 # define relocate(pathname) (pathname)
69 /* @@ end of prolog @@ */
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. */
75 # define strcasecmp(s1, s2) __strcasecmp_l (s1, s2, _nl_C_locobj_ptr)
78 # define mempcpy __mempcpy
80 # define HAVE_MEMPCPY 1
81 # define HAVE___FSETLOCKING 1
84 /* Handle multi-threaded applications. */
86 # include <libc-lock.h>
91 /* Some optimizations for glibc. */
93 # define FEOF(fp) __feof_unlocked (fp)
94 # define FGETS(buf, n, fp) __fgets_unlocked (buf, n, fp)
96 # define FEOF(fp) feof (fp)
97 # define FGETS(buf, n, fp) fgets (buf, n, fp)
100 /* For those losing systems which don't have `alloca' we have to add
101 some additional code emulating it. */
103 # define freea(p) /* nothing */
105 # define alloca(n) malloc (n)
106 # define freea(p) free (p)
109 #if defined _LIBC_REENTRANT || defined HAVE_DECL_FGETS_UNLOCKED
111 # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
113 #if defined _LIBC_REENTRANT || defined HAVE_DECL_FEOF_UNLOCKED
115 # define feof(s) feof_unlocked (s)
119 __libc_lock_define_initialized (static, lock
)
130 # define libc_freeres_ptr(decl) decl
133 libc_freeres_ptr (static char *string_space
);
134 static size_t string_space_act
;
135 static size_t string_space_max
;
136 libc_freeres_ptr (static struct alias_map
*map
);
138 static size_t maxmap
;
141 /* Prototypes for local functions. */
142 static size_t read_alias_file (const char *fname
, int fname_len
);
143 static int extend_alias_table (void);
144 static int alias_compare (const struct alias_map
*map1
,
145 const struct alias_map
*map2
);
149 _nl_expand_alias (const char *name
)
151 static const char *locale_alias_path
;
152 struct alias_map
*retval
;
153 const char *result
= NULL
;
156 __libc_lock_lock (lock
);
158 if (locale_alias_path
== NULL
)
159 locale_alias_path
= LOCALE_ALIAS_PATH
;
163 struct alias_map item
;
168 retval
= (struct alias_map
*) bsearch (&item
, map
, nmap
,
169 sizeof (struct alias_map
),
170 (int (*) (const void *,
176 /* We really found an alias. Return the value. */
179 result
= retval
->value
;
183 /* Perhaps we can find another alias file. */
185 while (added
== 0 && locale_alias_path
[0] != '\0')
189 while (locale_alias_path
[0] == PATH_SEPARATOR
)
191 start
= locale_alias_path
;
193 while (locale_alias_path
[0] != '\0'
194 && locale_alias_path
[0] != PATH_SEPARATOR
)
197 if (start
< locale_alias_path
)
198 added
= read_alias_file (start
, locale_alias_path
- start
);
203 __libc_lock_unlock (lock
);
210 read_alias_file (const char *fname
, int fname_len
)
215 static const char aliasfile
[] = "/locale.alias";
217 full_fname
= (char *) alloca (fname_len
+ sizeof aliasfile
);
219 mempcpy (mempcpy (full_fname
, fname
, fname_len
),
220 aliasfile
, sizeof aliasfile
);
222 memcpy (full_fname
, fname
, fname_len
);
223 memcpy (&full_fname
[fname_len
], aliasfile
, sizeof aliasfile
);
227 /* Note the file is opened with cancellation in the I/O functions
229 fp
= fopen (relocate (full_fname
), "rce");
231 fp
= fopen (relocate (full_fname
), "r");
237 #ifdef HAVE___FSETLOCKING
238 /* No threads present. */
239 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
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
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
258 if (FGETS (buf
, sizeof buf
, fp
) == NULL
)
262 /* Determine whether the line is complete. */
263 complete_line
= strchr (buf
, '\n') != NULL
;
266 /* Ignore leading white space. */
267 while (isspace ((unsigned char) cp
[0]))
270 /* A leading '#' signals a comment line. */
271 if (cp
[0] != '\0' && cp
[0] != '#')
274 while (cp
[0] != '\0' && !isspace ((unsigned char) cp
[0]))
276 /* Terminate alias name. */
280 /* Now look for the beginning of the value. */
281 while (isspace ((unsigned char) cp
[0]))
287 while (cp
[0] != '\0' && !isspace ((unsigned char) cp
[0]))
289 /* Terminate value. */
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. */
298 else if (cp
[0] != '\0')
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
306 if (strchr (alias
, '_') == NULL
)
313 if (__builtin_expect (extend_alias_table (), 0))
316 alias_len
= strlen (alias
) + 1;
317 value_len
= strlen (value
) + 1;
319 if (string_space_act
+ alias_len
+ value_len
> string_space_max
)
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
)
329 if (__builtin_expect (string_space
!= new_pool
, 0))
333 for (i
= 0; i
< nmap
; i
++)
335 map
[i
].alias
+= new_pool
- string_space
;
336 map
[i
].value
+= new_pool
- string_space
;
340 string_space
= new_pool
;
341 string_space_max
= new_size
;
345 (const char *) memcpy (&string_space
[string_space_act
],
347 string_space_act
+= alias_len
;
350 (const char *) memcpy (&string_space
[string_space_act
],
352 string_space_act
+= value_len
;
360 /* Possibly not the whole line fits into the buffer. Ignore
361 the rest of the line. */
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. */
368 while (strchr (buf
, '\n') == NULL
);
372 /* Should we test for ferror()? I think we have to silently ignore
377 qsort (map
, nmap
, sizeof (struct alias_map
),
378 (int (*) (const void *, const void *)) alias_compare
);
385 extend_alias_table (void)
388 struct alias_map
*new_map
;
390 new_size
= maxmap
== 0 ? 100 : 2 * maxmap
;
391 new_map
= (struct alias_map
*) realloc (map
, (new_size
392 * sizeof (struct alias_map
)));
394 /* Simply don't extend: we don't have any more core. */
404 alias_compare (const struct alias_map
*map1
, const struct alias_map
*map2
)
406 #if defined _LIBC || defined HAVE_STRCASECMP
407 return strcasecmp (map1
->alias
, map2
->alias
);
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
;
418 /* I know this seems to be odd but the tolower() function in
419 some systems libc cannot handle nonalpha characters. */
420 c1
= isupper (*p1
) ? tolower (*p1
) : *p1
;
421 c2
= isupper (*p2
) ? tolower (*p2
) : *p2
;