1 /* Handle aliases for locale names.
2 Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* Tell glibc's <string.h> to provide a prototype for mempcpy().
21 This must come before <config.h> because <config.h> may include
22 <features.h>, and once <features.h> has been included, it's too late. */
24 # define _GNU_SOURCE 1
33 #if defined _LIBC || defined HAVE___FSETLOCKING
34 # include <stdio_ext.h>
36 #include <sys/types.h>
39 # define alloca __builtin_alloca
40 # define HAVE_ALLOCA 1
42 # if defined HAVE_ALLOCA_H || defined _LIBC
60 /* @@ end of prolog @@ */
63 /* Rename the non ANSI C functions. This is required by the standard
64 because some ANSI C functions will require linking with this object
65 file and the name space must not be polluted. */
66 # define strcasecmp __strcasecmp
69 # define mempcpy __mempcpy
71 # define HAVE_MEMPCPY 1
72 # define HAVE___FSETLOCKING 1
74 /* We need locking here since we can be called from different places. */
75 # include <bits/libc-lock.h>
77 __libc_lock_define_initialized (static, lock
);
80 #ifndef internal_function
81 # define internal_function
84 /* Some optimizations for glibc. */
86 # define FEOF(fp) feof_unlocked (fp)
87 # define FGETS(buf, n, fp) fgets_unlocked (buf, n, fp)
89 # define FEOF(fp) feof (fp)
90 # define FGETS(buf, n, fp) fgets (buf, n, fp)
93 /* For those losing systems which don't have `alloca' we have to add
94 some additional code emulating it. */
96 # define freea(p) /* nothing */
98 # define alloca(n) malloc (n)
99 # define freea(p) free (p)
102 #if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
104 # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
106 #if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
108 # define feof(s) feof_unlocked (s)
119 static char *string_space
;
120 static size_t string_space_act
;
121 static size_t string_space_max
;
122 static struct alias_map
*map
;
124 static size_t maxmap
;
127 /* Prototypes for local functions. */
128 static size_t read_alias_file
PARAMS ((const char *fname
, int fname_len
))
130 static int extend_alias_table
PARAMS ((void));
131 static int alias_compare
PARAMS ((const struct alias_map
*map1
,
132 const struct alias_map
*map2
));
136 _nl_expand_alias (name
)
139 static const char *locale_alias_path
= LOCALE_ALIAS_PATH
;
140 struct alias_map
*retval
;
141 const char *result
= NULL
;
145 __libc_lock_lock (lock
);
150 struct alias_map item
;
155 retval
= (struct alias_map
*) bsearch (&item
, map
, nmap
,
156 sizeof (struct alias_map
),
157 (int (*) PARAMS ((const void *,
163 /* We really found an alias. Return the value. */
166 result
= retval
->value
;
170 /* Perhaps we can find another alias file. */
172 while (added
== 0 && locale_alias_path
[0] != '\0')
176 while (locale_alias_path
[0] == ':')
178 start
= locale_alias_path
;
180 while (locale_alias_path
[0] != '\0' && locale_alias_path
[0] != ':')
183 if (start
< locale_alias_path
)
184 added
= read_alias_file (start
, locale_alias_path
- start
);
190 __libc_lock_unlock (lock
);
199 read_alias_file (fname
, fname_len
)
206 static const char aliasfile
[] = "/locale.alias";
208 full_fname
= (char *) alloca (fname_len
+ sizeof aliasfile
);
210 mempcpy (mempcpy (full_fname
, fname
, fname_len
),
211 aliasfile
, sizeof aliasfile
);
213 memcpy (full_fname
, fname
, fname_len
);
214 memcpy (&full_fname
[fname_len
], aliasfile
, sizeof aliasfile
);
217 fp
= fopen (full_fname
, "r");
222 #ifdef HAVE___FSETLOCKING
223 /* No threads present. */
224 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
230 /* It is a reasonable approach to use a fix buffer here because
231 a) we are only interested in the first two fields
232 b) these fields must be usable as file names and so must not
240 if (FGETS (buf
, sizeof buf
, fp
) == NULL
)
244 /* Possibly not the whole line fits into the buffer. Ignore
245 the rest of the line. */
246 if (strchr (buf
, '\n') == NULL
)
250 if (FGETS (altbuf
, sizeof altbuf
, fp
) == NULL
)
251 /* Make sure the inner loop will be left. The outer loop
252 will exit at the `feof' test. */
254 while (strchr (altbuf
, '\n') == NULL
);
258 /* Ignore leading white space. */
259 while (isspace ((unsigned char) cp
[0]))
262 /* A leading '#' signals a comment line. */
263 if (cp
[0] != '\0' && cp
[0] != '#')
266 while (cp
[0] != '\0' && !isspace ((unsigned char) cp
[0]))
268 /* Terminate alias name. */
272 /* Now look for the beginning of the value. */
273 while (isspace ((unsigned char) cp
[0]))
282 while (cp
[0] != '\0' && !isspace ((unsigned char) cp
[0]))
284 /* Terminate value. */
287 /* This has to be done to make the following test
288 for the end of line possible. We are looking for
289 the terminating '\n' which do not overwrite here. */
293 else if (cp
[0] != '\0')
297 if (__builtin_expect (extend_alias_table (), 0))
300 alias_len
= strlen (alias
) + 1;
301 value_len
= strlen (value
) + 1;
303 if (string_space_act
+ alias_len
+ value_len
> string_space_max
)
305 /* Increase size of memory pool. */
306 size_t new_size
= (string_space_max
307 + (alias_len
+ value_len
> 1024
308 ? alias_len
+ value_len
: 1024));
309 char *new_pool
= (char *) realloc (string_space
, new_size
);
310 if (new_pool
== NULL
)
313 if (__builtin_expect (string_space
!= new_pool
, 0))
317 for (i
= 0; i
< nmap
; i
++)
319 map
[i
].alias
+= new_pool
- string_space
;
320 map
[i
].value
+= new_pool
- string_space
;
324 string_space
= new_pool
;
325 string_space_max
= new_size
;
328 map
[nmap
].alias
= memcpy (&string_space
[string_space_act
],
330 string_space_act
+= alias_len
;
332 map
[nmap
].value
= memcpy (&string_space
[string_space_act
],
334 string_space_act
+= value_len
;
342 /* Should we test for ferror()? I think we have to silently ignore
347 qsort (map
, nmap
, sizeof (struct alias_map
),
348 (int (*) PARAMS ((const void *, const void *))) alias_compare
);
355 extend_alias_table ()
358 struct alias_map
*new_map
;
360 new_size
= maxmap
== 0 ? 100 : 2 * maxmap
;
361 new_map
= (struct alias_map
*) realloc (map
, (new_size
362 * sizeof (struct alias_map
)));
364 /* Simply don't extend: we don't have any more core. */
374 static void __attribute__ ((unused
))
377 if (string_space
!= NULL
)
382 text_set_element (__libc_subfreeres
, free_mem
);
387 alias_compare (map1
, map2
)
388 const struct alias_map
*map1
;
389 const struct alias_map
*map2
;
391 #if defined _LIBC || defined HAVE_STRCASECMP
392 return strcasecmp (map1
->alias
, map2
->alias
);
394 const unsigned char *p1
= (const unsigned char *) map1
->alias
;
395 const unsigned char *p2
= (const unsigned char *) map2
->alias
;
396 unsigned char c1
, c2
;
403 /* I know this seems to be odd but the tolower() function in
404 some systems libc cannot handle nonalpha characters. */
405 c1
= isupper (*p1
) ? tolower (*p1
) : *p1
;
406 c2
= isupper (*p2
) ? tolower (*p2
) : *p2
;