Update.
[glibc.git] / intl / localealias.c
blob0afe98134b34a1c121070ebbf4899da364e5d58c
1 /* Handle aliases for locale names.
2 Copyright (C) 1995-1999, 2000,01,02 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
18 02111-1307 USA. */
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. */
23 #ifndef _GNU_SOURCE
24 # define _GNU_SOURCE 1
25 #endif
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
31 #include <ctype.h>
32 #include <stdio.h>
33 #if defined _LIBC || defined HAVE___FSETLOCKING
34 # include <stdio_ext.h>
35 #endif
36 #include <sys/types.h>
38 #ifdef __GNUC__
39 # undef alloca
40 # define alloca __builtin_alloca
41 # define HAVE_ALLOCA 1
42 #else
43 # if defined HAVE_ALLOCA_H || defined _LIBC
44 # include <alloca.h>
45 # else
46 # ifdef _AIX
47 #pragma alloca
48 # else
49 # ifndef alloca
50 char *alloca ();
51 # endif
52 # endif
53 # endif
54 #endif
56 #include <stdlib.h>
57 #include <string.h>
59 #include "gettextP.h"
61 /* @@ end of prolog @@ */
63 #ifdef _LIBC
64 /* Rename the non ANSI C functions. This is required by the standard
65 because some ANSI C functions will require linking with this object
66 file and the name space must not be polluted. */
67 # define strcasecmp __strcasecmp
69 # ifndef mempcpy
70 # define mempcpy __mempcpy
71 # endif
72 # define HAVE_MEMPCPY 1
73 # define HAVE___FSETLOCKING 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);
79 #endif
81 #ifndef internal_function
82 # define internal_function
83 #endif
85 /* Some optimizations for glibc. */
86 #ifdef _LIBC
87 # define FEOF(fp) feof_unlocked (fp)
88 # define FGETS(buf, n, fp) fgets_unlocked (buf, n, fp)
89 #else
90 # define FEOF(fp) feof (fp)
91 # define FGETS(buf, n, fp) fgets (buf, n, fp)
92 #endif
94 /* For those losing systems which don't have `alloca' we have to add
95 some additional code emulating it. */
96 #ifdef HAVE_ALLOCA
97 # define freea(p) /* nothing */
98 #else
99 # define alloca(n) malloc (n)
100 # define freea(p) free (p)
101 #endif
103 #if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
104 # undef fgets
105 # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
106 #endif
107 #if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
108 # undef feof
109 # define feof(s) feof_unlocked (s)
110 #endif
113 struct alias_map
115 const char *alias;
116 const char *value;
120 static char *string_space;
121 static size_t string_space_act;
122 static size_t string_space_max;
123 static struct alias_map *map;
124 static size_t nmap;
125 static size_t maxmap;
128 /* Prototypes for local functions. */
129 static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
130 internal_function;
131 static int extend_alias_table PARAMS ((void));
132 static int alias_compare PARAMS ((const struct alias_map *map1,
133 const struct alias_map *map2));
136 const char *
137 _nl_expand_alias (name)
138 const char *name;
140 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
141 struct alias_map *retval;
142 const char *result = NULL;
143 size_t added;
145 #ifdef _LIBC
146 __libc_lock_lock (lock);
147 #endif
151 struct alias_map item;
153 item.alias = name;
155 if (nmap > 0)
156 retval = (struct alias_map *) bsearch (&item, map, nmap,
157 sizeof (struct alias_map),
158 (int (*) PARAMS ((const void *,
159 const void *))
160 ) alias_compare);
161 else
162 retval = NULL;
164 /* We really found an alias. Return the value. */
165 if (retval != NULL)
167 result = retval->value;
168 break;
171 /* Perhaps we can find another alias file. */
172 added = 0;
173 while (added == 0 && locale_alias_path[0] != '\0')
175 const char *start;
177 while (locale_alias_path[0] == ':')
178 ++locale_alias_path;
179 start = locale_alias_path;
181 while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
182 ++locale_alias_path;
184 if (start < locale_alias_path)
185 added = read_alias_file (start, locale_alias_path - start);
188 while (added != 0);
190 #ifdef _LIBC
191 __libc_lock_unlock (lock);
192 #endif
194 return result;
198 static size_t
199 internal_function
200 read_alias_file (fname, fname_len)
201 const char *fname;
202 int fname_len;
204 FILE *fp;
205 char *full_fname;
206 size_t added;
207 static const char aliasfile[] = "/locale.alias";
209 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
210 #ifdef HAVE_MEMPCPY
211 mempcpy (mempcpy (full_fname, fname, fname_len),
212 aliasfile, sizeof aliasfile);
213 #else
214 memcpy (full_fname, fname, fname_len);
215 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
216 #endif
218 fp = fopen (full_fname, "r");
219 freea (full_fname);
220 if (fp == NULL)
221 return 0;
223 #ifdef HAVE___FSETLOCKING
224 /* No threads present. */
225 __fsetlocking (fp, FSETLOCKING_BYCALLER);
226 #endif
228 added = 0;
229 while (!FEOF (fp))
231 /* It is a reasonable approach to use a fix buffer here because
232 a) we are only interested in the first two fields
233 b) these fields must be usable as file names and so must not
234 be that long
236 char buf[BUFSIZ];
237 char *alias;
238 char *value;
239 char *cp;
241 if (FGETS (buf, sizeof buf, fp) == NULL)
242 /* EOF reached. */
243 break;
245 /* Possibly not the whole line fits into the buffer. Ignore
246 the rest of the line. */
247 if (strchr (buf, '\n') == NULL)
249 char altbuf[BUFSIZ];
251 if (FGETS (altbuf, sizeof altbuf, fp) == NULL)
252 /* Make sure the inner loop will be left. The outer loop
253 will exit at the `feof' test. */
254 break;
255 while (strchr (altbuf, '\n') == NULL);
258 cp = buf;
259 /* Ignore leading white space. */
260 while (isspace ((unsigned char) cp[0]))
261 ++cp;
263 /* A leading '#' signals a comment line. */
264 if (cp[0] != '\0' && cp[0] != '#')
266 alias = cp++;
267 while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
268 ++cp;
269 /* Terminate alias name. */
270 if (cp[0] != '\0')
271 *cp++ = '\0';
273 /* Now look for the beginning of the value. */
274 while (isspace ((unsigned char) cp[0]))
275 ++cp;
277 if (cp[0] != '\0')
279 size_t alias_len;
280 size_t value_len;
282 value = cp++;
283 while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
284 ++cp;
285 /* Terminate value. */
286 if (cp[0] == '\n')
288 /* This has to be done to make the following test
289 for the end of line possible. We are looking for
290 the terminating '\n' which do not overwrite here. */
291 *cp++ = '\0';
292 *cp = '\n';
294 else if (cp[0] != '\0')
295 *cp++ = '\0';
297 if (nmap >= maxmap)
298 if (__builtin_expect (extend_alias_table (), 0))
299 return added;
301 alias_len = strlen (alias) + 1;
302 value_len = strlen (value) + 1;
304 if (string_space_act + alias_len + value_len > string_space_max)
306 /* Increase size of memory pool. */
307 size_t new_size = (string_space_max
308 + (alias_len + value_len > 1024
309 ? alias_len + value_len : 1024));
310 char *new_pool = (char *) realloc (string_space, new_size);
311 if (new_pool == NULL)
312 return added;
314 if (__builtin_expect (string_space != new_pool, 0))
316 size_t i;
318 for (i = 0; i < nmap; i++)
320 map[i].alias += new_pool - string_space;
321 map[i].value += new_pool - string_space;
325 string_space = new_pool;
326 string_space_max = new_size;
329 map[nmap].alias = memcpy (&string_space[string_space_act],
330 alias, alias_len);
331 string_space_act += alias_len;
333 map[nmap].value = memcpy (&string_space[string_space_act],
334 value, value_len);
335 string_space_act += value_len;
337 ++nmap;
338 ++added;
343 /* Should we test for ferror()? I think we have to silently ignore
344 errors. --drepper */
345 fclose (fp);
347 if (added > 0)
348 qsort (map, nmap, sizeof (struct alias_map),
349 (int (*) PARAMS ((const void *, const void *))) alias_compare);
351 return added;
355 static int
356 extend_alias_table ()
358 size_t new_size;
359 struct alias_map *new_map;
361 new_size = maxmap == 0 ? 100 : 2 * maxmap;
362 new_map = (struct alias_map *) realloc (map, (new_size
363 * sizeof (struct alias_map)));
364 if (new_map == NULL)
365 /* Simply don't extend: we don't have any more core. */
366 return -1;
368 map = new_map;
369 maxmap = new_size;
370 return 0;
374 #ifdef _LIBC
375 static void __attribute__ ((unused))
376 free_mem (void)
378 if (string_space != NULL)
379 free (string_space);
380 if (map != NULL)
381 free (map);
383 text_set_element (__libc_subfreeres, free_mem);
384 #endif
387 static int
388 alias_compare (map1, map2)
389 const struct alias_map *map1;
390 const struct alias_map *map2;
392 #if defined _LIBC || defined HAVE_STRCASECMP
393 return strcasecmp (map1->alias, map2->alias);
394 #else
395 const unsigned char *p1 = (const unsigned char *) map1->alias;
396 const unsigned char *p2 = (const unsigned char *) map2->alias;
397 unsigned char c1, c2;
399 if (p1 == p2)
400 return 0;
404 /* I know this seems to be odd but the tolower() function in
405 some systems libc cannot handle nonalpha characters. */
406 c1 = isupper (*p1) ? tolower (*p1) : *p1;
407 c2 = isupper (*p2) ? tolower (*p2) : *p2;
408 if (c1 == '\0')
409 break;
410 ++p1;
411 ++p2;
413 while (c1 == c2);
415 return c1 - c2;
416 #endif