Update.
[glibc.git] / intl / localealias.c
blob01fbe04f2e16fc934bdfe7e5195f3fceff3bfe92
1 /* Handle aliases for locale names.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of the GNU C Library. Its master source is NOT part of
5 the C library, however.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <sys/types.h>
30 #ifdef __GNUC__
31 # define alloca __builtin_alloca
32 # define HAVE_ALLOCA 1
33 #else
34 # if defined HAVE_ALLOCA_H || defined _LIBC
35 # include <alloca.h>
36 # else
37 # ifdef _AIX
38 #pragma alloca
39 # else
40 # ifndef alloca
41 char *alloca ();
42 # endif
43 # endif
44 # endif
45 #endif
47 #if defined STDC_HEADERS || defined _LIBC
48 # include <stdlib.h>
49 #else
50 char *getenv ();
51 # ifdef HAVE_MALLOC_H
52 # include <malloc.h>
53 # else
54 void free ();
55 # endif
56 #endif
58 #if defined HAVE_STRING_H || defined _LIBC
59 # ifndef _GNU_SOURCE
60 # define _GNU_SOURCE 1
61 # endif
62 # include <string.h>
63 #else
64 # include <strings.h>
65 # ifndef memcpy
66 # define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
67 # endif
68 #endif
69 #if !HAVE_STRCHR && !defined _LIBC
70 # ifndef strchr
71 # define strchr index
72 # endif
73 #endif
75 #include "gettext.h"
76 #include "gettextP.h"
78 /* @@ end of prolog @@ */
80 #ifdef _LIBC
81 /* Rename the non ANSI C functions. This is required by the standard
82 because some ANSI C functions will require linking with this object
83 file and the name space must not be polluted. */
84 # define strcasecmp __strcasecmp
86 # define mempcpy __mempcpy
87 # define HAVE_MEMPCPY 1
89 /* We need locking here since we can be called from different places. */
90 # include <bits/libc-lock.h>
92 __libc_lock_define_initialized (static, lock);
93 #endif
95 #ifndef internal_function
96 # define internal_function
97 #endif
99 /* For those loosing systems which don't have `alloca' we have to add
100 some additional code emulating it. */
101 #ifdef HAVE_ALLOCA
102 /* Nothing has to be done. */
103 # define ADD_BLOCK(list, address) /* nothing */
104 # define FREE_BLOCKS(list) /* nothing */
105 #else
106 struct block_list
108 void *address;
109 struct block_list *next;
111 # define ADD_BLOCK(list, addr) \
112 do { \
113 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
114 /* If we cannot get a free block we cannot add the new element to \
115 the list. */ \
116 if (newp != NULL) { \
117 newp->address = (addr); \
118 newp->next = (list); \
119 (list) = newp; \
121 } while (0)
122 # define FREE_BLOCKS(list) \
123 do { \
124 while (list != NULL) { \
125 struct block_list *old = list; \
126 list = list->next; \
127 free (old); \
129 } while (0)
130 # undef alloca
131 # define alloca(size) (malloc (size))
132 #endif /* have alloca */
134 #if defined _LIBC || defined HAVE_FGETS_UNLOCKED
135 # undef fgets
136 # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
137 #endif
138 #if defined _LIBC || defined HAVE_FEOF_UNLOCKED
139 # undef feof
140 # define feof(s) feof_unlocked (s)
141 #endif
144 struct alias_map
146 const char *alias;
147 const char *value;
151 static char *string_space = NULL;
152 static size_t string_space_act = 0;
153 static size_t string_space_max = 0;
154 static struct alias_map *map;
155 static size_t nmap = 0;
156 static size_t maxmap = 0;
159 /* Prototypes for local functions. */
160 static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
161 internal_function;
162 static void extend_alias_table PARAMS ((void));
163 static int alias_compare PARAMS ((const struct alias_map *map1,
164 const struct alias_map *map2));
167 const char *
168 _nl_expand_alias (name)
169 const char *name;
171 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
172 struct alias_map *retval;
173 const char *result = NULL;
174 size_t added;
176 #ifdef _LIBC
177 __libc_lock_lock (lock);
178 #endif
182 struct alias_map item;
184 item.alias = name;
186 if (nmap > 0)
187 retval = (struct alias_map *) bsearch (&item, map, nmap,
188 sizeof (struct alias_map),
189 (int (*) PARAMS ((const void *,
190 const void *))
191 ) alias_compare);
192 else
193 retval = NULL;
195 /* We really found an alias. Return the value. */
196 if (retval != NULL)
198 result = retval->value;
199 break;
202 /* Perhaps we can find another alias file. */
203 added = 0;
204 while (added == 0 && locale_alias_path[0] != '\0')
206 const char *start;
208 while (locale_alias_path[0] == ':')
209 ++locale_alias_path;
210 start = locale_alias_path;
212 while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
213 ++locale_alias_path;
215 if (start < locale_alias_path)
216 added = read_alias_file (start, locale_alias_path - start);
219 while (added != 0);
221 #ifdef _LIBC
222 __libc_lock_unlock (lock);
223 #endif
225 return result;
229 static size_t
230 internal_function
231 read_alias_file (fname, fname_len)
232 const char *fname;
233 int fname_len;
235 #ifndef HAVE_ALLOCA
236 struct block_list *block_list = NULL;
237 #endif
238 FILE *fp;
239 char *full_fname;
240 size_t added;
241 static const char aliasfile[] = "/locale.alias";
243 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
244 ADD_BLOCK (block_list, full_fname);
245 #ifdef HAVE_MEMPCPY
246 mempcpy (mempcpy (full_fname, fname, fname_len),
247 aliasfile, sizeof aliasfile);
248 #else
249 memcpy (full_fname, fname, fname_len);
250 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
251 #endif
253 fp = fopen (full_fname, "r");
254 if (fp == NULL)
256 FREE_BLOCKS (block_list);
257 return 0;
260 added = 0;
261 while (!feof (fp))
263 /* It is a reasonable approach to use a fix buffer here because
264 a) we are only interested in the first two fields
265 b) these fields must be usable as file names and so must not
266 be that long
268 char buf[BUFSIZ];
269 char *alias;
270 char *value;
271 char *cp;
273 if (fgets (buf, sizeof buf, fp) == NULL)
274 /* EOF reached. */
275 break;
277 /* Possibly not the whole line fits into the buffer. Ignore
278 the rest of the line. */
279 if (strchr (buf, '\n') == NULL)
281 char altbuf[BUFSIZ];
283 if (fgets (altbuf, sizeof altbuf, fp) == NULL)
284 /* Make sure the inner loop will be left. The outer loop
285 will exit at the `feof' test. */
286 break;
287 while (strchr (altbuf, '\n') == NULL);
290 cp = buf;
291 /* Ignore leading white space. */
292 while (isspace (cp[0]))
293 ++cp;
295 /* A leading '#' signals a comment line. */
296 if (cp[0] != '\0' && cp[0] != '#')
298 alias = cp++;
299 while (cp[0] != '\0' && !isspace (cp[0]))
300 ++cp;
301 /* Terminate alias name. */
302 if (cp[0] != '\0')
303 *cp++ = '\0';
305 /* Now look for the beginning of the value. */
306 while (isspace (cp[0]))
307 ++cp;
309 if (cp[0] != '\0')
311 size_t alias_len;
312 size_t value_len;
314 value = cp++;
315 while (cp[0] != '\0' && !isspace (cp[0]))
316 ++cp;
317 /* Terminate value. */
318 if (cp[0] == '\n')
320 /* This has to be done to make the following test
321 for the end of line possible. We are looking for
322 the terminating '\n' which do not overwrite here. */
323 *cp++ = '\0';
324 *cp = '\n';
326 else if (cp[0] != '\0')
327 *cp++ = '\0';
329 if (nmap >= maxmap)
330 extend_alias_table ();
332 alias_len = strlen (alias) + 1;
333 value_len = strlen (value) + 1;
335 if (string_space_act + alias_len + value_len > string_space_max)
337 /* Increase size of memory pool. */
338 size_t new_size = (string_space_max
339 + (alias_len + value_len > 1024
340 ? alias_len + value_len : 1024));
341 char *new_pool = (char *) realloc (string_space, new_size);
342 if (new_pool == NULL)
344 FREE_BLOCKS (block_list);
345 return added;
347 string_space = new_pool;
348 string_space_max = new_size;
351 map[nmap].alias = memcpy (&string_space[string_space_act],
352 alias, alias_len);
353 string_space_act += alias_len;
355 map[nmap].value = memcpy (&string_space[string_space_act],
356 value, value_len);
357 string_space_act += value_len;
359 ++nmap;
360 ++added;
365 /* Should we test for ferror()? I think we have to silently ignore
366 errors. --drepper */
367 fclose (fp);
369 if (added > 0)
370 qsort (map, nmap, sizeof (struct alias_map),
371 (int (*) PARAMS ((const void *, const void *))) alias_compare);
373 FREE_BLOCKS (block_list);
374 return added;
378 static void
379 extend_alias_table ()
381 size_t new_size;
382 struct alias_map *new_map;
384 new_size = maxmap == 0 ? 100 : 2 * maxmap;
385 new_map = (struct alias_map *) realloc (map, (new_size
386 * sizeof (struct alias_map)));
387 if (new_map == NULL)
388 /* Simply don't extend: we don't have any more core. */
389 return;
391 map = new_map;
392 maxmap = new_size;
396 #ifdef _LIBC
397 static void __attribute__ ((unused))
398 free_mem (void)
400 if (string_space != NULL)
401 free (string_space);
402 if (map != NULL)
403 free (map);
405 text_set_element (__libc_subfreeres, free_mem);
406 #endif
409 static int
410 alias_compare (map1, map2)
411 const struct alias_map *map1;
412 const struct alias_map *map2;
414 #if defined _LIBC || defined HAVE_STRCASECMP
415 return strcasecmp (map1->alias, map2->alias);
416 #else
417 const unsigned char *p1 = (const unsigned char *) map1->alias;
418 const unsigned char *p2 = (const unsigned char *) map2->alias;
419 unsigned char c1, c2;
421 if (p1 == p2)
422 return 0;
426 /* I know this seems to be odd but the tolower() function in
427 some systems libc cannot handle nonalpha characters. */
428 c1 = isupper (*p1) ? tolower (*p1) : *p1;
429 c2 = isupper (*p2) ? tolower (*p2) : *p2;
430 if (c1 == '\0')
431 break;
432 ++p1;
433 ++p2;
435 while (c1 == c2);
437 return c1 - c2;
438 #endif