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. */
25 #include <sys/types.h>
28 # define alloca __builtin_alloca
29 # define HAVE_ALLOCA 1
31 # if defined HAVE_ALLOCA_H || defined _LIBC
44 #if defined STDC_HEADERS || defined _LIBC
55 #if defined HAVE_STRING_H || defined _LIBC
57 # define _GNU_SOURCE 1
63 # define memcpy(Dst, Src, Num) bcopy (Src, Dst, Num)
66 #if !HAVE_STRCHR && !defined _LIBC
75 /* @@ end of prolog @@ */
78 /* Rename the non ANSI C functions. This is required by the standard
79 because some ANSI C functions will require linking with this object
80 file and the name space must not be polluted. */
81 # define strcasecmp __strcasecmp
84 # define mempcpy __mempcpy
86 # define HAVE_MEMPCPY 1
88 /* We need locking here since we can be called from different places. */
89 # include <bits/libc-lock.h>
91 __libc_lock_define_initialized (static, lock);
94 #ifndef internal_function
95 # define internal_function
98 /* For those loosing systems which don't have `alloca' we have to add
99 some additional code emulating it. */
101 /* Nothing has to be done. */
102 # define ADD_BLOCK(list, address) /* nothing */
103 # define FREE_BLOCKS(list) /* nothing */
108 struct block_list *next;
110 # define ADD_BLOCK(list, addr) \
112 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
113 /* If we cannot get a free block we cannot add the new element to \
115 if (newp != NULL) { \
116 newp->address = (addr); \
117 newp->next = (list); \
121 # define FREE_BLOCKS(list) \
123 while (list != NULL) { \
124 struct block_list *old = list; \
130 # define alloca(size) (malloc (size))
131 #endif /* have alloca */
133 #if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
135 # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
137 #if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
139 # define feof(s) feof_unlocked (s)
150 static char *string_space = NULL;
151 static size_t string_space_act = 0;
152 static size_t string_space_max = 0;
153 static struct alias_map *map;
154 static size_t nmap = 0;
155 static size_t maxmap = 0;
158 /* Prototypes for local functions. */
159 static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
161 static void extend_alias_table PARAMS ((void));
162 static int alias_compare PARAMS ((const struct alias_map *map1,
163 const struct alias_map *map2));
167 _nl_expand_alias (name)
170 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
171 struct alias_map *retval;
172 const char *result = NULL;
176 __libc_lock_lock (lock);
181 struct alias_map item;
186 retval = (struct alias_map *) bsearch (&item, map, nmap,
187 sizeof (struct alias_map),
188 (int (*) PARAMS ((const void *,
194 /* We really found an alias. Return the value. */
197 result = retval->value;
201 /* Perhaps we can find another alias file. */
203 while (added == 0 && locale_alias_path[0] != '\0')
207 while (locale_alias_path[0] == ':')
209 start = locale_alias_path;
211 while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
214 if (start < locale_alias_path)
215 added = read_alias_file (start, locale_alias_path - start);
221 __libc_lock_unlock (lock);
230 read_alias_file (fname, fname_len)
235 struct block_list *block_list = NULL;
240 static const char aliasfile[] = "/locale.alias";
242 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
243 ADD_BLOCK (block_list, full_fname);
245 mempcpy (mempcpy (full_fname, fname, fname_len),
246 aliasfile, sizeof aliasfile);
248 memcpy (full_fname, fname, fname_len);
249 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
252 fp = fopen (full_fname, "r");
255 FREE_BLOCKS (block_list);
262 /* It is a reasonable approach to use a fix buffer here because
263 a) we are only interested in the first two fields
264 b) these fields must be usable as file names and so must not
272 if (fgets (buf, sizeof buf, fp) == NULL)
276 /* Possibly not the whole line fits into the buffer. Ignore
277 the rest of the line. */
278 if (strchr (buf, '\n') == NULL)
282 if (fgets (altbuf, sizeof altbuf, fp) == NULL)
283 /* Make sure the inner loop will be left. The outer loop
284 will exit at the `feof' test. */
286 while (strchr (altbuf, '\n') == NULL);
290 /* Ignore leading white space. */
291 while (isspace (cp[0]))
294 /* A leading '#' signals a comment line. */
295 if (cp[0] != '\0' && cp[0] != '#')
298 while (cp[0] != '\0' && !isspace (cp[0]))
300 /* Terminate alias name. */
304 /* Now look for the beginning of the value. */
305 while (isspace (cp[0]))
314 while (cp[0] != '\0' && !isspace (cp[0]))
316 /* Terminate value. */
319 /* This has to be done to make the following test
320 for the end of line possible. We are looking for
321 the terminating '\n' which do not overwrite here. */
325 else if (cp[0] != '\0')
329 extend_alias_table ();
331 alias_len = strlen (alias) + 1;
332 value_len = strlen (value) + 1;
334 if (string_space_act + alias_len + value_len > string_space_max)
336 /* Increase size of memory pool. */
337 size_t new_size = (string_space_max
338 + (alias_len + value_len > 1024
339 ? alias_len + value_len : 1024));
340 char *new_pool = (char *) realloc (string_space, new_size);
341 if (new_pool == NULL)
343 FREE_BLOCKS (block_list);
346 string_space = new_pool;
347 string_space_max = new_size;
350 map[nmap].alias = memcpy (&string_space[string_space_act],
352 string_space_act += alias_len;
354 map[nmap].value = memcpy (&string_space[string_space_act],
356 string_space_act += value_len;
364 /* Should we test for ferror()? I think we have to silently ignore
369 qsort (map, nmap, sizeof (struct alias_map),
370 (int (*) PARAMS ((const void *, const void *))) alias_compare);
372 FREE_BLOCKS (block_list);
378 extend_alias_table ()
381 struct alias_map *new_map;
383 new_size = maxmap == 0 ? 100 : 2 * maxmap;
384 new_map = (struct alias_map *) realloc (map, (new_size
385 * sizeof (struct alias_map)));
387 /* Simply don't extend: we don't have any more core. */
396 static void __attribute__ ((unused))
399 if (string_space != NULL)
404 text_set_element (__libc_subfreeres, free_mem);
409 alias_compare (map1, map2)
410 const struct alias_map *map1;
411 const struct alias_map *map2;
413 #if defined _LIBC || defined HAVE_STRCASECMP
414 return strcasecmp (map1->alias, map2->alias);
416 const unsigned char *p1 = (const unsigned char *) map1->alias;
417 const unsigned char *p2 = (const unsigned char *) map2->alias;
418 unsigned char c1, c2;
425 /* I know this seems to be odd but the tolower() function in
426 some systems libc cannot handle nonalpha characters. */
427 c1 = isupper (*p1) ? tolower (*p1) : *p1;
428 c2 = isupper (*p2) ? tolower (*p2) : *p2;