* posix/execl.c: Fix last argument of memcpy. Reported by Brian
[glibc.git] / intl / localealias.c
blobfa9396fe9a44c5f561b286a63d93e0c4aeec324a
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
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 # 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 #if defined STDC_HEADERS || defined _LIBC
56 # include <stdlib.h>
57 #else
58 # ifdef HAVE_MALLOC_H
59 # include <malloc.h>
60 # else
61 void free ();
62 # endif
63 #endif
65 #if defined HAVE_STRING_H || defined _LIBC
66 # include <string.h>
67 #else
68 # include <strings.h>
69 # ifndef memcpy
70 # define memcpy(Dst, Src, Num) (bcopy (Src, Dst, Num), (Dst))
71 # endif
72 #endif
73 #if !HAVE_STRCHR && !defined _LIBC
74 # ifndef strchr
75 # define strchr index
76 # endif
77 #endif
79 #include "gettextP.h"
81 /* @@ end of prolog @@ */
83 #ifdef _LIBC
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
89 # ifndef mempcpy
90 # define mempcpy __mempcpy
91 # endif
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);
99 #endif
101 #ifndef internal_function
102 # define internal_function
103 #endif
105 /* Some optimizations for glibc. */
106 #ifdef _LIBC
107 # define FEOF(fp) feof_unlocked (fp)
108 # define FGETS(buf, n, fp) fgets_unlocked (buf, n, fp)
109 #else
110 # define FEOF(fp) feof (fp)
111 # define FGETS(buf, n, fp) fgets (buf, n, fp)
112 #endif
114 /* For those losing systems which don't have `alloca' we have to add
115 some additional code emulating it. */
116 #ifdef HAVE_ALLOCA
117 # define freea(p) /* nothing */
118 #else
119 # define alloca(n) malloc (n)
120 # define freea(p) free (p)
121 #endif
123 #if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
124 # undef fgets
125 # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
126 #endif
127 #if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
128 # undef feof
129 # define feof(s) feof_unlocked (s)
130 #endif
133 struct alias_map
135 const char *alias;
136 const char *value;
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;
144 static size_t nmap;
145 static size_t maxmap;
148 /* Prototypes for local functions. */
149 static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
150 internal_function;
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));
156 const char *
157 _nl_expand_alias (name)
158 const char *name;
160 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
161 struct alias_map *retval;
162 const char *result = NULL;
163 size_t added;
165 #ifdef _LIBC
166 __libc_lock_lock (lock);
167 #endif
171 struct alias_map item;
173 item.alias = name;
175 if (nmap > 0)
176 retval = (struct alias_map *) bsearch (&item, map, nmap,
177 sizeof (struct alias_map),
178 (int (*) PARAMS ((const void *,
179 const void *))
180 ) alias_compare);
181 else
182 retval = NULL;
184 /* We really found an alias. Return the value. */
185 if (retval != NULL)
187 result = retval->value;
188 break;
191 /* Perhaps we can find another alias file. */
192 added = 0;
193 while (added == 0 && locale_alias_path[0] != '\0')
195 const char *start;
197 while (locale_alias_path[0] == ':')
198 ++locale_alias_path;
199 start = locale_alias_path;
201 while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
202 ++locale_alias_path;
204 if (start < locale_alias_path)
205 added = read_alias_file (start, locale_alias_path - start);
208 while (added != 0);
210 #ifdef _LIBC
211 __libc_lock_unlock (lock);
212 #endif
214 return result;
218 static size_t
219 internal_function
220 read_alias_file (fname, fname_len)
221 const char *fname;
222 int fname_len;
224 FILE *fp;
225 char *full_fname;
226 size_t added;
227 static const char aliasfile[] = "/locale.alias";
229 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
230 #ifdef HAVE_MEMPCPY
231 mempcpy (mempcpy (full_fname, fname, fname_len),
232 aliasfile, sizeof aliasfile);
233 #else
234 memcpy (full_fname, fname, fname_len);
235 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
236 #endif
238 fp = fopen (full_fname, "r");
239 freea (full_fname);
240 if (fp == NULL)
241 return 0;
243 #ifdef HAVE___FSETLOCKING
244 /* No threads present. */
245 __fsetlocking (fp, FSETLOCKING_BYCALLER);
246 #endif
248 added = 0;
249 while (!FEOF (fp))
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
254 be that long
256 char buf[BUFSIZ];
257 char *alias;
258 char *value;
259 char *cp;
261 if (FGETS (buf, sizeof buf, fp) == NULL)
262 /* EOF reached. */
263 break;
265 /* Possibly not the whole line fits into the buffer. Ignore
266 the rest of the line. */
267 if (strchr (buf, '\n') == NULL)
269 char altbuf[BUFSIZ];
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. */
274 break;
275 while (strchr (altbuf, '\n') == NULL);
278 cp = buf;
279 /* Ignore leading white space. */
280 while (isspace (cp[0]))
281 ++cp;
283 /* A leading '#' signals a comment line. */
284 if (cp[0] != '\0' && cp[0] != '#')
286 alias = cp++;
287 while (cp[0] != '\0' && !isspace (cp[0]))
288 ++cp;
289 /* Terminate alias name. */
290 if (cp[0] != '\0')
291 *cp++ = '\0';
293 /* Now look for the beginning of the value. */
294 while (isspace (cp[0]))
295 ++cp;
297 if (cp[0] != '\0')
299 size_t alias_len;
300 size_t value_len;
302 value = cp++;
303 while (cp[0] != '\0' && !isspace (cp[0]))
304 ++cp;
305 /* Terminate value. */
306 if (cp[0] == '\n')
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. */
311 *cp++ = '\0';
312 *cp = '\n';
314 else if (cp[0] != '\0')
315 *cp++ = '\0';
317 if (nmap >= maxmap)
318 if (__builtin_expect (extend_alias_table (), 0))
319 return added;
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)
332 return added;
334 if (__builtin_expect (string_space != new_pool, 0))
336 size_t i;
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],
350 alias, alias_len);
351 string_space_act += alias_len;
353 map[nmap].value = memcpy (&string_space[string_space_act],
354 value, value_len);
355 string_space_act += value_len;
357 ++nmap;
358 ++added;
363 /* Should we test for ferror()? I think we have to silently ignore
364 errors. --drepper */
365 fclose (fp);
367 if (added > 0)
368 qsort (map, nmap, sizeof (struct alias_map),
369 (int (*) PARAMS ((const void *, const void *))) alias_compare);
371 return added;
375 static int
376 extend_alias_table ()
378 size_t new_size;
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)));
384 if (new_map == NULL)
385 /* Simply don't extend: we don't have any more core. */
386 return -1;
388 map = new_map;
389 maxmap = new_size;
390 return 0;
394 #ifdef _LIBC
395 static void __attribute__ ((unused))
396 free_mem (void)
398 if (string_space != NULL)
399 free (string_space);
400 if (map != NULL)
401 free (map);
403 text_set_element (__libc_subfreeres, free_mem);
404 #endif
407 static int
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);
414 #else
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;
419 if (p1 == p2)
420 return 0;
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;
428 if (c1 == '\0')
429 break;
430 ++p1;
431 ++p2;
433 while (c1 == c2);
435 return c1 - c2;
436 #endif