(glob_in_dir): Add support for cases insensitive VMS.
[glibc.git] / intl / localealias.c
blob58ed2708aec988b39bee771629533aa1d07f6e68
1 /* 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.
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
85 # define strdup __strdup
87 # define mempcpy __mempcpy
88 # define HAVE_MEMPCPY 1
90 /* We need locking here since we can be called from different palces. */
91 # include <bits/libc-lock.h>
93 __libc_lock_define_initialized (static, lock);
94 #endif
96 #ifndef internal_function
97 # define internal_function
98 #endif
100 /* For those loosing systems which don't have `alloca' we have to add
101 some additional code emulating it. */
102 #ifdef HAVE_ALLOCA
103 /* Nothing has to be done. */
104 # define ADD_BLOCK(list, address) /* nothing */
105 # define FREE_BLOCKS(list) /* nothing */
106 #else
107 struct block_list
109 void *address;
110 struct block_list *next;
112 # define ADD_BLOCK(list, addr) \
113 do { \
114 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
115 /* If we cannot get a free block we cannot add the new element to \
116 the list. */ \
117 if (newp != NULL) { \
118 newp->address = (addr); \
119 newp->next = (list); \
120 (list) = newp; \
122 } while (0)
123 # define FREE_BLOCKS(list) \
124 do { \
125 while (list != NULL) { \
126 struct block_list *old = list; \
127 list = list->next; \
128 free (old); \
130 } while (0)
131 # undef alloca
132 # define alloca(size) (malloc (size))
133 #endif /* have alloca */
136 struct alias_map
138 const char *alias;
139 const char *value;
143 static char *string_space = NULL;
144 static size_t string_space_act = 0;
145 static size_t string_space_max = 0;
146 static struct alias_map *map;
147 static size_t nmap = 0;
148 static size_t maxmap = 0;
151 /* Prototypes for local functions. */
152 static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
153 internal_function;
154 static void extend_alias_table PARAMS ((void));
155 static int alias_compare PARAMS ((const struct alias_map *map1,
156 const struct alias_map *map2));
159 const char *
160 _nl_expand_alias (name)
161 const char *name;
163 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
164 struct alias_map *retval;
165 const char *result = NULL;
166 size_t added;
168 #ifdef _LIBC
169 __libc_lock_lock (lock);
170 #endif
174 struct alias_map item;
176 item.alias = name;
178 if (nmap > 0)
179 retval = (struct alias_map *) bsearch (&item, map, nmap,
180 sizeof (struct alias_map),
181 (int (*) PARAMS ((const void *,
182 const void *))
183 ) alias_compare);
184 else
185 retval = NULL;
187 /* We really found an alias. Return the value. */
188 if (retval != NULL)
190 result = retval->value;
191 break;
194 /* Perhaps we can find another alias file. */
195 added = 0;
196 while (added == 0 && locale_alias_path[0] != '\0')
198 const char *start;
200 while (locale_alias_path[0] == ':')
201 ++locale_alias_path;
202 start = locale_alias_path;
204 while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
205 ++locale_alias_path;
207 if (start < locale_alias_path)
208 added = read_alias_file (start, locale_alias_path - start);
211 while (added != 0);
213 #ifdef _LIBC
214 __libc_lock_unlock (lock);
215 #endif
217 return result;
221 static size_t
222 internal_function
223 read_alias_file (fname, fname_len)
224 const char *fname;
225 int fname_len;
227 #ifndef HAVE_ALLOCA
228 struct block_list *block_list = NULL;
229 #endif
230 FILE *fp;
231 char *full_fname;
232 size_t added;
233 static const char aliasfile[] = "/locale.alias";
235 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
236 ADD_BLOCK (block_list, full_fname);
237 #ifdef HAVE_MEMPCPY
238 mempcpy (mempcpy (full_fname, fname, fname_len),
239 aliasfile, sizeof aliasfile);
240 #else
241 memcpy (full_fname, fname, fname_len);
242 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
243 #endif
245 fp = fopen (full_fname, "r");
246 if (fp == NULL)
248 FREE_BLOCKS (block_list);
249 return 0;
252 added = 0;
253 while (!feof (fp))
255 /* It is a reasonable approach to use a fix buffer here because
256 a) we are only interested in the first two fields
257 b) these fields must be usable as file names and so must not
258 be that long
260 char buf[BUFSIZ];
261 char *alias;
262 char *value;
263 char *cp;
265 if (fgets (buf, sizeof buf, fp) == NULL)
266 /* EOF reached. */
267 break;
269 /* Possibly not the whole line fits into the buffer. Ignore
270 the rest of the line. */
271 if (strchr (buf, '\n') == NULL)
273 char altbuf[BUFSIZ];
275 if (fgets (altbuf, sizeof altbuf, fp) == NULL)
276 /* Make sure the inner loop will be left. The outer loop
277 will exit at the `feof' test. */
278 break;
279 while (strchr (altbuf, '\n') == NULL);
282 cp = buf;
283 /* Ignore leading white space. */
284 while (isspace (cp[0]))
285 ++cp;
287 /* A leading '#' signals a comment line. */
288 if (cp[0] != '\0' && cp[0] != '#')
290 alias = cp++;
291 while (cp[0] != '\0' && !isspace (cp[0]))
292 ++cp;
293 /* Terminate alias name. */
294 if (cp[0] != '\0')
295 *cp++ = '\0';
297 /* Now look for the beginning of the value. */
298 while (isspace (cp[0]))
299 ++cp;
301 if (cp[0] != '\0')
303 char *tp;
304 size_t alias_len;
305 size_t value_len;
307 value = cp++;
308 while (cp[0] != '\0' && !isspace (cp[0]))
309 ++cp;
310 /* Terminate value. */
311 if (cp[0] == '\n')
313 /* This has to be done to make the following test
314 for the end of line possible. We are looking for
315 the terminating '\n' which do not overwrite here. */
316 *cp++ = '\0';
317 *cp = '\n';
319 else if (cp[0] != '\0')
320 *cp++ = '\0';
322 if (nmap >= maxmap)
323 extend_alias_table ();
325 alias_len = strlen (alias) + 1;
326 value_len = strlen (value) + 1;
328 if (string_space_act + alias_len + value_len > string_space_max)
330 /* Increase size of memory pool. */
331 size_t new_size = (string_space_max
332 + (alias_len + value_len > 1024
333 ? alias_len + value_len : 1024));
334 char *new_pool = (char *) realloc (string_space, new_size);
335 if (new_pool == NULL)
337 FREE_BLOCKS (block_list);
338 return added;
340 string_space = new_pool;
341 string_space_max = new_size;
344 map[nmap].alias = memcpy (&string_space[string_space_act],
345 alias, alias_len);
346 string_space_act += alias_len;
348 map[nmap].value = memcpy (&string_space[string_space_act],
349 value, value_len);
350 string_space_act += value_len;
352 ++nmap;
353 ++added;
358 /* Should we test for ferror()? I think we have to silently ignore
359 errors. --drepper */
360 fclose (fp);
362 if (added > 0)
363 qsort (map, nmap, sizeof (struct alias_map),
364 (int (*) PARAMS ((const void *, const void *))) alias_compare);
366 FREE_BLOCKS (block_list);
367 return added;
371 static void
372 extend_alias_table ()
374 size_t new_size;
375 struct alias_map *new_map;
377 new_size = maxmap == 0 ? 100 : 2 * maxmap;
378 new_map = (struct alias_map *) realloc (map, (new_size
379 * sizeof (struct alias_map)));
380 if (new_map == NULL)
381 /* Simply don't extend: we don't have any more core. */
382 return;
384 map = new_map;
385 maxmap = new_size;
389 #ifdef _LIBC
390 static void __attribute__ ((unused))
391 free_mem (void)
393 if (string_space != NULL)
394 free (string_space);
395 if (map != NULL)
396 free (map);
398 text_set_element (__libc_subfreeres, free_mem);
399 #endif
402 static int
403 alias_compare (map1, map2)
404 const struct alias_map *map1;
405 const struct alias_map *map2;
407 #if defined _LIBC || defined HAVE_STRCASECMP
408 return strcasecmp (map1->alias, map2->alias);
409 #else
410 const unsigned char *p1 = (const unsigned char *) map1->alias;
411 const unsigned char *p2 = (const unsigned char *) map2->alias;
412 unsigned char c1, c2;
414 if (p1 == p2)
415 return 0;
419 /* I know this seems to be odd but the tolower() function in
420 some systems libc cannot handle nonalpha characters. */
421 c1 = isupper (*p1) ? tolower (*p1) : *p1;
422 c2 = isupper (*p2) ? tolower (*p2) : *p2;
423 if (c1 == '\0')
424 break;
425 ++p1;
426 ++p2;
428 while (c1 == c2);
430 return c1 - c2;
431 #endif