1 /* localealias.c -- handle aliases for locale names
2 Copyright (C) 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include <sys/types.h>
27 # define alloca __builtin_alloca
29 # if defined HAVE_ALLOCA_H || defined _LIBC
42 #if defined STDC_HEADERS || defined _LIBC
53 #if defined HAVE_STRING_H || defined _LIBC
58 #if !HAVE_STRCHR && !defined _LIBC
67 /* @@ end of prolog @@ */
70 /* Rename the non ANSI C functions. This is required by the standard
71 because some ANSI C functions will require linking with this object
72 file and the name space must not be polluted. */
73 # define strcasecmp __strcasecmp
83 static struct alias_map
*map
;
84 static size_t nmap
= 0;
85 static size_t maxmap
= 0;
88 /* Prototypes for local functions. */
89 static size_t read_alias_file
__P ((const char *fname
, int fname_len
));
90 static void extend_alias_table
__P ((void));
91 static int alias_compare
__P ((const struct alias_map
*map1
,
92 const struct alias_map
*map2
));
96 _nl_expand_alias (name
)
99 static const char *locale_alias_path
= LOCALE_ALIAS_PATH
;
100 struct alias_map
*retval
;
105 struct alias_map item
;
110 retval
= (struct alias_map
*) bsearch (&item
, map
, nmap
,
111 sizeof (struct alias_map
),
112 (int (*) __P ((const void *,
118 /* We really found an alias. Return the value. */
120 return retval
->value
;
122 /* Perhaps we can find another alias file. */
124 while (added
== 0 && locale_alias_path
[0] != '\0')
128 while (locale_alias_path
[0] == ':')
130 start
= locale_alias_path
;
132 while (locale_alias_path
[0] != '\0' && locale_alias_path
[0] != ':')
135 if (start
< locale_alias_path
)
136 added
= read_alias_file (start
, locale_alias_path
- start
);
146 read_alias_file (fname
, fname_len
)
153 static const char aliasfile
[] = "/locale.alias";
155 full_fname
= (char *) alloca (fname_len
+ sizeof aliasfile
);
156 memcpy (full_fname
, fname
, fname_len
);
157 memcpy (&full_fname
[fname_len
], aliasfile
, sizeof aliasfile
);
159 fp
= fopen (full_fname
, "r");
166 /* It is a reasonable approach to use a fix buffer here because
167 a) we are only interested in the first two fields
168 b) these fields must be usable as file names and so must not
176 if (fgets (buf
, BUFSIZ
, fp
) == NULL
)
181 /* Ignore leading white space. */
182 while (isspace (cp
[0]))
185 /* A leading '#' signals a comment line. */
186 if (cp
[0] != '\0' && cp
[0] != '#')
189 while (cp
[0] != '\0' && !isspace (cp
[0]))
191 /* Terminate alias name. */
195 /* Now look for the beginning of the value. */
196 while (isspace (cp
[0]))
205 while (cp
[0] != '\0' && !isspace (cp
[0]))
207 /* Terminate value. */
210 /* This has to be done to make the following test
211 for the end of line possible. We are looking for
212 the terminating '\n' which do not overwrite here. */
216 else if (cp
[0] != '\0')
220 extend_alias_table ();
222 /* We cannot depend on strdup available in the libc. Sigh! */
223 len
= strlen (alias
) + 1;
224 tp
= (char *) malloc (len
);
227 memcpy (tp
, alias
, len
);
228 map
[nmap
].alias
= tp
;
230 len
= strlen (value
) + 1;
231 tp
= (char *) malloc (len
);
234 memcpy (tp
, value
, len
);
235 map
[nmap
].value
= tp
;
242 /* Possibily not the whole line fitted into the buffer. Ignore
243 the rest of the line. */
244 while (strchr (cp
, '\n') == NULL
)
247 if (fgets (buf
, BUFSIZ
, fp
) == NULL
)
248 /* Make sure the inner loop will be left. The outer loop
249 will exit at the `feof' test. */
254 /* Should we test for ferror()? I think we have to silently ignore
259 qsort (map
, nmap
, sizeof (struct alias_map
),
260 (int (*) __P ((const void *, const void *))) alias_compare
);
267 extend_alias_table ()
270 struct alias_map
*new_map
;
272 new_size
= maxmap
== 0 ? 100 : 2 * maxmap
;
273 new_map
= (struct alias_map
*) malloc (new_size
274 * sizeof (struct alias_map
));
276 /* Simply don't extend: we don't have any more core. */
279 memcpy (new_map
, map
, nmap
* sizeof (struct alias_map
));
290 alias_compare (map1
, map2
)
291 const struct alias_map
*map1
;
292 const struct alias_map
*map2
;
294 #if defined _LIBC || defined HAVE_STRCASECMP
295 return strcasecmp (map1
->alias
, map2
->alias
);
297 const unsigned char *p1
= (const unsigned char *) map1
->alias
;
298 const unsigned char *p2
= (const unsigned char *) map2
->alias
;
299 unsigned char c1
, c2
;
306 /* I know this seems to be odd but the tolower() function in
307 some systems libc cannot handle nonalpha characters. */
308 c1
= isupper (*p1
) ? tolower (*p1
) : *p1
;
309 c2
= isupper (*p2
) ? tolower (*p2
) : *p2
;