Fri Nov 3 17:27:49 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / intl / localealias.c
blob716657c8441bfaba5131e911e4bb4bf55ec15aa8
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)
7 any later version.
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. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <ctype.h>
23 #include <stdio.h>
24 #include <sys/types.h>
26 #ifdef __GNUC__
27 # define alloca __builtin_alloca
28 #else
29 # if defined HAVE_ALLOCA_H || defined _LIBC
30 # include <alloca.h>
31 # else
32 # ifdef _AIX
33 #pragma alloca
34 # else
35 # ifndef alloca
36 char *alloca ();
37 # endif
38 # endif
39 # endif
40 #endif
42 #if defined STDC_HEADERS || defined _LIBC
43 # include <stdlib.h>
44 #else
45 char *getenv ();
46 # ifdef HAVE_MALLOC_H
47 # include <malloc.h>
48 # else
49 void free ();
50 # endif
51 #endif
53 #if defined HAVE_STRING_H || defined _LIBC
54 # include <string.h>
55 #else
56 # include <strings.h>
57 #endif
58 #if !HAVE_STRCHR && !defined _LIBC
59 # ifndef strchr
60 # define strchr index
61 # endif
62 #endif
64 #include "gettext.h"
65 #include "gettextP.h"
67 /* @@ end of prolog @@ */
69 #ifdef _LIBC
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
74 #endif
76 struct alias_map
78 const char *alias;
79 const char *value;
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));
95 const char *
96 _nl_expand_alias (name)
97 const char *name;
99 static const char *locale_alias_path = LOCALE_ALIAS_PATH;
100 struct alias_map *retval;
101 size_t added;
105 struct alias_map item;
107 item.alias = name;
109 if (nmap > 0)
110 retval = (struct alias_map *) bsearch (&item, map, nmap,
111 sizeof (struct alias_map),
112 (int (*) (const void *,
113 const void *))
114 alias_compare);
115 else
116 retval = NULL;
118 /* We really found an alias. Return the value. */
119 if (retval != NULL)
120 return retval->value;
122 /* Perhaps we can find another alias file. */
123 added = 0;
124 while (added == 0 && locale_alias_path[0] != '\0')
126 const char *start;
128 while (locale_alias_path[0] != '\0' && locale_alias_path[0] == ':')
129 ++locale_alias_path;
130 start = locale_alias_path;
132 while (locale_alias_path[0] != '\0' && locale_alias_path[0] != ':')
133 ++locale_alias_path;
135 if (start < locale_alias_path)
136 added = read_alias_file (start, locale_alias_path - start);
139 while (added != 0);
141 return NULL;
145 static size_t
146 read_alias_file (fname, fname_len)
147 const char *fname;
148 int fname_len;
150 FILE *fp;
151 char *full_fname;
152 size_t added;
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");
158 if (fp == NULL)
159 return 0;
161 added = 0;
162 while (!feof (fp))
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
167 be that long
169 char buf[BUFSIZ];
170 char *alias;
171 char *value;
172 char *cp;
174 if (fgets (buf, BUFSIZ, fp) == NULL)
175 /* EOF reached. */
176 break;
178 cp = buf;
179 /* Ignore leading white space. */
180 while (isspace (cp[0]))
181 ++cp;
183 /* A leading '#' signals a comment line. */
184 if (cp[0] != '\0' && cp[0] != '#')
186 alias = cp++;
187 while (cp[0] != '\0' && !isspace (cp[0]))
188 ++cp;
189 /* Terminate alias name. */
190 if (cp[0] != '\0')
191 *cp++ = '\0';
193 /* Now look for the beginning of the value. */
194 while (isspace (cp[0]))
195 ++cp;
197 if (cp[0] != '\0')
199 char *tp;
200 size_t len;
202 value = cp++;
203 while (cp[0] != '\0' && !isspace (cp[0]))
204 ++cp;
205 /* Terminate value. */
206 if (cp[0] == '\n')
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. */
211 *cp++ = '\0';
212 *cp = '\n';
214 else if (cp[0] != '\0')
215 *cp++ = '\0';
217 if (nmap >= maxmap)
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);
223 if (tp == NULL)
224 return added;
225 memcpy (tp, alias, len);
226 map[nmap].alias = tp;
228 len = strlen (value) + 1;
229 tp = (char *) malloc (len);
230 if (tp == NULL)
231 return added;
232 memcpy (tp, value, len);
233 map[nmap].value = tp;
235 ++nmap;
236 ++added;
240 /* Possibily not the whole line fits into the buffer. Ignore
241 the rest of the line. */
242 while (strchr (cp, '\n') == NULL)
244 cp = buf;
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. */
248 *cp = '\n';
252 /* Should we test for ferror()? I think we have to silently ignore
253 errors. --drepper */
254 fclose (fp);
256 if (added > 0)
257 qsort (map, nmap, sizeof (struct alias_map),
258 (int (*) (const void *, const void *)) alias_compare);
260 return added;
264 static void
265 extend_alias_table ()
267 size_t new_size;
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));
273 if (new_map == NULL)
274 /* Simply don't extend: we don't have any more core. */
275 return;
277 memcpy (new_map, map, nmap * sizeof (struct alias_map));
279 if (maxmap != 0)
280 free (map);
282 map = new_map;
283 maxmap = new_size;
287 static int
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);
294 #else
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;
299 if (p1 == p2)
300 return 0;
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;
308 if (c1 == '\0')
309 break;
311 while (c1 == c2);
313 return c1 - c2;
314 #endif