1 /* localealias.c -- handle aliases for locale names
2 Copyright (C) 1995 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 (*) (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] != '\0' && 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
)
154 full_fname
= (char *) alloca (fname_len
+ sizeof ("/locale.alias"));
155 sprintf (full_fname
, "%.*s/locale.alias", fname_len
, fname
);
157 fp
= fopen (full_fname
, "r");
164 /* It is a reasonable approach to use a fix buffer here because
165 a) we are only interested in the first two fields
166 b) these fields must be usable as file names and so must not
174 if (fgets (buf
, BUFSIZ
, fp
) == NULL
)
179 /* Ignore leading white space. */
180 while (isspace (cp
[0]))
183 /* A leading '#' signals a comment line. */
184 if (cp
[0] != '\0' && cp
[0] != '#')
187 while (cp
[0] != '\0' && !isspace (cp
[0]))
189 /* Terminate alias name. */
193 /* Now look for the beginning of the value. */
194 while (isspace (cp
[0]))
203 while (cp
[0] != '\0' && !isspace (cp
[0]))
205 /* Terminate value. */
208 /* This has to be done to make the following test
209 for the end of line possible. We are looking for
210 the terminating '\n' which do not overwrite here. */
214 else if (cp
[0] != '\0')
218 extend_alias_table ();
220 /* We cannot depend on strdup available in the libc. Sigh! */
221 len
= strlen (alias
) + 1;
222 tp
= (char *) malloc (len
);
225 memcpy (tp
, alias
, len
);
226 map
[nmap
].alias
= tp
;
228 len
= strlen (value
) + 1;
229 tp
= (char *) malloc (len
);
232 memcpy (tp
, value
, len
);
233 map
[nmap
].value
= tp
;
240 /* Possibily not the whole line fits into the buffer. Ignore
241 the rest of the line. */
242 while (strchr (cp
, '\n') == NULL
)
245 if (fgets (buf
, BUFSIZ
, fp
) == NULL
)
246 /* Make sure the inner loop will be left. The outer loop
247 will exit at the `feof' test. */
252 /* Should we test for ferror()? I think we have to silently ignore
257 qsort (map
, nmap
, sizeof (struct alias_map
),
258 (int (*) (const void *, const void *)) alias_compare
);
265 extend_alias_table ()
268 struct alias_map
*new_map
;
270 new_size
= maxmap
== 0 ? 100 : 2 * maxmap
;
271 new_map
= (struct alias_map
*) malloc (new_size
272 * sizeof (struct alias_map
));
274 /* Simply don't extend: we don't have any more core. */
277 memcpy (new_map
, map
, nmap
* sizeof (struct alias_map
));
288 alias_compare (map1
, map2
)
289 const struct alias_map
*map1
;
290 const struct alias_map
*map2
;
292 #if defined _LIBC || defined HAVE_STRCASECMP
293 return strcasecmp (map1
->alias
, map2
->alias
);
295 const unsigned char *p1
= (const unsigned char *) map1
->alias
;
296 const unsigned char *p2
= (const unsigned char *) map2
->alias
;
297 unsigned char c1
, c2
;
304 /* I know this seems to be odd but the tolower() function in
305 some systems libc cannot handle nonalpha characters. */
306 c1
= isalpha (*p1
) ? tolower (*p1
) : *p1
;
307 c2
= isalpha (*p2
) ? tolower (*p2
) : *p2
;