1 /* localealias.c -- handle aliases for locale names
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
4 This file is part of the GNU C Library. Its master source is NOT part of
5 the C library, however. The master source lives in /gd/gnu/lib.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library 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 GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, USA. */
28 #include <sys/types.h>
31 # define alloca __builtin_alloca
32 # define HAVE_ALLOCA 1
34 # if defined HAVE_ALLOCA_H || defined _LIBC
47 #if defined STDC_HEADERS || defined _LIBC
58 #if defined HAVE_STRING_H || defined _LIBC
63 #if !HAVE_STRCHR && !defined _LIBC
72 /* @@ end of prolog @@ */
75 /* Rename the non ANSI C functions. This is required by the standard
76 because some ANSI C functions will require linking with this object
77 file and the name space must not be polluted. */
78 # define strcasecmp __strcasecmp
82 /* For those loosing systems which don't have `alloca' we have to add
83 some additional code emulating it. */
85 /* Nothing has to be done. */
86 # define ADD_BLOCK(list, address) /* nothing */
87 # define FREE_BLOCKS(list) /* nothing */
92 struct block_list
*next
;
94 # define ADD_BLOCK(list, addr) \
96 struct block_list *newp = (struct block_list *) malloc (sizeof (*newp)); \
97 /* If we cannot get a free block we cannot add the new element to \
100 newp->address = (addr); \
101 newp->next = (list); \
105 # define FREE_BLOCKS(list) \
107 while (list != NULL) { \
108 struct block_list *old = list; \
113 #endif /* have alloca */
123 static struct alias_map
*map
;
124 static size_t nmap
= 0;
125 static size_t maxmap
= 0;
128 /* Prototypes for local functions. */
129 static size_t read_alias_file
PARAMS ((const char *fname
, int fname_len
));
130 static void 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
;
145 struct alias_map item
;
150 retval
= (struct alias_map
*) bsearch (&item
, map
, nmap
,
151 sizeof (struct alias_map
),
152 (int (*) PARAMS ((const void *,
158 /* We really found an alias. Return the value. */
160 return retval
->value
;
162 /* Perhaps we can find another alias file. */
164 while (added
== 0 && locale_alias_path
[0] != '\0')
168 while (locale_alias_path
[0] == ':')
170 start
= locale_alias_path
;
172 while (locale_alias_path
[0] != '\0' && locale_alias_path
[0] != ':')
175 if (start
< locale_alias_path
)
176 added
= read_alias_file (start
, locale_alias_path
- start
);
186 read_alias_file (fname
, fname_len
)
191 struct block_list
*alloca_list
= NULL
;
196 static const char aliasfile
[] = "/locale.alias";
198 full_fname
= (char *) alloca (fname_len
+ sizeof aliasfile
);
199 ADD_BLOCK (block_list
, full_fname
);
200 memcpy (full_fname
, fname
, fname_len
);
201 memcpy (&full_fname
[fname_len
], aliasfile
, sizeof aliasfile
);
203 fp
= fopen (full_fname
, "r");
206 FREE_BLOCKS (block_list
);
213 /* It is a reasonable approach to use a fix buffer here because
214 a) we are only interested in the first two fields
215 b) these fields must be usable as file names and so must not
223 if (fgets (buf
, BUFSIZ
, fp
) == NULL
)
228 /* Ignore leading white space. */
229 while (isspace (cp
[0]))
232 /* A leading '#' signals a comment line. */
233 if (cp
[0] != '\0' && cp
[0] != '#')
236 while (cp
[0] != '\0' && !isspace (cp
[0]))
238 /* Terminate alias name. */
242 /* Now look for the beginning of the value. */
243 while (isspace (cp
[0]))
252 while (cp
[0] != '\0' && !isspace (cp
[0]))
254 /* Terminate value. */
257 /* This has to be done to make the following test
258 for the end of line possible. We are looking for
259 the terminating '\n' which do not overwrite here. */
263 else if (cp
[0] != '\0')
267 extend_alias_table ();
269 /* We cannot depend on strdup available in the libc. Sigh! */
270 len
= strlen (alias
) + 1;
271 tp
= (char *) malloc (len
);
274 FREE_BLOCKS (block_list
);
277 memcpy (tp
, alias
, len
);
278 map
[nmap
].alias
= tp
;
280 len
= strlen (value
) + 1;
281 tp
= (char *) malloc (len
);
284 FREE_BLOCKS (block_list
);
287 memcpy (tp
, value
, len
);
288 map
[nmap
].value
= tp
;
295 /* Possibily not the whole line fitted into the buffer. Ignore
296 the rest of the line. */
297 while (strchr (cp
, '\n') == NULL
)
300 if (fgets (buf
, BUFSIZ
, fp
) == NULL
)
301 /* Make sure the inner loop will be left. The outer loop
302 will exit at the `feof' test. */
307 /* Should we test for ferror()? I think we have to silently ignore
312 qsort (map
, nmap
, sizeof (struct alias_map
),
313 (int (*) PARAMS ((const void *, const void *))) alias_compare
);
315 FREE_BLOCKS (block_list
);
321 extend_alias_table ()
324 struct alias_map
*new_map
;
326 new_size
= maxmap
== 0 ? 100 : 2 * maxmap
;
327 new_map
= (struct alias_map
*) malloc (new_size
328 * sizeof (struct alias_map
));
330 /* Simply don't extend: we don't have any more core. */
333 memcpy (new_map
, map
, nmap
* sizeof (struct alias_map
));
344 alias_compare (map1
, map2
)
345 const struct alias_map
*map1
;
346 const struct alias_map
*map2
;
348 #if defined _LIBC || defined HAVE_STRCASECMP
349 return strcasecmp (map1
->alias
, map2
->alias
);
351 const unsigned char *p1
= (const unsigned char *) map1
->alias
;
352 const unsigned char *p2
= (const unsigned char *) map2
->alias
;
353 unsigned char c1
, c2
;
360 /* I know this seems to be odd but the tolower() function in
361 some systems libc cannot handle nonalpha characters. */
362 c1
= isupper (*p1
) ? tolower (*p1
) : *p1
;
363 c2
= isupper (*p2
) ? tolower (*p2
) : *p2
;