1
/* Handle aliases for locale names.
2
Copyright (C) 1995-1999, 2000-2001, 2003, 2005 Free Software Foundation, Inc.
4
This program is free software; you can redistribute it and/or modify it
5
under the terms of the GNU Library General Public License as published
6
by the Free Software Foundation; either version 2, or (at your option)
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 GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public
15
License along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19
/* Tell glibc's <string.h> to provide a prototype for mempcpy().
20
This must come before <config.h> because <config.h> may include
21
<features.h>, and once <features.h> has been included, it's too late. */
23
# define _GNU_SOURCE 1
32
#if defined _LIBC || defined HAVE___FSETLOCKING
33
# include <stdio_ext.h>
35
#include <sys/types.h>
39
# define alloca __builtin_alloca
40
# define HAVE_ALLOCA 1
44
# define alloca _alloca
46
# if defined HAVE_ALLOCA_H || defined _LIBC
65
#if ENABLE_RELOCATABLE
66
# include "relocatable.h"
68
# define relocate(pathname) (pathname)
71
/* @@ end of prolog @@ */
74
/* Rename the non ANSI C functions. This is required by the standard
75
because some ANSI C functions will require linking with this object
76
file and the name space must not be polluted. */
77
# define strcasecmp __strcasecmp
80
# define mempcpy __mempcpy
82
# define HAVE_MEMPCPY 1
83
# define HAVE___FSETLOCKING 1
86
/* Handle multi-threaded applications. */
88
# include <bits/libc-lock.h>
93
#ifndef internal_function
94
# define internal_function
97
/* Some optimizations for glibc. */
99
# define FEOF(fp) feof_unlocked (fp)
100
# define FGETS(buf, n, fp) fgets_unlocked (buf, n, fp)
102
# define FEOF(fp) feof (fp)
103
# define FGETS(buf, n, fp) fgets (buf, n, fp)
106
/* For those losing systems which don't have `alloca' we have to add
107
some additional code emulating it. */
109
# define freea(p) /* nothing */
111
# define alloca(n) malloc (n)
112
# define freea(p) free (p)
115
#if defined _LIBC_REENTRANT || HAVE_DECL_FGETS_UNLOCKED
117
# define fgets(buf, len, s) fgets_unlocked (buf, len, s)
119
#if defined _LIBC_REENTRANT || HAVE_DECL_FEOF_UNLOCKED
121
# define feof(s) feof_unlocked (s)
125
__libc_lock_define_initialized (static, lock)
136
# define libc_freeres_ptr(decl) decl
139
libc_freeres_ptr (static char *string_space);
140
static size_t string_space_act;
141
static size_t string_space_max;
142
libc_freeres_ptr (static struct alias_map *map);
144
static size_t maxmap;
147
/* Prototypes for local functions. */
148
static size_t read_alias_file (const char *fname, int fname_len)
150
static int extend_alias_table (void);
151
static int alias_compare (const struct alias_map *map1,
152
const struct alias_map *map2);
156
_nl_expand_alias (const char *name)
158
static const char *locale_alias_path;
159
struct alias_map *retval;
160
const char *result = NULL;
163
__libc_lock_lock (lock);
165
if (locale_alias_path == NULL)
166
locale_alias_path = LOCALE_ALIAS_PATH;
170
struct alias_map item;
175
retval = (struct alias_map *) bsearch (&item, map, nmap,
176
sizeof (struct alias_map),
177
(int (*) (const void *,
183
/* We really found an alias. Return the value. */
186
result = retval->value;
190
/* Perhaps we can find another alias file. */
192
while (added == 0 && locale_alias_path[0] != '\0')
196
while (locale_alias_path[0] == PATH_SEPARATOR)
198
start = locale_alias_path;
200
while (locale_alias_path[0] != '\0'
201
&& locale_alias_path[0] != PATH_SEPARATOR)
204
if (start < locale_alias_path)
205
added = read_alias_file (start, locale_alias_path - start);
210
__libc_lock_unlock (lock);
218
read_alias_file (const char *fname, int fname_len)
223
static const char aliasfile[] = "/locale.alias";
225
full_fname = (char *) alloca (fname_len + sizeof aliasfile);
227
mempcpy (mempcpy (full_fname, fname, fname_len),
228
aliasfile, sizeof aliasfile);
230
memcpy (full_fname, fname, fname_len);
231
memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
235
/* Note the file is opened with cancellation in the I/O functions
237
fp = fopen (relocate (full_fname), "rc");
239
fp = fopen (relocate (full_fname), "r");
245
#ifdef HAVE___FSETLOCKING
246
/* No threads present. */
247
__fsetlocking (fp, FSETLOCKING_BYCALLER);
253
/* It is a reasonable approach to use a fix buffer here because
254
a) we are only interested in the first two fields
255
b) these fields must be usable as file names and so must not
257
We avoid a multi-kilobyte buffer here since this would use up
258
stack space which we might not have if the program ran out of
266
if (FGETS (buf, sizeof buf, fp) == NULL)
270
/* Determine whether the line is complete. */
271
complete_line = strchr (buf, '\n') != NULL;
274
/* Ignore leading white space. */
275
while (isspace ((unsigned char) cp[0]))
278
/* A leading '#' signals a comment line. */
279
if (cp[0] != '\0' && cp[0] != '#')
282
while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
284
/* Terminate alias name. */
288
/* Now look for the beginning of the value. */
289
while (isspace ((unsigned char) cp[0]))
295
while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
297
/* Terminate value. */
300
/* This has to be done to make the following test
301
for the end of line possible. We are looking for
302
the terminating '\n' which do not overwrite here. */
306
else if (cp[0] != '\0')
310
/* glibc's locale.alias contains entries for ja_JP and ko_KR
311
that make it impossible to use a Japanese or Korean UTF-8
312
locale under the name "ja_JP" or "ko_KR". Ignore these
314
if (strchr (alias, '_') == NULL)
321
if (__builtin_expect (extend_alias_table (), 0))
324
alias_len = strlen (alias) + 1;
325
value_len = strlen (value) + 1;
327
if (string_space_act + alias_len + value_len > string_space_max)
329
/* Increase size of memory pool. */
330
size_t new_size = (string_space_max
331
+ (alias_len + value_len > 1024
332
? alias_len + value_len : 1024));
333
char *new_pool = (char *) realloc (string_space, new_size);
334
if (new_pool == NULL)
337
if (__builtin_expect (string_space != new_pool, 0))
341
for (i = 0; i < nmap; i++)
343
map[i].alias += new_pool - string_space;
344
map[i].value += new_pool - string_space;
348
string_space = new_pool;
349
string_space_max = new_size;
352
map[nmap].alias = memcpy (&string_space[string_space_act],
354
string_space_act += alias_len;
356
map[nmap].value = memcpy (&string_space[string_space_act],
358
string_space_act += value_len;
366
/* Possibly not the whole line fits into the buffer. Ignore
367
the rest of the line. */
370
if (FGETS (buf, sizeof buf, fp) == NULL)
371
/* Make sure the inner loop will be left. The outer loop
372
will exit at the `feof' test. */
374
while (strchr (buf, '\n') == NULL);
378
/* Should we test for ferror()? I think we have to silently ignore
383
qsort (map, nmap, sizeof (struct alias_map),
384
(int (*) (const void *, const void *)) alias_compare);
391
extend_alias_table ()
394
struct alias_map *new_map;
396
new_size = maxmap == 0 ? 100 : 2 * maxmap;
397
new_map = (struct alias_map *) realloc (map, (new_size
398
* sizeof (struct alias_map)));
400
/* Simply don't extend: we don't have any more core. */
410
alias_compare (const struct alias_map *map1, const struct alias_map *map2)
412
#if defined _LIBC || defined HAVE_STRCASECMP
413
return strcasecmp (map1->alias, map2->alias);
415
const unsigned char *p1 = (const unsigned char *) map1->alias;
416
const unsigned char *p2 = (const unsigned char *) map2->alias;
417
unsigned char c1, c2;
424
/* I know this seems to be odd but the tolower() function in
425
some systems libc cannot handle nonalpha characters. */
426
c1 = isupper (*p1) ? tolower (*p1) : *p1;
427
c2 = isupper (*p2) ? tolower (*p2) : *p2;