1 /* localealias.c - Handle aliases for locale names. */
3 /* Copyright (C) 1995-1999, 2000-2001, 2003, 2005-2009 Free Software Foundation, Inc.
5 This file is part of GNU Bash.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash 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
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21 /* Tell glibc's <string.h> to provide a prototype for mempcpy().
22 This must come before <config.h> because <config.h> may include
23 <features.h>, and once <features.h> has been included, it's too late. */
25 # define _GNU_SOURCE 1
34 #if defined _LIBC || defined HAVE___FSETLOCKING
35 # include <stdio_ext.h>
37 #include <sys/types.h>
41 # define alloca __builtin_alloca
42 # define HAVE_ALLOCA 1
46 # define alloca _alloca
48 # if defined HAVE_ALLOCA_H || defined _LIBC
67 #if ENABLE_RELOCATABLE
68 # include "relocatable.h"
70 # define relocate(pathname) (pathname)
73 /* @@ end of prolog @@ */
76 /* Rename the non ANSI C functions. This is required by the standard
77 because some ANSI C functions will require linking with this object
78 file and the name space must not be polluted. */
79 # define strcasecmp __strcasecmp
82 # define mempcpy __mempcpy
84 # define HAVE_MEMPCPY 1
85 # define HAVE___FSETLOCKING 1
87 /* We need locking here since we can be called from different places. */
88 # include <bits/libc-lock.h>
90 __libc_lock_define_initialized (static, lock
);
93 #ifndef internal_function
94 # define internal_function
97 /* Some optimizations for glibc. */
99 # define FEOF(fp) feof_unlocked (fp)
100 # define FGETS(buf, n, fp) fgets_unlocked (buf, n, fp)
102 # define FEOF(fp) feof (fp)
103 # define FGETS(buf, n, fp) fgets (buf, n, fp)
106 /* For those losing systems which don't have `alloca' we have to add
107 some additional code emulating it. */
109 # define freea(p) /* nothing */
111 # define alloca(n) malloc (n)
112 # define freea(p) free (p)
115 #if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
117 # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
119 #if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
121 # define feof(s) feof_unlocked (s)
133 # define libc_freeres_ptr(decl) decl
136 libc_freeres_ptr (static char *string_space
);
137 static size_t string_space_act
;
138 static size_t string_space_max
;
139 libc_freeres_ptr (static struct alias_map
*map
);
141 static size_t maxmap
;
144 /* Prototypes for local functions. */
145 static size_t read_alias_file
PARAMS ((const char *fname
, int fname_len
))
147 static int extend_alias_table
PARAMS ((void));
148 static int alias_compare
PARAMS ((const struct alias_map
*map1
,
149 const struct alias_map
*map2
));
153 _nl_expand_alias (name
)
156 static const char *locale_alias_path
;
157 struct alias_map
*retval
;
158 const char *result
= NULL
;
162 __libc_lock_lock (lock
);
165 if (locale_alias_path
== NULL
)
166 locale_alias_path
= LOCALE_ALIAS_PATH
;
170 struct alias_map item
;
175 retval
= (struct alias_map
*) bsearch (&item
, map
, nmap
,
176 sizeof (struct alias_map
),
177 (int (*) PARAMS ((const void *,
183 /* We really found an alias. Return the value. */
186 result
= retval
->value
;
190 /* Perhaps we can find another alias file. */
192 while (added
== 0 && locale_alias_path
[0] != '\0')
196 while (locale_alias_path
[0] == PATH_SEPARATOR
)
198 start
= locale_alias_path
;
200 while (locale_alias_path
[0] != '\0'
201 && locale_alias_path
[0] != PATH_SEPARATOR
)
204 if (start
< locale_alias_path
)
205 added
= read_alias_file (start
, locale_alias_path
- start
);
211 __libc_lock_unlock (lock
);
220 read_alias_file (fname
, fname_len
)
227 static const char aliasfile
[] = "/locale.alias";
229 full_fname
= (char *) alloca (fname_len
+ sizeof aliasfile
);
231 mempcpy (mempcpy (full_fname
, fname
, fname_len
),
232 aliasfile
, sizeof aliasfile
);
234 memcpy (full_fname
, fname
, fname_len
);
235 memcpy (&full_fname
[fname_len
], aliasfile
, sizeof aliasfile
);
238 fp
= fopen (relocate (full_fname
), "r");
243 #ifdef HAVE___FSETLOCKING
244 /* No threads present. */
245 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
251 /* It is a reasonable approach to use a fix buffer here because
252 a) we are only interested in the first two fields
253 b) these fields must be usable as file names and so must not
255 We avoid a multi-kilobyte buffer here since this would use up
256 stack space which we might not have if the program ran out of
263 if (FGETS (buf
, sizeof buf
, fp
) == NULL
)
268 /* Ignore leading white space. */
269 while (isspace ((unsigned char) cp
[0]))
272 /* A leading '#' signals a comment line. */
273 if (cp
[0] != '\0' && cp
[0] != '#')
276 while (cp
[0] != '\0' && !isspace ((unsigned char) cp
[0]))
278 /* Terminate alias name. */
282 /* Now look for the beginning of the value. */
283 while (isspace ((unsigned char) cp
[0]))
292 while (cp
[0] != '\0' && !isspace ((unsigned char) cp
[0]))
294 /* Terminate value. */
297 /* This has to be done to make the following test
298 for the end of line possible. We are looking for
299 the terminating '\n' which do not overwrite here. */
303 else if (cp
[0] != '\0')
307 if (__builtin_expect (extend_alias_table (), 0))
310 alias_len
= strlen (alias
) + 1;
311 value_len
= strlen (value
) + 1;
313 if (string_space_act
+ alias_len
+ value_len
> string_space_max
)
315 /* Increase size of memory pool. */
316 size_t new_size
= (string_space_max
317 + (alias_len
+ value_len
> 1024
318 ? alias_len
+ value_len
: 1024));
319 char *new_pool
= (char *) realloc (string_space
, new_size
);
320 if (new_pool
== NULL
)
323 if (__builtin_expect (string_space
!= new_pool
, 0))
327 for (i
= 0; i
< nmap
; i
++)
329 map
[i
].alias
+= new_pool
- string_space
;
330 map
[i
].value
+= new_pool
- string_space
;
334 string_space
= new_pool
;
335 string_space_max
= new_size
;
338 map
[nmap
].alias
= memcpy (&string_space
[string_space_act
],
340 string_space_act
+= alias_len
;
342 map
[nmap
].value
= memcpy (&string_space
[string_space_act
],
344 string_space_act
+= value_len
;
351 /* Possibly not the whole line fits into the buffer. Ignore
352 the rest of the line. */
353 while (strchr (buf
, '\n') == NULL
)
354 if (FGETS (buf
, sizeof buf
, fp
) == NULL
)
355 /* Make sure the inner loop will be left. The outer loop
356 will exit at the `feof' test. */
360 /* Should we test for ferror()? I think we have to silently ignore
365 qsort (map
, nmap
, sizeof (struct alias_map
),
366 (int (*) PARAMS ((const void *, const void *))) alias_compare
);
373 extend_alias_table ()
376 struct alias_map
*new_map
;
378 new_size
= maxmap
== 0 ? 100 : 2 * maxmap
;
379 new_map
= (struct alias_map
*) realloc (map
, (new_size
380 * sizeof (struct alias_map
)));
382 /* Simply don't extend: we don't have any more core. */
392 alias_compare (map1
, map2
)
393 const struct alias_map
*map1
;
394 const struct alias_map
*map2
;
396 #if defined _LIBC || defined HAVE_STRCASECMP
397 return strcasecmp (map1
->alias
, map2
->alias
);
399 const unsigned char *p1
= (const unsigned char *) map1
->alias
;
400 const unsigned char *p2
= (const unsigned char *) map2
->alias
;
401 unsigned char c1
, c2
;
408 /* I know this seems to be odd but the tolower() function in
409 some systems libc cannot handle nonalpha characters. */
410 c1
= isupper (*p1
) ? tolower (*p1
) : *p1
;
411 c2
= isupper (*p2
) ? tolower (*p2
) : *p2
;