1 /* Handle aliases for locale names.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program 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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 # define _GNU_SOURCE 1
29 #include <sys/types.h>
32 # define alloca __builtin_alloca
33 # define HAVE_ALLOCA 1
35 # if defined HAVE_ALLOCA_H || defined _LIBC
48 #if defined STDC_HEADERS || defined _LIBC
59 #if defined HAVE_STRING_H || defined _LIBC
64 # define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
67 #if !HAVE_STRCHR && !defined _LIBC
76 /* @@ end of prolog @@ */
79 /* Rename the non ANSI C functions. This is required by the standard
80 because some ANSI C functions will require linking with this object
81 file and the name space must not be polluted. */
82 # define strcasecmp __strcasecmp
84 # define mempcpy __mempcpy
85 # define HAVE_MEMPCPY 1
87 /* We need locking here since we can be called from different places. */
88 # include <bits/libc-lock.h>
90 __libc_lock_define_initialized (static, lock);
94 /* For those loosing systems which don't have `alloca' we have to add
95 some additional code emulating it. */
97 /* Nothing has to be done. */
98 # define ADD_BLOCK(list, address) /* nothing */
99 # define FREE_BLOCKS(list) /* nothing */
104 struct block_list *next;
106 # define ADD_BLOCK(list, addr) \
108 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
109 /* If we cannot get a free block we cannot add the new element to \
111 if (newp != NULL) { \
112 newp->address = (addr); \
113 newp->next = (list); \
117 # define FREE_BLOCKS(list) \
119 while (list != NULL) { \
120 struct block_list *old = list; \
126 # define alloca(size) (malloc (size))
127 #endif /* have alloca */
137 static char *string_space = NULL;
138 static size_t string_space_act = 0;
139 static size_t string_space_max = 0;
140 static struct alias_map *map;
141 static size_t nmap = 0;
142 static size_t maxmap = 0;
145 /* Prototypes for local functions. */
146 static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
148 static void extend_alias_table PARAMS ((void));
149 static int alias_compare PARAMS ((const struct alias_map *map1,
150 const struct alias_map *map2));
154 _nl_expand_alias (name)
157 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
158 struct alias_map *retval;
159 const char *result = NULL;
163 __libc_lock_lock (lock);
168 struct alias_map item;
173 retval = (struct alias_map *) bsearch (&item, map, nmap,
174 sizeof (struct alias_map),
175 (int (*) PARAMS ((const void *,
181 /* We really found an alias. Return the value. */
184 result = retval->value;
188 /* Perhaps we can find another alias file. */
190 while (added == 0 && locale_alias_path[0] != '\0')
194 while (locale_alias_path[0] == ':')
196 start = locale_alias_path;
198 while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
201 if (start < locale_alias_path)
202 added = read_alias_file (start, locale_alias_path - start);
208 __libc_lock_unlock (lock);
217 read_alias_file (fname, fname_len)
222 struct block_list *block_list = NULL;
227 static const char aliasfile[] = "/locale.alias";
229 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
230 ADD_BLOCK (block_list, full_fname);
231 memcpy (full_fname, fname, fname_len);
232 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
234 fp = fopen (full_fname, "r");
237 FREE_BLOCKS (block_list);
244 /* It is a reasonable approach to use a fix buffer here because
245 a) we are only interested in the first two fields
246 b) these fields must be usable as file names and so must not
249 unsigned char buf[BUFSIZ];
250 unsigned char *alias;
251 unsigned char *value;
254 if (fgets ((char *)buf, sizeof buf, fp) == NULL)
258 /* Possibly not the whole line fits into the buffer. Ignore
259 the rest of the line. */
260 if (strchr ((char *)buf, '\n') == NULL)
264 if (fgets (altbuf, sizeof altbuf, fp) == NULL)
265 /* Make sure the inner loop will be left. The outer loop
266 will exit at the `feof' test. */
268 while (strchr (altbuf, '\n') == NULL);
272 /* Ignore leading white space. */
273 while (isspace (cp[0]))
276 /* A leading '#' signals a comment line. */
277 if (cp[0] != '\0' && cp[0] != '#')
280 while (cp[0] != '\0' && !isspace (cp[0]))
282 /* Terminate alias name. */
286 /* Now look for the beginning of the value. */
287 while (isspace (cp[0]))
296 while (cp[0] != '\0' && !isspace (cp[0]))
298 /* Terminate value. */
301 /* This has to be done to make the following test
302 for the end of line possible. We are looking for
303 the terminating '\n' which do not overwrite here. */
307 else if (cp[0] != '\0')
311 extend_alias_table ();
313 alias_len = strlen ((char *)alias) + 1;
314 value_len = strlen ((char *)value) + 1;
316 if (string_space_act + alias_len + value_len > string_space_max)
318 /* Increase size of memory pool. */
319 size_t new_size = (string_space_max
320 + (alias_len + value_len > 1024
321 ? alias_len + value_len : 1024));
322 char *new_pool = (char *) realloc (string_space, new_size);
323 if (new_pool == NULL)
325 FREE_BLOCKS (block_list);
328 string_space = new_pool;
329 string_space_max = new_size;
332 memcpy (&string_space[string_space_act], alias, alias_len);
333 map[nmap].alias = &string_space[string_space_act];
334 string_space_act += alias_len;
336 memcpy (&string_space[string_space_act], value, value_len);
337 map[nmap].value = &string_space[string_space_act];
338 string_space_act += value_len;
346 /* Should we test for ferror()? I think we have to silently ignore
351 qsort (map, nmap, sizeof (struct alias_map),
352 (int (*) PARAMS ((const void *, const void *))) alias_compare);
354 FREE_BLOCKS (block_list);
360 extend_alias_table ()
363 struct alias_map *new_map;
365 new_size = maxmap == 0 ? 100 : 2 * maxmap;
366 new_map = (struct alias_map *) realloc (map, (new_size
367 * sizeof (struct alias_map)));
369 /* Simply don't extend: we don't have any more core. */
378 static void __attribute__ ((unused))
381 if (string_space != NULL)
386 text_set_element (__libc_subfreeres, free_mem);
391 alias_compare (map1, map2)
392 const struct alias_map *map1;
393 const struct alias_map *map2;
395 #if defined _LIBC || defined HAVE_STRCASECMP
396 return strcasecmp (map1->alias, map2->alias);
398 const unsigned char *p1 = (const unsigned char *) map1->alias;
399 const unsigned char *p2 = (const unsigned char *) map2->alias;
400 unsigned char c1, c2;
407 /* I know this seems to be odd but the tolower() function in
408 some systems libc cannot handle nonalpha characters. */
409 c1 = isupper (*p1) ? tolower (*p1) : *p1;
410 c2 = isupper (*p2) ? tolower (*p2) : *p2;