]> git.ipfire.org Git - thirdparty/glibc.git/blame - intl/localealias.c
dcigettext.c: Add missing bracket
[thirdparty/glibc.git] / intl / localealias.c
CommitLineData
c84142e8 1/* Handle aliases for locale names.
d6c7294e 2 Copyright (C) 1995-2002, 2003, 2005 Free Software Foundation, Inc.
41bdb6e2 3 This file is part of the GNU C Library.
24906b43 4
c84142e8 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
0393dfd6 9
c84142e8
UD
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
41bdb6e2 13 Lesser General Public License for more details.
24906b43 14
41bdb6e2
AJ
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
18 02111-1307 USA. */
24906b43 19
17c389fc
UD
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. */
23#ifndef _GNU_SOURCE
24# define _GNU_SOURCE 1
25#endif
26
24906b43
RM
27#ifdef HAVE_CONFIG_H
28# include <config.h>
29#endif
30
31#include <ctype.h>
32#include <stdio.h>
2706ee38
UD
33#if defined _LIBC || defined HAVE___FSETLOCKING
34# include <stdio_ext.h>
35#endif
24906b43
RM
36#include <sys/types.h>
37
38#ifdef __GNUC__
0f283ffc 39# undef alloca
24906b43 40# define alloca __builtin_alloca
fa00327f 41# define HAVE_ALLOCA 1
24906b43
RM
42#else
43# if defined HAVE_ALLOCA_H || defined _LIBC
44# include <alloca.h>
45# else
46# ifdef _AIX
47 #pragma alloca
48# else
49# ifndef alloca
50char *alloca ();
51# endif
52# endif
53# endif
54#endif
55
0555fcce
UD
56#include <stdlib.h>
57#include <string.h>
24906b43 58
24906b43
RM
59#include "gettextP.h"
60
61/* @@ end of prolog @@ */
62
63#ifdef _LIBC
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
40a55d20 68
e7c5513d
UD
69# ifndef mempcpy
70# define mempcpy __mempcpy
71# endif
1618c590 72# define HAVE_MEMPCPY 1
2706ee38 73# define HAVE___FSETLOCKING 1
1618c590 74
838e5ffe 75/* We need locking here since we can be called from different places. */
f41c8091 76# include <bits/libc-lock.h>
40a55d20
UD
77
78__libc_lock_define_initialized (static, lock);
24906b43
RM
79#endif
80
dfd2257a
UD
81#ifndef internal_function
82# define internal_function
83#endif
5f2eab42 84
2706ee38
UD
85/* Some optimizations for glibc. */
86#ifdef _LIBC
87# define FEOF(fp) feof_unlocked (fp)
88# define FGETS(buf, n, fp) fgets_unlocked (buf, n, fp)
89#else
90# define FEOF(fp) feof (fp)
91# define FGETS(buf, n, fp) fgets (buf, n, fp)
92#endif
93
4a4d50f3 94/* For those losing systems which don't have `alloca' we have to add
5f2eab42
RM
95 some additional code emulating it. */
96#ifdef HAVE_ALLOCA
4a4d50f3 97# define freea(p) /* nothing */
5f2eab42 98#else
4a4d50f3
UD
99# define alloca(n) malloc (n)
100# define freea(p) free (p)
101#endif
5f2eab42 102
628a0aa1 103#if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
77ccaba1
UD
104# undef fgets
105# define fgets(buf, len, s) fgets_unlocked (buf, len, s)
106#endif
628a0aa1 107#if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
77ccaba1
UD
108# undef feof
109# define feof(s) feof_unlocked (s)
110#endif
111
5f2eab42 112
24906b43
RM
113struct alias_map
114{
115 const char *alias;
116 const char *value;
117};
118
119
c877418f
RM
120#ifndef _LIBC
121# define libc_freeres_ptr(decl) decl
122#endif
123
124libc_freeres_ptr (static char *string_space);
390500b1
UD
125static size_t string_space_act;
126static size_t string_space_max;
c877418f 127libc_freeres_ptr (static struct alias_map *map);
390500b1
UD
128static size_t nmap;
129static size_t maxmap;
24906b43
RM
130
131
132/* Prototypes for local functions. */
dfd2257a
UD
133static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
134 internal_function;
17c389fc 135static int extend_alias_table PARAMS ((void));
be10a868
RM
136static int alias_compare PARAMS ((const struct alias_map *map1,
137 const struct alias_map *map2));
24906b43
RM
138
139
140const char *
141_nl_expand_alias (name)
142 const char *name;
143{
144 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
145 struct alias_map *retval;
40a55d20 146 const char *result = NULL;
24906b43
RM
147 size_t added;
148
40a55d20
UD
149#ifdef _LIBC
150 __libc_lock_lock (lock);
151#endif
152
24906b43
RM
153 do
154 {
155 struct alias_map item;
156
157 item.alias = name;
158
159 if (nmap > 0)
160 retval = (struct alias_map *) bsearch (&item, map, nmap,
161 sizeof (struct alias_map),
be10a868
RM
162 (int (*) PARAMS ((const void *,
163 const void *))
164 ) alias_compare);
24906b43
RM
165 else
166 retval = NULL;
167
168 /* We really found an alias. Return the value. */
169 if (retval != NULL)
40a55d20
UD
170 {
171 result = retval->value;
172 break;
173 }
24906b43
RM
174
175 /* Perhaps we can find another alias file. */
176 added = 0;
177 while (added == 0 && locale_alias_path[0] != '\0')
178 {
179 const char *start;
180
75914335 181 while (locale_alias_path[0] == ':')
24906b43
RM
182 ++locale_alias_path;
183 start = locale_alias_path;
184
185 while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
186 ++locale_alias_path;
187
188 if (start < locale_alias_path)
189 added = read_alias_file (start, locale_alias_path - start);
190 }
191 }
192 while (added != 0);
193
40a55d20
UD
194#ifdef _LIBC
195 __libc_lock_unlock (lock);
196#endif
197
198 return result;
24906b43
RM
199}
200
201
202static size_t
dfd2257a 203internal_function
24906b43
RM
204read_alias_file (fname, fname_len)
205 const char *fname;
206 int fname_len;
207{
208 FILE *fp;
209 char *full_fname;
210 size_t added;
86d2c878 211 static const char aliasfile[] = "/locale.alias";
24906b43 212
86d2c878 213 full_fname = (char *) alloca (fname_len + sizeof aliasfile);
1618c590
UD
214#ifdef HAVE_MEMPCPY
215 mempcpy (mempcpy (full_fname, fname, fname_len),
216 aliasfile, sizeof aliasfile);
217#else
86d2c878
RM
218 memcpy (full_fname, fname, fname_len);
219 memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
1618c590 220#endif
24906b43 221
ee8449f7
UD
222 /* Note the file is opened with cancellation in the I/O functions
223 disabled. */
224 fp = fopen (full_fname, "rc");
4a4d50f3 225 freea (full_fname);
24906b43 226 if (fp == NULL)
4a4d50f3 227 return 0;
24906b43 228
2706ee38
UD
229#ifdef HAVE___FSETLOCKING
230 /* No threads present. */
231 __fsetlocking (fp, FSETLOCKING_BYCALLER);
232#endif
233
24906b43 234 added = 0;
2706ee38 235 while (!FEOF (fp))
24906b43
RM
236 {
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
240 be that long
51b3c8f6
UD
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
243 memory. */
244 char buf[400];
8cb569b7
UD
245 char *alias;
246 char *value;
247 char *cp;
fae49c62 248 int complete_line;
24906b43 249
2706ee38 250 if (FGETS (buf, sizeof buf, fp) == NULL)
24906b43
RM
251 /* EOF reached. */
252 break;
253
fae49c62
UD
254 /* Determine whether the line is complete. */
255 complete_line = strchr (buf, '\n') != NULL;
256
24906b43
RM
257 cp = buf;
258 /* Ignore leading white space. */
0555fcce 259 while (isspace ((unsigned char) cp[0]))
24906b43
RM
260 ++cp;
261
262 /* A leading '#' signals a comment line. */
263 if (cp[0] != '\0' && cp[0] != '#')
264 {
265 alias = cp++;
0555fcce 266 while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
24906b43
RM
267 ++cp;
268 /* Terminate alias name. */
269 if (cp[0] != '\0')
270 *cp++ = '\0';
271
272 /* Now look for the beginning of the value. */
0555fcce 273 while (isspace ((unsigned char) cp[0]))
24906b43
RM
274 ++cp;
275
276 if (cp[0] != '\0')
277 {
a5a0310d
UD
278 size_t alias_len;
279 size_t value_len;
24906b43
RM
280
281 value = cp++;
0555fcce 282 while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
24906b43
RM
283 ++cp;
284 /* Terminate value. */
285 if (cp[0] == '\n')
286 {
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. */
290 *cp++ = '\0';
291 *cp = '\n';
292 }
293 else if (cp[0] != '\0')
294 *cp++ = '\0';
295
296 if (nmap >= maxmap)
17c389fc 297 if (__builtin_expect (extend_alias_table (), 0))
d6c7294e 298 goto out;
24906b43 299
a5a0310d
UD
300 alias_len = strlen (alias) + 1;
301 value_len = strlen (value) + 1;
24906b43 302
a5a0310d 303 if (string_space_act + alias_len + value_len > string_space_max)
5f2eab42 304 {
a5a0310d
UD
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)
d6c7294e 311 goto out;
42be70d4
UD
312
313 if (__builtin_expect (string_space != new_pool, 0))
314 {
315 size_t i;
316
317 for (i = 0; i < nmap; i++)
318 {
319 map[i].alias += new_pool - string_space;
320 map[i].value += new_pool - string_space;
321 }
322 }
323
a5a0310d
UD
324 string_space = new_pool;
325 string_space_max = new_size;
5f2eab42 326 }
a5a0310d
UD
327
328 map[nmap].alias = memcpy (&string_space[string_space_act],
329 alias, alias_len);
330 string_space_act += alias_len;
331
1618c590 332 map[nmap].value = memcpy (&string_space[string_space_act],
a5a0310d
UD
333 value, value_len);
334 string_space_act += value_len;
24906b43
RM
335
336 ++nmap;
337 ++added;
338 }
339 }
51b3c8f6
UD
340
341 /* Possibly not the whole line fits into the buffer. Ignore
342 the rest of the line. */
fae49c62
UD
343 if (! complete_line)
344 do
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. */
348 break;
f6c93bd9 349 while (strchr (buf, '\n') == NULL);
24906b43
RM
350 }
351
d6c7294e 352out:
24906b43
RM
353 /* Should we test for ferror()? I think we have to silently ignore
354 errors. --drepper */
355 fclose (fp);
356
357 if (added > 0)
358 qsort (map, nmap, sizeof (struct alias_map),
be10a868 359 (int (*) PARAMS ((const void *, const void *))) alias_compare);
24906b43
RM
360
361 return added;
362}
363
364
17c389fc 365static int
24906b43
RM
366extend_alias_table ()
367{
368 size_t new_size;
369 struct alias_map *new_map;
370
371 new_size = maxmap == 0 ? 100 : 2 * maxmap;
a5a0310d
UD
372 new_map = (struct alias_map *) realloc (map, (new_size
373 * sizeof (struct alias_map)));
24906b43
RM
374 if (new_map == NULL)
375 /* Simply don't extend: we don't have any more core. */
17c389fc 376 return -1;
24906b43 377
24906b43
RM
378 map = new_map;
379 maxmap = new_size;
17c389fc 380 return 0;
24906b43
RM
381}
382
383
384static int
385alias_compare (map1, map2)
386 const struct alias_map *map1;
387 const struct alias_map *map2;
388{
389#if defined _LIBC || defined HAVE_STRCASECMP
390 return strcasecmp (map1->alias, map2->alias);
391#else
392 const unsigned char *p1 = (const unsigned char *) map1->alias;
393 const unsigned char *p2 = (const unsigned char *) map2->alias;
394 unsigned char c1, c2;
395
396 if (p1 == p2)
397 return 0;
398
399 do
400 {
401 /* I know this seems to be odd but the tolower() function in
402 some systems libc cannot handle nonalpha characters. */
75914335
RM
403 c1 = isupper (*p1) ? tolower (*p1) : *p1;
404 c2 = isupper (*p2) ? tolower (*p2) : *p2;
24906b43
RM
405 if (c1 == '\0')
406 break;
be10a868
RM
407 ++p1;
408 ++p2;
24906b43
RM
409 }
410 while (c1 == c2);
411
412 return c1 - c2;
413#endif
414}