1
/* Handle aliases for locale names.
2
Copyright (C) 1995-1999, 2000, 2001 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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
#include <sys/types.h>
35
# define alloca __builtin_alloca
36
# define HAVE_ALLOCA 1
38
# if defined HAVE_ALLOCA_H || defined _LIBC
54
#if !HAVE_STRCHR && !defined _LIBC
62
/* @@ end of prolog @@ */
65
/* Rename the non ANSI C functions. This is required by the standard
66
because some ANSI C functions will require linking with this object
67
file and the name space must not be polluted. */
68
# define strcasecmp __strcasecmp
71
# define mempcpy __mempcpy
73
# define HAVE_MEMPCPY 1
75
/* We need locking here since we can be called from different places. */
76
# include <bits/libc-lock.h>
78
__libc_lock_define_initialized (static, lock);
81
#ifndef internal_function
82
# define internal_function
85
/* For those losing systems which don't have `alloca' we have to add
86
some additional code emulating it. */
88
# define freea(p) /* nothing */
90
# define alloca(n) malloc (n)
91
# define freea(p) free (p)
94
#if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
96
# define fgets(buf, len, s) fgets_unlocked (buf, len, s)
98
#if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
100
# define feof(s) feof_unlocked (s)
111
static char *string_space;
112
static size_t string_space_act;
113
static size_t string_space_max;
114
static struct alias_map *map;
116
static size_t maxmap;
119
/* Prototypes for local functions. */
120
static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
122
static int extend_alias_table PARAMS ((void));
123
static int alias_compare PARAMS ((const struct alias_map *map1,
124
const struct alias_map *map2));
128
_nl_expand_alias (name)
131
static const char *locale_alias_path = LOCALE_ALIAS_PATH;
132
struct alias_map *retval;
133
const char *result = NULL;
137
__libc_lock_lock (lock);
142
struct alias_map item;
147
retval = (struct alias_map *) bsearch (&item, map, nmap,
148
sizeof (struct alias_map),
149
(int (*) PARAMS ((const void *,
155
/* We really found an alias. Return the value. */
158
result = retval->value;
162
/* Perhaps we can find another alias file. */
164
while (added == 0 && locale_alias_path[0] != '\0')
168
while (locale_alias_path[0] == PATH_SEPARATOR)
170
start = locale_alias_path;
172
while (locale_alias_path[0] != '\0'
173
&& locale_alias_path[0] != PATH_SEPARATOR)
176
if (start < locale_alias_path)
177
added = read_alias_file (start, locale_alias_path - start);
183
__libc_lock_unlock (lock);
192
read_alias_file (fname, fname_len)
199
static const char aliasfile[] = "/locale.alias";
201
full_fname = (char *) alloca (fname_len + sizeof aliasfile);
203
mempcpy (mempcpy (full_fname, fname, fname_len),
204
aliasfile, sizeof aliasfile);
206
memcpy (full_fname, fname, fname_len);
207
memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
210
fp = fopen (full_fname, "r");
218
/* It is a reasonable approach to use a fix buffer here because
219
a) we are only interested in the first two fields
220
b) these fields must be usable as file names and so must not
228
if (fgets (buf, sizeof buf, fp) == NULL)
232
/* Possibly not the whole line fits into the buffer. Ignore
233
the rest of the line. */
234
if (strchr (buf, '\n') == NULL)
238
if (fgets (altbuf, sizeof altbuf, fp) == NULL)
239
/* Make sure the inner loop will be left. The outer loop
240
will exit at the `feof' test. */
242
while (strchr (altbuf, '\n') == NULL);
246
/* Ignore leading white space. */
247
while (isspace (cp[0]))
250
/* A leading '#' signals a comment line. */
251
if (cp[0] != '\0' && cp[0] != '#')
254
while (cp[0] != '\0' && !isspace (cp[0]))
256
/* Terminate alias name. */
260
/* Now look for the beginning of the value. */
261
while (isspace (cp[0]))
270
while (cp[0] != '\0' && !isspace (cp[0]))
272
/* Terminate value. */
275
/* This has to be done to make the following test
276
for the end of line possible. We are looking for
277
the terminating '\n' which do not overwrite here. */
281
else if (cp[0] != '\0')
285
if (__builtin_expect (extend_alias_table (), 0))
288
alias_len = strlen (alias) + 1;
289
value_len = strlen (value) + 1;
291
if (string_space_act + alias_len + value_len > string_space_max)
293
/* Increase size of memory pool. */
294
size_t new_size = (string_space_max
295
+ (alias_len + value_len > 1024
296
? alias_len + value_len : 1024));
297
char *new_pool = (char *) realloc (string_space, new_size);
298
if (new_pool == NULL)
301
if (__builtin_expect (string_space != new_pool, 0))
305
for (i = 0; i < nmap; i++)
307
map[i].alias += new_pool - string_space;
308
map[i].value += new_pool - string_space;
312
string_space = new_pool;
313
string_space_max = new_size;
316
map[nmap].alias = memcpy (&string_space[string_space_act],
318
string_space_act += alias_len;
320
map[nmap].value = memcpy (&string_space[string_space_act],
322
string_space_act += value_len;
330
/* Should we test for ferror()? I think we have to silently ignore
335
qsort (map, nmap, sizeof (struct alias_map),
336
(int (*) PARAMS ((const void *, const void *))) alias_compare);
343
extend_alias_table ()
346
struct alias_map *new_map;
348
new_size = maxmap == 0 ? 100 : 2 * maxmap;
349
new_map = (struct alias_map *) realloc (map, (new_size
350
* sizeof (struct alias_map)));
352
/* Simply don't extend: we don't have any more core. */
362
static void __attribute__ ((unused))
365
if (string_space != NULL)
370
text_set_element (__libc_subfreeres, free_mem);
375
alias_compare (map1, map2)
376
const struct alias_map *map1;
377
const struct alias_map *map2;
379
#if defined _LIBC || defined HAVE_STRCASECMP
380
return strcasecmp (map1->alias, map2->alias);
382
const unsigned char *p1 = (const unsigned char *) map1->alias;
383
const unsigned char *p2 = (const unsigned char *) map2->alias;
384
unsigned char c1, c2;
391
/* I know this seems to be odd but the tolower() function in
392
some systems libc cannot handle nonalpha characters. */
393
c1 = isupper (*p1) ? tolower (*p1) : *p1;
394
c2 = isupper (*p2) ? tolower (*p2) : *p2;