1 /* Handle aliases for locale names.
2 Copyright (C) 1995-1999, 2000 Free Software Foundation, Inc.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 /* Tell glibc's <string.h> to provide a prototype for mempcpy().
20 This must come before <config.h> because <config.h> may include
21 <features.h>, and once <features.h> has been included, it's too late. */
23 # define _GNU_SOURCE 1
32 #include <sys/types.h>
35 # define alloca __builtin_alloca
36 # define HAVE_ALLOCA 1
38 # if defined HAVE_ALLOCA_H || defined _LIBC
51 #if defined STDC_HEADERS || defined _LIBC
62 #if defined HAVE_STRING_H || defined _LIBC
67 # define memcpy(Dst, Src, Num) (bcopy (Src, Dst, Num), (Dst))
70 #if !HAVE_STRCHR && !defined _LIBC
79 /* @@ end of prolog @@ */
82 /* Rename the non ANSI C functions. This is required by the standard
83 because some ANSI C functions will require linking with this object
84 file and the name space must not be polluted. */
85 # define strcasecmp __strcasecmp
88 # define mempcpy __mempcpy
90 # define HAVE_MEMPCPY 1
92 /* We need locking here since we can be called from different places. */
93 # include <bits/libc-lock.h>
95 __libc_lock_define_initialized (static, lock
);
98 #ifndef internal_function
99 # define internal_function
102 /* For those loosing systems which don't have `alloca' we have to add
103 some additional code emulating it. */
105 /* Nothing has to be done. */
106 # define ADD_BLOCK(list, address) /* nothing */
107 # define FREE_BLOCKS(list) /* nothing */
112 struct block_list
*next
;
114 # define ADD_BLOCK(list, addr) \
116 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
117 /* If we cannot get a free block we cannot add the new element to \
119 if (newp != NULL) { \
120 newp->address = (addr); \
121 newp->next = (list); \
125 # define FREE_BLOCKS(list) \
127 while (list != NULL) { \
128 struct block_list *old = list; \
134 # define alloca(size) (malloc (size))
135 #endif /* have alloca */
137 #if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
139 # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
141 #if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
143 # define feof(s) feof_unlocked (s)
154 static char *string_space
;
155 static size_t string_space_act
;
156 static size_t string_space_max
;
157 static struct alias_map
*map
;
159 static size_t maxmap
;
162 /* Prototypes for local functions. */
163 static size_t read_alias_file
PARAMS ((const char *fname
, int fname_len
))
165 static int extend_alias_table
PARAMS ((void));
166 static int alias_compare
PARAMS ((const struct alias_map
*map1
,
167 const struct alias_map
*map2
));
171 _nl_expand_alias (name
)
174 static const char *locale_alias_path
= LOCALE_ALIAS_PATH
;
175 struct alias_map
*retval
;
176 const char *result
= NULL
;
180 __libc_lock_lock (lock
);
185 struct alias_map item
;
190 retval
= (struct alias_map
*) bsearch (&item
, map
, nmap
,
191 sizeof (struct alias_map
),
192 (int (*) PARAMS ((const void *,
198 /* We really found an alias. Return the value. */
201 result
= retval
->value
;
205 /* Perhaps we can find another alias file. */
207 while (added
== 0 && locale_alias_path
[0] != '\0')
211 while (locale_alias_path
[0] == ':')
213 start
= locale_alias_path
;
215 while (locale_alias_path
[0] != '\0' && locale_alias_path
[0] != ':')
218 if (start
< locale_alias_path
)
219 added
= read_alias_file (start
, locale_alias_path
- start
);
225 __libc_lock_unlock (lock
);
234 read_alias_file (fname
, fname_len
)
239 struct block_list
*block_list
= NULL
;
244 static const char aliasfile
[] = "/locale.alias";
246 full_fname
= (char *) alloca (fname_len
+ sizeof aliasfile
);
247 ADD_BLOCK (block_list
, full_fname
);
249 mempcpy (mempcpy (full_fname
, fname
, fname_len
),
250 aliasfile
, sizeof aliasfile
);
252 memcpy (full_fname
, fname
, fname_len
);
253 memcpy (&full_fname
[fname_len
], aliasfile
, sizeof aliasfile
);
256 fp
= fopen (full_fname
, "r");
259 FREE_BLOCKS (block_list
);
266 /* It is a reasonable approach to use a fix buffer here because
267 a) we are only interested in the first two fields
268 b) these fields must be usable as file names and so must not
276 if (fgets (buf
, sizeof buf
, fp
) == NULL
)
280 /* Possibly not the whole line fits into the buffer. Ignore
281 the rest of the line. */
282 if (strchr (buf
, '\n') == NULL
)
286 if (fgets (altbuf
, sizeof altbuf
, fp
) == NULL
)
287 /* Make sure the inner loop will be left. The outer loop
288 will exit at the `feof' test. */
290 while (strchr (altbuf
, '\n') == NULL
);
294 /* Ignore leading white space. */
295 while (isspace (cp
[0]))
298 /* A leading '#' signals a comment line. */
299 if (cp
[0] != '\0' && cp
[0] != '#')
302 while (cp
[0] != '\0' && !isspace (cp
[0]))
304 /* Terminate alias name. */
308 /* Now look for the beginning of the value. */
309 while (isspace (cp
[0]))
318 while (cp
[0] != '\0' && !isspace (cp
[0]))
320 /* Terminate value. */
323 /* This has to be done to make the following test
324 for the end of line possible. We are looking for
325 the terminating '\n' which do not overwrite here. */
329 else if (cp
[0] != '\0')
333 if (__builtin_expect (extend_alias_table (), 0))
335 FREE_BLOCKS (block_list
);
339 alias_len
= strlen (alias
) + 1;
340 value_len
= strlen (value
) + 1;
342 if (string_space_act
+ alias_len
+ value_len
> string_space_max
)
344 /* Increase size of memory pool. */
345 size_t new_size
= (string_space_max
346 + (alias_len
+ value_len
> 1024
347 ? alias_len
+ value_len
: 1024));
348 char *new_pool
= (char *) realloc (string_space
, new_size
);
349 if (new_pool
== NULL
)
351 FREE_BLOCKS (block_list
);
355 if (__builtin_expect (string_space
!= new_pool
, 0))
359 for (i
= 0; i
< nmap
; i
++)
361 map
[i
].alias
+= new_pool
- string_space
;
362 map
[i
].value
+= new_pool
- string_space
;
366 string_space
= new_pool
;
367 string_space_max
= new_size
;
370 map
[nmap
].alias
= memcpy (&string_space
[string_space_act
],
372 string_space_act
+= alias_len
;
374 map
[nmap
].value
= memcpy (&string_space
[string_space_act
],
376 string_space_act
+= value_len
;
384 /* Should we test for ferror()? I think we have to silently ignore
389 qsort (map
, nmap
, sizeof (struct alias_map
),
390 (int (*) PARAMS ((const void *, const void *))) alias_compare
);
392 FREE_BLOCKS (block_list
);
398 extend_alias_table ()
401 struct alias_map
*new_map
;
403 new_size
= maxmap
== 0 ? 100 : 2 * maxmap
;
404 new_map
= (struct alias_map
*) realloc (map
, (new_size
405 * sizeof (struct alias_map
)));
407 /* Simply don't extend: we don't have any more core. */
417 static void __attribute__ ((unused
))
420 if (string_space
!= NULL
)
425 text_set_element (__libc_subfreeres
, free_mem
);
430 alias_compare (map1
, map2
)
431 const struct alias_map
*map1
;
432 const struct alias_map
*map2
;
434 #if defined _LIBC || defined HAVE_STRCASECMP
435 return strcasecmp (map1
->alias
, map2
->alias
);
437 const unsigned char *p1
= (const unsigned char *) map1
->alias
;
438 const unsigned char *p2
= (const unsigned char *) map2
->alias
;
439 unsigned char c1
, c2
;
446 /* I know this seems to be odd but the tolower() function in
447 some systems libc cannot handle nonalpha characters. */
448 c1
= isupper (*p1
) ? tolower (*p1
) : *p1
;
449 c2
= isupper (*p2
) ? tolower (*p2
) : *p2
;