1 /* Handle aliases for locale names.
2 Copyright (C) 1995-2002, 2003, 2005, 2011 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, see
17 <http://www.gnu.org/licenses/>. */
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 #if defined _LIBC || defined HAVE___FSETLOCKING
33 # include <stdio_ext.h>
35 #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(s1, s2) __strcasecmp_l (s1, s2, _nl_C_locobj_ptr)
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)
120 # define libc_freeres_ptr(decl) decl
123 libc_freeres_ptr (static char *string_space
);
124 static size_t string_space_act
;
125 static size_t string_space_max
;
126 libc_freeres_ptr (static struct alias_map
*map
);
128 static size_t maxmap
;
131 /* Prototypes for local functions. */
132 static size_t read_alias_file
PARAMS ((const char *fname
, int fname_len
))
134 static int extend_alias_table
PARAMS ((void));
135 static int alias_compare
PARAMS ((const struct alias_map
*map1
,
136 const struct alias_map
*map2
));
140 _nl_expand_alias (name
)
143 static const char *locale_alias_path
= LOCALE_ALIAS_PATH
;
144 struct alias_map
*retval
;
145 const char *result
= NULL
;
149 __libc_lock_lock (lock
);
154 struct alias_map item
;
159 retval
= (struct alias_map
*) bsearch (&item
, map
, nmap
,
160 sizeof (struct alias_map
),
161 (int (*) PARAMS ((const void *,
167 /* We really found an alias. Return the value. */
170 result
= retval
->value
;
174 /* Perhaps we can find another alias file. */
176 while (added
== 0 && locale_alias_path
[0] != '\0')
180 while (locale_alias_path
[0] == ':')
182 start
= locale_alias_path
;
184 while (locale_alias_path
[0] != '\0' && locale_alias_path
[0] != ':')
187 if (start
< locale_alias_path
)
188 added
= read_alias_file (start
, locale_alias_path
- start
);
194 __libc_lock_unlock (lock
);
203 read_alias_file (fname
, fname_len
)
210 static const char aliasfile
[] = "/locale.alias";
212 full_fname
= (char *) alloca (fname_len
+ sizeof aliasfile
);
214 mempcpy (mempcpy (full_fname
, fname
, fname_len
),
215 aliasfile
, sizeof aliasfile
);
217 memcpy (full_fname
, fname
, fname_len
);
218 memcpy (&full_fname
[fname_len
], aliasfile
, sizeof aliasfile
);
221 /* Note the file is opened with cancellation in the I/O functions
223 fp
= fopen (full_fname
, "rce");
228 #ifdef HAVE___FSETLOCKING
229 /* No threads present. */
230 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
236 /* It is a reasonable approach to use a fix buffer here because
237 a) we are only interested in the first two fields
238 b) these fields must be usable as file names and so must not
240 We avoid a multi-kilobyte buffer here since this would use up
241 stack space which we might not have if the program ran out of
249 if (FGETS (buf
, sizeof buf
, fp
) == NULL
)
253 /* Determine whether the line is complete. */
254 complete_line
= strchr (buf
, '\n') != NULL
;
257 /* Ignore leading white space. */
258 while (isspace ((unsigned char) cp
[0]))
261 /* A leading '#' signals a comment line. */
262 if (cp
[0] != '\0' && cp
[0] != '#')
265 while (cp
[0] != '\0' && !isspace ((unsigned char) cp
[0]))
267 /* Terminate alias name. */
271 /* Now look for the beginning of the value. */
272 while (isspace ((unsigned char) cp
[0]))
281 while (cp
[0] != '\0' && !isspace ((unsigned char) cp
[0]))
283 /* Terminate value. */
286 /* This has to be done to make the following test
287 for the end of line possible. We are looking for
288 the terminating '\n' which do not overwrite here. */
292 else if (cp
[0] != '\0')
296 if (__builtin_expect (extend_alias_table (), 0))
299 alias_len
= strlen (alias
) + 1;
300 value_len
= strlen (value
) + 1;
302 if (string_space_act
+ alias_len
+ value_len
> string_space_max
)
304 /* Increase size of memory pool. */
305 size_t new_size
= (string_space_max
306 + (alias_len
+ value_len
> 1024
307 ? alias_len
+ value_len
: 1024));
308 char *new_pool
= (char *) realloc (string_space
, new_size
);
309 if (new_pool
== NULL
)
312 if (__builtin_expect (string_space
!= new_pool
, 0))
316 for (i
= 0; i
< nmap
; i
++)
318 map
[i
].alias
+= new_pool
- string_space
;
319 map
[i
].value
+= new_pool
- string_space
;
323 string_space
= new_pool
;
324 string_space_max
= new_size
;
327 map
[nmap
].alias
= memcpy (&string_space
[string_space_act
],
329 string_space_act
+= alias_len
;
331 map
[nmap
].value
= memcpy (&string_space
[string_space_act
],
333 string_space_act
+= value_len
;
340 /* Possibly not the whole line fits into the buffer. Ignore
341 the rest of the line. */
344 if (FGETS (buf
, sizeof buf
, fp
) == NULL
)
345 /* Make sure the inner loop will be left. The outer loop
346 will exit at the `feof' test. */
348 while (strchr (buf
, '\n') == NULL
);
352 /* Should we test for ferror()? I think we have to silently ignore
357 qsort (map
, nmap
, sizeof (struct alias_map
),
358 (int (*) PARAMS ((const void *, const void *))) alias_compare
);
365 extend_alias_table ()
368 struct alias_map
*new_map
;
370 new_size
= maxmap
== 0 ? 100 : 2 * maxmap
;
371 new_map
= (struct alias_map
*) realloc (map
, (new_size
372 * sizeof (struct alias_map
)));
374 /* Simply don't extend: we don't have any more core. */
384 alias_compare (map1
, map2
)
385 const struct alias_map
*map1
;
386 const struct alias_map
*map2
;
388 #if defined _LIBC || defined HAVE_STRCASECMP
389 return strcasecmp (map1
->alias
, map2
->alias
);
391 const unsigned char *p1
= (const unsigned char *) map1
->alias
;
392 const unsigned char *p2
= (const unsigned char *) map2
->alias
;
393 unsigned char c1
, c2
;
400 /* I know this seems to be odd but the tolower() function in
401 some systems libc cannot handle nonalpha characters. */
402 c1
= isupper (*p1
) ? tolower (*p1
) : *p1
;
403 c2
= isupper (*p2
) ? tolower (*p2
) : *p2
;