Update copyright years
[glibc.git] / intl / localealias.c
blob1341228716241ffeff6f720a72e32ca9833924ea
1 /* Handle aliases for locale names.
2 Copyright (C) 1995-2002, 2003, 2005, 2011 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, see
17 <http://www.gnu.org/licenses/>. */
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. */
22 #ifndef _GNU_SOURCE
23 # define _GNU_SOURCE 1
24 #endif
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
30 #include <ctype.h>
31 #include <stdio.h>
32 #if defined _LIBC || defined HAVE___FSETLOCKING
33 # include <stdio_ext.h>
34 #endif
35 #include <sys/types.h>
37 #ifdef __GNUC__
38 # undef alloca
39 # define alloca __builtin_alloca
40 # define HAVE_ALLOCA 1
41 #else
42 # if defined HAVE_ALLOCA_H || defined _LIBC
43 # include <alloca.h>
44 # else
45 # ifdef _AIX
46 #pragma alloca
47 # else
48 # ifndef alloca
49 char *alloca ();
50 # endif
51 # endif
52 # endif
53 #endif
55 #include <stdlib.h>
56 #include <string.h>
58 #include "gettextP.h"
60 /* @@ end of prolog @@ */
62 #ifdef _LIBC
63 /* Rename the non ANSI C functions. This is required by the standard
64 because some ANSI C functions will require linking with this object
65 file and the name space must not be polluted. */
66 # define strcasecmp(s1, s2) __strcasecmp_l (s1, s2, _nl_C_locobj_ptr)
68 # ifndef mempcpy
69 # define mempcpy __mempcpy
70 # endif
71 # define HAVE_MEMPCPY 1
72 # define HAVE___FSETLOCKING 1
74 /* We need locking here since we can be called from different places. */
75 # include <bits/libc-lock.h>
77 __libc_lock_define_initialized (static, lock);
78 #endif
80 #ifndef internal_function
81 # define internal_function
82 #endif
84 /* Some optimizations for glibc. */
85 #ifdef _LIBC
86 # define FEOF(fp) feof_unlocked (fp)
87 # define FGETS(buf, n, fp) fgets_unlocked (buf, n, fp)
88 #else
89 # define FEOF(fp) feof (fp)
90 # define FGETS(buf, n, fp) fgets (buf, n, fp)
91 #endif
93 /* For those losing systems which don't have `alloca' we have to add
94 some additional code emulating it. */
95 #ifdef HAVE_ALLOCA
96 # define freea(p) /* nothing */
97 #else
98 # define alloca(n) malloc (n)
99 # define freea(p) free (p)
100 #endif
102 #if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
103 # undef fgets
104 # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
105 #endif
106 #if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
107 # undef feof
108 # define feof(s) feof_unlocked (s)
109 #endif
112 struct alias_map
114 const char *alias;
115 const char *value;
119 #ifndef _LIBC
120 # define libc_freeres_ptr(decl) decl
121 #endif
123 libc_freeres_ptr (static char *string_space);
124 static size_t string_space_act;
125 static size_t string_space_max;
126 libc_freeres_ptr (static struct alias_map *map);
127 static size_t nmap;
128 static size_t maxmap;
131 /* Prototypes for local functions. */
132 static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
133 internal_function;
134 static int extend_alias_table PARAMS ((void));
135 static int alias_compare PARAMS ((const struct alias_map *map1,
136 const struct alias_map *map2));
139 const char *
140 _nl_expand_alias (name)
141 const char *name;
143 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
144 struct alias_map *retval;
145 const char *result = NULL;
146 size_t added;
148 #ifdef _LIBC
149 __libc_lock_lock (lock);
150 #endif
154 struct alias_map item;
156 item.alias = name;
158 if (nmap > 0)
159 retval = (struct alias_map *) bsearch (&item, map, nmap,
160 sizeof (struct alias_map),
161 (int (*) PARAMS ((const void *,
162 const void *))
163 ) alias_compare);
164 else
165 retval = NULL;
167 /* We really found an alias. Return the value. */
168 if (retval != NULL)
170 result = retval->value;
171 break;
174 /* Perhaps we can find another alias file. */
175 added = 0;
176 while (added == 0 && locale_alias_path[0] != '\0')
178 const char *start;
180 while (locale_alias_path[0] == ':')
181 ++locale_alias_path;
182 start = locale_alias_path;
184 while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
185 ++locale_alias_path;
187 if (start < locale_alias_path)
188 added = read_alias_file (start, locale_alias_path - start);
191 while (added != 0);
193 #ifdef _LIBC
194 __libc_lock_unlock (lock);
195 #endif
197 return result;
201 static size_t
202 internal_function
203 read_alias_file (fname, fname_len)
204 const char *fname;
205 int fname_len;
207 FILE *fp;
208 char *full_fname;
209 size_t added;
210 static const char aliasfile[] = "/locale.alias";
212 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
213 #ifdef HAVE_MEMPCPY
214 mempcpy (mempcpy (full_fname, fname, fname_len),
215 aliasfile, sizeof aliasfile);
216 #else
217 memcpy (full_fname, fname, fname_len);
218 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
219 #endif
221 /* Note the file is opened with cancellation in the I/O functions
222 disabled. */
223 fp = fopen (full_fname, "rce");
224 freea (full_fname);
225 if (fp == NULL)
226 return 0;
228 #ifdef HAVE___FSETLOCKING
229 /* No threads present. */
230 __fsetlocking (fp, FSETLOCKING_BYCALLER);
231 #endif
233 added = 0;
234 while (!FEOF (fp))
236 /* It is a reasonable approach to use a fix buffer here because
237 a) we are only interested in the first two fields
238 b) these fields must be usable as file names and so must not
239 be that long
240 We avoid a multi-kilobyte buffer here since this would use up
241 stack space which we might not have if the program ran out of
242 memory. */
243 char buf[400];
244 char *alias;
245 char *value;
246 char *cp;
247 int complete_line;
249 if (FGETS (buf, sizeof buf, fp) == NULL)
250 /* EOF reached. */
251 break;
253 /* Determine whether the line is complete. */
254 complete_line = strchr (buf, '\n') != NULL;
256 cp = buf;
257 /* Ignore leading white space. */
258 while (isspace ((unsigned char) cp[0]))
259 ++cp;
261 /* A leading '#' signals a comment line. */
262 if (cp[0] != '\0' && cp[0] != '#')
264 alias = cp++;
265 while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
266 ++cp;
267 /* Terminate alias name. */
268 if (cp[0] != '\0')
269 *cp++ = '\0';
271 /* Now look for the beginning of the value. */
272 while (isspace ((unsigned char) cp[0]))
273 ++cp;
275 if (cp[0] != '\0')
277 size_t alias_len;
278 size_t value_len;
280 value = cp++;
281 while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
282 ++cp;
283 /* Terminate value. */
284 if (cp[0] == '\n')
286 /* This has to be done to make the following test
287 for the end of line possible. We are looking for
288 the terminating '\n' which do not overwrite here. */
289 *cp++ = '\0';
290 *cp = '\n';
292 else if (cp[0] != '\0')
293 *cp++ = '\0';
295 if (nmap >= maxmap)
296 if (__builtin_expect (extend_alias_table (), 0))
297 goto out;
299 alias_len = strlen (alias) + 1;
300 value_len = strlen (value) + 1;
302 if (string_space_act + alias_len + value_len > string_space_max)
304 /* Increase size of memory pool. */
305 size_t new_size = (string_space_max
306 + (alias_len + value_len > 1024
307 ? alias_len + value_len : 1024));
308 char *new_pool = (char *) realloc (string_space, new_size);
309 if (new_pool == NULL)
310 goto out;
312 if (__builtin_expect (string_space != new_pool, 0))
314 size_t i;
316 for (i = 0; i < nmap; i++)
318 map[i].alias += new_pool - string_space;
319 map[i].value += new_pool - string_space;
323 string_space = new_pool;
324 string_space_max = new_size;
327 map[nmap].alias = memcpy (&string_space[string_space_act],
328 alias, alias_len);
329 string_space_act += alias_len;
331 map[nmap].value = memcpy (&string_space[string_space_act],
332 value, value_len);
333 string_space_act += value_len;
335 ++nmap;
336 ++added;
340 /* Possibly not the whole line fits into the buffer. Ignore
341 the rest of the line. */
342 if (! complete_line)
344 if (FGETS (buf, sizeof buf, fp) == NULL)
345 /* Make sure the inner loop will be left. The outer loop
346 will exit at the `feof' test. */
347 break;
348 while (strchr (buf, '\n') == NULL);
351 out:
352 /* Should we test for ferror()? I think we have to silently ignore
353 errors. --drepper */
354 fclose (fp);
356 if (added > 0)
357 qsort (map, nmap, sizeof (struct alias_map),
358 (int (*) PARAMS ((const void *, const void *))) alias_compare);
360 return added;
364 static int
365 extend_alias_table ()
367 size_t new_size;
368 struct alias_map *new_map;
370 new_size = maxmap == 0 ? 100 : 2 * maxmap;
371 new_map = (struct alias_map *) realloc (map, (new_size
372 * sizeof (struct alias_map)));
373 if (new_map == NULL)
374 /* Simply don't extend: we don't have any more core. */
375 return -1;
377 map = new_map;
378 maxmap = new_size;
379 return 0;
383 static int
384 alias_compare (map1, map2)
385 const struct alias_map *map1;
386 const struct alias_map *map2;
388 #if defined _LIBC || defined HAVE_STRCASECMP
389 return strcasecmp (map1->alias, map2->alias);
390 #else
391 const unsigned char *p1 = (const unsigned char *) map1->alias;
392 const unsigned char *p2 = (const unsigned char *) map2->alias;
393 unsigned char c1, c2;
395 if (p1 == p2)
396 return 0;
400 /* I know this seems to be odd but the tolower() function in
401 some systems libc cannot handle nonalpha characters. */
402 c1 = isupper (*p1) ? tolower (*p1) : *p1;
403 c2 = isupper (*p2) ? tolower (*p2) : *p2;
404 if (c1 == '\0')
405 break;
406 ++p1;
407 ++p2;
409 while (c1 == c2);
411 return c1 - c2;
412 #endif