1 /* Handle aliases for locale names.
2 Copyright (C) 1995-1999, 2000, 2001 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* Tell glibc's <string.h> to provide a prototype for mempcpy().
21 This must come before <config.h> because <config.h> may include
22 <features.h>, and once <features.h> has been included, it's too late. */
24 # define _GNU_SOURCE 1
33 #if defined _LIBC || defined HAVE___FSETLOCKING
34 # include <stdio_ext.h>
36 #include <sys/types.h>
39 # define alloca __builtin_alloca
40 # define HAVE_ALLOCA 1
42 # if defined HAVE_ALLOCA_H || defined _LIBC
55 #if defined STDC_HEADERS || defined _LIBC
65 #if defined HAVE_STRING_H || defined _LIBC
70 # define memcpy(Dst, Src, Num) (bcopy (Src, Dst, Num), (Dst))
73 #if !HAVE_STRCHR && !defined _LIBC
81 /* @@ end of prolog @@ */
84 /* Rename the non ANSI C functions. This is required by the standard
85 because some ANSI C functions will require linking with this object
86 file and the name space must not be polluted. */
87 # define strcasecmp __strcasecmp
90 # define mempcpy __mempcpy
92 # define HAVE_MEMPCPY 1
93 # define HAVE___FSETLOCKING 1
95 /* We need locking here since we can be called from different places. */
96 # include <bits/libc-lock.h>
98 __libc_lock_define_initialized (static, lock
);
101 #ifndef internal_function
102 # define internal_function
105 /* Some optimizations for glibc. */
107 # define FEOF(fp) feof_unlocked (fp)
108 # define FGETS(buf, n, fp) fgets_unlocked (buf, n, fp)
110 # define FEOF(fp) feof (fp)
111 # define FGETS(buf, n, fp) fgets (buf, n, fp)
114 /* For those losing systems which don't have `alloca' we have to add
115 some additional code emulating it. */
117 # define freea(p) /* nothing */
119 # define alloca(n) malloc (n)
120 # define freea(p) free (p)
123 #if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
125 # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
127 #if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
129 # define feof(s) feof_unlocked (s)
140 static char *string_space
;
141 static size_t string_space_act
;
142 static size_t string_space_max
;
143 static struct alias_map
*map
;
145 static size_t maxmap
;
148 /* Prototypes for local functions. */
149 static size_t read_alias_file
PARAMS ((const char *fname
, int fname_len
))
151 static int extend_alias_table
PARAMS ((void));
152 static int alias_compare
PARAMS ((const struct alias_map
*map1
,
153 const struct alias_map
*map2
));
157 _nl_expand_alias (name
)
160 static const char *locale_alias_path
= LOCALE_ALIAS_PATH
;
161 struct alias_map
*retval
;
162 const char *result
= NULL
;
166 __libc_lock_lock (lock
);
171 struct alias_map item
;
176 retval
= (struct alias_map
*) bsearch (&item
, map
, nmap
,
177 sizeof (struct alias_map
),
178 (int (*) PARAMS ((const void *,
184 /* We really found an alias. Return the value. */
187 result
= retval
->value
;
191 /* Perhaps we can find another alias file. */
193 while (added
== 0 && locale_alias_path
[0] != '\0')
197 while (locale_alias_path
[0] == ':')
199 start
= locale_alias_path
;
201 while (locale_alias_path
[0] != '\0' && locale_alias_path
[0] != ':')
204 if (start
< locale_alias_path
)
205 added
= read_alias_file (start
, locale_alias_path
- start
);
211 __libc_lock_unlock (lock
);
220 read_alias_file (fname
, fname_len
)
227 static const char aliasfile
[] = "/locale.alias";
229 full_fname
= (char *) alloca (fname_len
+ sizeof aliasfile
);
231 mempcpy (mempcpy (full_fname
, fname
, fname_len
),
232 aliasfile
, sizeof aliasfile
);
234 memcpy (full_fname
, fname
, fname_len
);
235 memcpy (&full_fname
[fname_len
], aliasfile
, sizeof aliasfile
);
238 fp
= fopen (full_fname
, "r");
243 #ifdef HAVE___FSETLOCKING
244 /* No threads present. */
245 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
251 /* It is a reasonable approach to use a fix buffer here because
252 a) we are only interested in the first two fields
253 b) these fields must be usable as file names and so must not
261 if (FGETS (buf
, sizeof buf
, fp
) == NULL
)
265 /* Possibly not the whole line fits into the buffer. Ignore
266 the rest of the line. */
267 if (strchr (buf
, '\n') == NULL
)
271 if (FGETS (altbuf
, sizeof altbuf
, fp
) == NULL
)
272 /* Make sure the inner loop will be left. The outer loop
273 will exit at the `feof' test. */
275 while (strchr (altbuf
, '\n') == NULL
);
279 /* Ignore leading white space. */
280 while (isspace (cp
[0]))
283 /* A leading '#' signals a comment line. */
284 if (cp
[0] != '\0' && cp
[0] != '#')
287 while (cp
[0] != '\0' && !isspace (cp
[0]))
289 /* Terminate alias name. */
293 /* Now look for the beginning of the value. */
294 while (isspace (cp
[0]))
303 while (cp
[0] != '\0' && !isspace (cp
[0]))
305 /* Terminate value. */
308 /* This has to be done to make the following test
309 for the end of line possible. We are looking for
310 the terminating '\n' which do not overwrite here. */
314 else if (cp
[0] != '\0')
318 if (__builtin_expect (extend_alias_table (), 0))
321 alias_len
= strlen (alias
) + 1;
322 value_len
= strlen (value
) + 1;
324 if (string_space_act
+ alias_len
+ value_len
> string_space_max
)
326 /* Increase size of memory pool. */
327 size_t new_size
= (string_space_max
328 + (alias_len
+ value_len
> 1024
329 ? alias_len
+ value_len
: 1024));
330 char *new_pool
= (char *) realloc (string_space
, new_size
);
331 if (new_pool
== NULL
)
334 if (__builtin_expect (string_space
!= new_pool
, 0))
338 for (i
= 0; i
< nmap
; i
++)
340 map
[i
].alias
+= new_pool
- string_space
;
341 map
[i
].value
+= new_pool
- string_space
;
345 string_space
= new_pool
;
346 string_space_max
= new_size
;
349 map
[nmap
].alias
= memcpy (&string_space
[string_space_act
],
351 string_space_act
+= alias_len
;
353 map
[nmap
].value
= memcpy (&string_space
[string_space_act
],
355 string_space_act
+= value_len
;
363 /* Should we test for ferror()? I think we have to silently ignore
368 qsort (map
, nmap
, sizeof (struct alias_map
),
369 (int (*) PARAMS ((const void *, const void *))) alias_compare
);
376 extend_alias_table ()
379 struct alias_map
*new_map
;
381 new_size
= maxmap
== 0 ? 100 : 2 * maxmap
;
382 new_map
= (struct alias_map
*) realloc (map
, (new_size
383 * sizeof (struct alias_map
)));
385 /* Simply don't extend: we don't have any more core. */
395 static void __attribute__ ((unused
))
398 if (string_space
!= NULL
)
403 text_set_element (__libc_subfreeres
, free_mem
);
408 alias_compare (map1
, map2
)
409 const struct alias_map
*map1
;
410 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
;