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
83 # define mempcpy __mempcpy
84 # define HAVE_MEMPCPY 1
86 /* We need locking here since we can be called from different places. */
87 # include <bits/libc-lock.h>
89 __libc_lock_define_initialized (static, lock);
93 /* For those loosing systems which don't have `alloca' we have to add
94 some additional code emulating it. */
96 /* Nothing has to be done. */
97 # define ADD_BLOCK(list, address) /* nothing */
98 # define FREE_BLOCKS(list) /* nothing */
103 struct block_list *next;
105 # define ADD_BLOCK(list, addr) \
107 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
108 /* If we cannot get a free block we cannot add the new element to \
110 if (newp != NULL) { \
111 newp->address = (addr); \
112 newp->next = (list); \
116 # define FREE_BLOCKS(list) \
118 while (list != NULL) { \
119 struct block_list *old = list; \
125 # define alloca(size) (malloc (size))
126 #endif /* have alloca */
136 static char *string_space = NULL;
137 static size_t string_space_act = 0;
138 static size_t string_space_max = 0;
139 static struct alias_map *map;
140 static size_t nmap = 0;
141 static size_t maxmap = 0;
144 /* Prototypes for local functions. */
145 static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
147 static void extend_alias_table PARAMS ((void));
148 static int alias_compare PARAMS ((const struct alias_map *map1,
149 const struct alias_map *map2));
153 _nl_expand_alias (name)
156 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
157 struct alias_map *retval;
158 const char *result = NULL;
162 __libc_lock_lock (lock);
167 struct alias_map item;
172 retval = (struct alias_map *) bsearch (&item, map, nmap,
173 sizeof (struct alias_map),
174 (int (*) PARAMS ((const void *,
180 /* We really found an alias. Return the value. */
183 result = retval->value;
187 /* Perhaps we can find another alias file. */
189 while (added == 0 && locale_alias_path[0] != '\0')
193 while (locale_alias_path[0] == ':')
195 start = locale_alias_path;
197 while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
200 if (start < locale_alias_path)
201 added = read_alias_file (start, locale_alias_path - start);
207 __libc_lock_unlock (lock);
216 read_alias_file (fname, fname_len)
221 struct block_list *block_list = NULL;
226 static const char aliasfile[] = "/locale.alias";
228 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
229 ADD_BLOCK (block_list, full_fname);
231 mempcpy (mempcpy (full_fname, fname, fname_len),
232 aliasfile, sizeof aliasfile);
234 memcpy (full_fname, fname, fname_len);
235 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
238 fp = fopen (full_fname, "r");
241 FREE_BLOCKS (block_list);
248 /* It is a reasonable approach to use a fix buffer here because
249 a) we are only interested in the first two fields
250 b) these fields must be usable as file names and so must not
253 unsigned char buf[BUFSIZ];
254 unsigned char *alias;
255 unsigned char *value;
258 if (fgets (buf, sizeof buf, fp) == NULL)
262 /* Possibly not the whole line fits into the buffer. Ignore
263 the rest of the line. */
264 if (strchr (buf, '\n') == NULL)
268 if (fgets (altbuf, sizeof altbuf, fp) == NULL)
269 /* Make sure the inner loop will be left. The outer loop
270 will exit at the `feof' test. */
272 while (strchr (altbuf, '\n') == NULL);
276 /* Ignore leading white space. */
277 while (isspace (cp[0]))
280 /* A leading '#' signals a comment line. */
281 if (cp[0] != '\0' && cp[0] != '#')
284 while (cp[0] != '\0' && !isspace (cp[0]))
286 /* Terminate alias name. */
290 /* Now look for the beginning of the value. */
291 while (isspace (cp[0]))
300 while (cp[0] != '\0' && !isspace (cp[0]))
302 /* Terminate value. */
305 /* This has to be done to make the following test
306 for the end of line possible. We are looking for
307 the terminating '\n' which do not overwrite here. */
311 else if (cp[0] != '\0')
315 extend_alias_table ();
317 alias_len = strlen (alias) + 1;
318 value_len = strlen (value) + 1;
320 if (string_space_act + alias_len + value_len > string_space_max)
322 /* Increase size of memory pool. */
323 size_t new_size = (string_space_max
324 + (alias_len + value_len > 1024
325 ? alias_len + value_len : 1024));
326 char *new_pool = (char *) realloc (string_space, new_size);
327 if (new_pool == NULL)
329 FREE_BLOCKS (block_list);
332 string_space = new_pool;
333 string_space_max = new_size;
336 map[nmap].alias = memcpy (&string_space[string_space_act],
338 string_space_act += alias_len;
340 map[nmap].value = memcpy (&string_space[string_space_act],
342 string_space_act += value_len;
350 /* Should we test for ferror()? I think we have to silently ignore
355 qsort (map, nmap, sizeof (struct alias_map),
356 (int (*) PARAMS ((const void *, const void *))) alias_compare);
358 FREE_BLOCKS (block_list);
364 extend_alias_table ()
367 struct alias_map *new_map;
369 new_size = maxmap == 0 ? 100 : 2 * maxmap;
370 new_map = (struct alias_map *) realloc (map, (new_size
371 * sizeof (struct alias_map)));
373 /* Simply don't extend: we don't have any more core. */
382 static void __attribute__ ((unused))
385 if (string_space != NULL)
390 text_set_element (__libc_subfreeres, free_mem);
395 alias_compare (map1, map2)
396 const struct alias_map *map1;
397 const struct alias_map *map2;
399 #if defined _LIBC || defined HAVE_STRCASECMP
400 return strcasecmp (map1->alias, map2->alias);
402 const unsigned char *p1 = (const unsigned char *) map1->alias;
403 const unsigned char *p2 = (const unsigned char *) map2->alias;
404 unsigned char c1, c2;
411 /* I know this seems to be odd but the tolower() function in
412 some systems libc cannot handle nonalpha characters. */
413 c1 = isupper (*p1) ? tolower (*p1) : *p1;
414 c2 = isupper (*p2) ? tolower (*p2) : *p2;