1 /* Handle aliases for locale names.
2 Copyright (C) 1995-2002, 2003 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>
40 # define alloca __builtin_alloca
41 # define HAVE_ALLOCA 1
43 # if defined HAVE_ALLOCA_H || defined _LIBC
61 /* @@ end of prolog @@ */
64 /* Rename the non ANSI C functions. This is required by the standard
65 because some ANSI C functions will require linking with this object
66 file and the name space must not be polluted. */
67 # define strcasecmp __strcasecmp
70 # define mempcpy __mempcpy
72 # define HAVE_MEMPCPY 1
73 # define HAVE___FSETLOCKING 1
75 /* We need locking here since we can be called from different places. */
76 # include <bits/libc-lock.h>
78 __libc_lock_define_initialized (static, lock
);
81 #ifndef internal_function
82 # define internal_function
85 /* Some optimizations for glibc. */
87 # define FEOF(fp) feof_unlocked (fp)
88 # define FGETS(buf, n, fp) fgets_unlocked (buf, n, fp)
90 # define FEOF(fp) feof (fp)
91 # define FGETS(buf, n, fp) fgets (buf, n, fp)
94 /* For those losing systems which don't have `alloca' we have to add
95 some additional code emulating it. */
97 # define freea(p) /* nothing */
99 # define alloca(n) malloc (n)
100 # define freea(p) free (p)
103 #if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
105 # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
107 #if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
109 # define feof(s) feof_unlocked (s)
121 # define libc_freeres_ptr(decl) decl
124 libc_freeres_ptr (static char *string_space
);
125 static size_t string_space_act
;
126 static size_t string_space_max
;
127 libc_freeres_ptr (static struct alias_map
*map
);
129 static size_t maxmap
;
132 /* Prototypes for local functions. */
133 static size_t read_alias_file
PARAMS ((const char *fname
, int fname_len
))
135 static int extend_alias_table
PARAMS ((void));
136 static int alias_compare
PARAMS ((const struct alias_map
*map1
,
137 const struct alias_map
*map2
));
141 _nl_expand_alias (name
)
144 static const char *locale_alias_path
= LOCALE_ALIAS_PATH
;
145 struct alias_map
*retval
;
146 const char *result
= NULL
;
150 __libc_lock_lock (lock
);
155 struct alias_map item
;
160 retval
= (struct alias_map
*) bsearch (&item
, map
, nmap
,
161 sizeof (struct alias_map
),
162 (int (*) PARAMS ((const void *,
168 /* We really found an alias. Return the value. */
171 result
= retval
->value
;
175 /* Perhaps we can find another alias file. */
177 while (added
== 0 && locale_alias_path
[0] != '\0')
181 while (locale_alias_path
[0] == ':')
183 start
= locale_alias_path
;
185 while (locale_alias_path
[0] != '\0' && locale_alias_path
[0] != ':')
188 if (start
< locale_alias_path
)
189 added
= read_alias_file (start
, locale_alias_path
- start
);
195 __libc_lock_unlock (lock
);
204 read_alias_file (fname
, fname_len
)
211 static const char aliasfile
[] = "/locale.alias";
213 full_fname
= (char *) alloca (fname_len
+ sizeof aliasfile
);
215 mempcpy (mempcpy (full_fname
, fname
, fname_len
),
216 aliasfile
, sizeof aliasfile
);
218 memcpy (full_fname
, fname
, fname_len
);
219 memcpy (&full_fname
[fname_len
], aliasfile
, sizeof aliasfile
);
222 /* Note the file is opened with cancellation in the I/O functions
224 fp
= fopen (full_fname
, "rc");
229 #ifdef HAVE___FSETLOCKING
230 /* No threads present. */
231 __fsetlocking (fp
, FSETLOCKING_BYCALLER
);
237 /* It is a reasonable approach to use a fix buffer here because
238 a) we are only interested in the first two fields
239 b) these fields must be usable as file names and so must not
241 We avoid a multi-kilobyte buffer here since this would use up
242 stack space which we might not have if the program ran out of
250 if (FGETS (buf
, sizeof buf
, fp
) == NULL
)
254 /* Determine whether the line is complete. */
255 complete_line
= strchr (buf
, '\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
;
341 /* Possibly not the whole line fits into the buffer. Ignore
342 the rest of the line. */
345 if (FGETS (buf
, sizeof buf
, fp
) == NULL
)
346 /* Make sure the inner loop will be left. The outer loop
347 will exit at the `feof' test. */
349 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
;