(_dl_map_object): Don't get LD_LIBRARY_PATH envvar, use global variable
[glibc.git] / intl / localealias.c
blob94673de35b6c0ba9dc73d9b7f67d0e464115b66d
1 /* localealias.c -- handle aliases for locale names
2 Copyright (C) 1995, 1996, 1997 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. The master source lives in /gd/gnu/lib.
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
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, 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 #endif
66 #if !HAVE_STRCHR && !defined _LIBC
67 # ifndef strchr
68 # define strchr index
69 # endif
70 #endif
72 #include "gettext.h"
73 #include "gettextP.h"
75 /* @@ end of prolog @@ */
77 #ifdef _LIBC
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
82 #endif
85 /* For those loosing systems which don't have `alloca' we have to add
86 some additional code emulating it. */
87 #ifdef HAVE_ALLOCA
88 /* Nothing has to be done. */
89 # define ADD_BLOCK(list, address) /* nothing */
90 # define FREE_BLOCKS(list) /* nothing */
91 #else
92 struct block_list
94 void *address;
95 struct block_list *next;
97 # define ADD_BLOCK(list, addr) \
98 do { \
99 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
100 /* If we cannot get a free block we cannot add the new element to \
101 the list. */ \
102 if (newp != NULL) { \
103 newp->address = (addr); \
104 newp->next = (list); \
105 (list) = newp; \
107 } while (0)
108 # define FREE_BLOCKS(list) \
109 do { \
110 while (list != NULL) { \
111 struct block_list *old = list; \
112 list = list->next; \
113 free (old); \
115 } while (0)
116 # undef alloca
117 # define alloca(size) (malloc (size))
118 #endif /* have alloca */
121 struct alias_map
123 const char *alias;
124 const char *value;
128 static struct alias_map *map;
129 static size_t nmap = 0;
130 static size_t maxmap = 0;
133 /* Prototypes for local functions. */
134 static size_t read_alias_file PARAMS ((const char *fname, int fname_len));
135 static void extend_alias_table PARAMS ((void));
136 static int alias_compare PARAMS ((const struct alias_map *map1,
137 const struct alias_map *map2));
140 const char *
141 _nl_expand_alias (name)
142 const char *name;
144 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
145 struct alias_map *retval;
146 size_t added;
150 struct alias_map item;
152 item.alias = name;
154 if (nmap > 0)
155 retval = (struct alias_map *) bsearch (&item, map, nmap,
156 sizeof (struct alias_map),
157 (int (*) PARAMS ((const void *,
158 const void *))
159 ) alias_compare);
160 else
161 retval = NULL;
163 /* We really found an alias. Return the value. */
164 if (retval != NULL)
165 return retval->value;
167 /* Perhaps we can find another alias file. */
168 added = 0;
169 while (added == 0 && locale_alias_path[0] != '\0')
171 const char *start;
173 while (locale_alias_path[0] == ':')
174 ++locale_alias_path;
175 start = locale_alias_path;
177 while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
178 ++locale_alias_path;
180 if (start < locale_alias_path)
181 added = read_alias_file (start, locale_alias_path - start);
184 while (added != 0);
186 return NULL;
190 static size_t
191 read_alias_file (fname, fname_len)
192 const char *fname;
193 int fname_len;
195 #ifndef HAVE_ALLOCA
196 struct block_list *block_list = NULL;
197 #endif
198 FILE *fp;
199 char *full_fname;
200 size_t added;
201 static const char aliasfile[] = "/locale.alias";
203 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
204 ADD_BLOCK (block_list, full_fname);
205 memcpy (full_fname, fname, fname_len);
206 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
208 fp = fopen (full_fname, "r");
209 if (fp == NULL)
211 FREE_BLOCKS (block_list);
212 return 0;
215 added = 0;
216 while (!feof (fp))
218 /* It is a reasonable approach to use a fix buffer here because
219 a) we are only interested in the first two fields
220 b) these fields must be usable as file names and so must not
221 be that long
223 char buf[BUFSIZ];
224 char *alias;
225 char *value;
226 char *cp;
228 if (fgets (buf, BUFSIZ, fp) == NULL)
229 /* EOF reached. */
230 break;
232 /* Possibly not the whole line fits into the buffer. Ignore
233 the rest of the line. */
234 if (strchr (buf, '\n') == NULL)
236 char altbuf[BUFSIZ];
238 if (fgets (altbuf, sizeof altbuf, fp) == NULL)
239 /* Make sure the inner loop will be left. The outer loop
240 will exit at the `feof' test. */
241 break;
242 while (strchr (altbuf, '\n') == NULL);
245 cp = buf;
246 /* Ignore leading white space. */
247 while (isspace (cp[0]))
248 ++cp;
250 /* A leading '#' signals a comment line. */
251 if (cp[0] != '\0' && cp[0] != '#')
253 alias = cp++;
254 while (cp[0] != '\0' && !isspace (cp[0]))
255 ++cp;
256 /* Terminate alias name. */
257 if (cp[0] != '\0')
258 *cp++ = '\0';
260 /* Now look for the beginning of the value. */
261 while (isspace (cp[0]))
262 ++cp;
264 if (cp[0] != '\0')
266 char *tp;
267 size_t len;
269 value = cp++;
270 while (cp[0] != '\0' && !isspace (cp[0]))
271 ++cp;
272 /* Terminate value. */
273 if (cp[0] == '\n')
275 /* This has to be done to make the following test
276 for the end of line possible. We are looking for
277 the terminating '\n' which do not overwrite here. */
278 *cp++ = '\0';
279 *cp = '\n';
281 else if (cp[0] != '\0')
282 *cp++ = '\0';
284 if (nmap >= maxmap)
285 extend_alias_table ();
287 /* We cannot depend on strdup available in the libc. Sigh! */
288 len = strlen (alias) + 1;
289 tp = (char *) malloc (len);
290 if (tp == NULL)
292 FREE_BLOCKS (block_list);
293 return added;
295 memcpy (tp, alias, len);
296 map[nmap].alias = tp;
298 len = strlen (value) + 1;
299 tp = (char *) malloc (len);
300 if (tp == NULL)
302 FREE_BLOCKS (block_list);
303 return added;
305 memcpy (tp, value, len);
306 map[nmap].value = tp;
308 ++nmap;
309 ++added;
314 /* Should we test for ferror()? I think we have to silently ignore
315 errors. --drepper */
316 fclose (fp);
318 if (added > 0)
319 qsort (map, nmap, sizeof (struct alias_map),
320 (int (*) PARAMS ((const void *, const void *))) alias_compare);
322 FREE_BLOCKS (block_list);
323 return added;
327 static void
328 extend_alias_table ()
330 size_t new_size;
331 struct alias_map *new_map;
333 new_size = maxmap == 0 ? 100 : 2 * maxmap;
334 new_map = (struct alias_map *) malloc (new_size
335 * sizeof (struct alias_map));
336 if (new_map == NULL)
337 /* Simply don't extend: we don't have any more core. */
338 return;
340 memcpy (new_map, map, nmap * sizeof (struct alias_map));
342 if (maxmap != 0)
343 free (map);
345 map = new_map;
346 maxmap = new_size;
350 static int
351 alias_compare (map1, map2)
352 const struct alias_map *map1;
353 const struct alias_map *map2;
355 #if defined _LIBC || defined HAVE_STRCASECMP
356 return strcasecmp (map1->alias, map2->alias);
357 #else
358 const unsigned char *p1 = (const unsigned char *) map1->alias;
359 const unsigned char *p2 = (const unsigned char *) map2->alias;
360 unsigned char c1, c2;
362 if (p1 == p2)
363 return 0;
367 /* I know this seems to be odd but the tolower() function in
368 some systems libc cannot handle nonalpha characters. */
369 c1 = isupper (*p1) ? tolower (*p1) : *p1;
370 c2 = isupper (*p2) ? tolower (*p2) : *p2;
371 if (c1 == '\0')
372 break;
373 ++p1;
374 ++p2;
376 while (c1 == c2);
378 return c1 - c2;
379 #endif