1 /* Copyright (C) 1995-2015 Free Software Foundation, Inc.
2 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Tell glibc's <string.h> to provide a prototype for stpcpy().
18 This must come before <config.h> because <config.h> may include
19 <features.h>, and once <features.h> has been included, it's too late. */
21 # define _GNU_SOURCE 1
30 #if defined _LIBC || defined HAVE_ARGZ_H
34 #include <sys/types.h>
39 /* On some strange systems still no definition of NULL is found. Sigh! */
41 # if defined __STDC__ && __STDC__
42 # define NULL ((void *) 0)
48 /* @@ end of prolog @@ */
51 /* Rename the non ANSI C functions. This is required by the standard
52 because some ANSI C functions will require linking with this object
53 file and the name space must not be polluted. */
55 # define stpcpy(dest, src) __stpcpy(dest, src)
59 static char *stpcpy (char *dest, const char *src);
64 ISSLASH(C) tests whether C is a directory separator character.
65 IS_ABSOLUTE_PATH(P) tests whether P is an absolute path. If it is not,
66 it may be concatenated to a directory pathname.
68 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__ || defined __EMX__ || defined __DJGPP__
69 /* Win32, Cygwin, OS/2, DOS */
70 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
71 # define HAS_DEVICE(P) \
72 ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
74 # define IS_ABSOLUTE_PATH(P) (ISSLASH ((P)[0]) || HAS_DEVICE (P))
77 # define ISSLASH(C) ((C) == '/')
78 # define IS_ABSOLUTE_PATH(P) ISSLASH ((P)[0])
81 /* Define function which are usually not available. */
83 #if defined HAVE_ARGZ_COUNT
85 # define __argz_count argz_count
87 /* Returns the number of strings in ARGZ. */
89 argz_count__ (const char *argz, size_t len)
94 size_t part_len = strlen (argz);
102 # define __argz_count(argz, len) argz_count__ (argz, len)
103 #endif /* !_LIBC && !HAVE_ARGZ_COUNT */
105 #if defined HAVE_ARGZ_STRINGIFY
106 # undef __argz_stringify
107 # define __argz_stringify argz_stringify
109 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
110 except the last into the character SEP. */
112 argz_stringify__ (char *argz, size_t len, int sep)
116 size_t part_len = strlen (argz);
123 # undef __argz_stringify
124 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
125 #endif /* !_LIBC && !HAVE_ARGZ_STRINGIFY */
128 #elif defined HAVE_ARGZ_NEXT
130 # define __argz_next argz_next
133 argz_next__ (char *argz, size_t argz_len, const char *entry)
137 if (entry < argz + argz_len)
138 entry = strchr (entry, '\0') + 1;
140 return entry >= argz + argz_len ? NULL : (char *) entry;
149 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
150 #endif /* !_LIBC && !HAVE_ARGZ_NEXT */
152 /* Return number of bits set in X. */
157 /* We assume that no more than 16 bits are used. */
158 x = ((x & ~0x5555) >> 1) + (x & 0x5555);
159 x = ((x & ~0x3333) >> 2) + (x & 0x3333);
160 x = ((x >> 4) + x) & 0x0f0f;
161 x = ((x >> 8) + x) & 0xff;
168 struct loaded_l10nfile *
169 _nl_make_l10nflist (struct loaded_l10nfile **l10nfile_list,
170 const char *dirlist, size_t dirlist_len,
171 int mask, const char *language, const char *territory,
172 const char *codeset, const char *normalized_codeset,
173 const char *modifier,
174 const char *filename, int do_allocate)
177 struct loaded_l10nfile **lastp;
178 struct loaded_l10nfile *retval;
180 size_t dirlist_count;
184 /* If LANGUAGE contains an absolute directory specification, we ignore
186 if (IS_ABSOLUTE_PATH (language))
189 /* Allocate room for the full file name. */
190 abs_filename = (char *) malloc (dirlist_len
192 + ((mask & XPG_TERRITORY) != 0
193 ? strlen (territory) + 1 : 0)
194 + ((mask & XPG_CODESET) != 0
195 ? strlen (codeset) + 1 : 0)
196 + ((mask & XPG_NORM_CODESET) != 0
197 ? strlen (normalized_codeset) + 1 : 0)
198 + ((mask & XPG_MODIFIER) != 0
199 ? strlen (modifier) + 1 : 0)
200 + 1 + strlen (filename) + 1);
202 if (abs_filename == NULL)
205 /* Construct file name. */
209 memcpy (cp, dirlist, dirlist_len);
210 __argz_stringify (cp, dirlist_len, PATH_SEPARATOR);
215 cp = stpcpy (cp, language);
217 if ((mask & XPG_TERRITORY) != 0)
220 cp = stpcpy (cp, territory);
222 if ((mask & XPG_CODESET) != 0)
225 cp = stpcpy (cp, codeset);
227 if ((mask & XPG_NORM_CODESET) != 0)
230 cp = stpcpy (cp, normalized_codeset);
232 if ((mask & XPG_MODIFIER) != 0)
235 cp = stpcpy (cp, modifier);
239 stpcpy (cp, filename);
241 /* Look in list of already loaded domains whether it is already
243 lastp = l10nfile_list;
244 for (retval = *l10nfile_list; retval != NULL; retval = retval->next)
245 if (retval->filename != NULL)
247 int compare = strcmp (retval->filename, abs_filename);
253 /* It's not in the list. */
258 lastp = &retval->next;
261 if (retval != NULL || do_allocate == 0)
267 dirlist_count = (dirlist_len > 0 ? __argz_count (dirlist, dirlist_len) : 1);
269 /* Allocate a new loaded_l10nfile. */
271 (struct loaded_l10nfile *)
272 malloc (sizeof (*retval)
273 + (((dirlist_count << pop (mask)) + (dirlist_count > 1 ? 1 : 0))
274 * sizeof (struct loaded_l10nfile *)));
281 retval->filename = abs_filename;
283 /* We set retval->data to NULL here; it is filled in later.
284 Setting retval->decided to 1 here means that retval does not
285 correspond to a real file (dirlist_count > 1) or is not worth
286 looking up (if an unnormalized codeset was specified). */
287 retval->decided = (dirlist_count > 1
288 || ((mask & XPG_CODESET) != 0
289 && (mask & XPG_NORM_CODESET) != 0));
292 retval->next = *lastp;
296 /* Recurse to fill the inheritance list of RETVAL.
297 If the DIRLIST is a real list (i.e. DIRLIST_COUNT > 1), the RETVAL
298 entry does not correspond to a real file; retval->filename contains
299 colons. In this case we loop across all elements of DIRLIST and
300 across all bit patterns dominated by MASK.
301 If the DIRLIST is a single directory or entirely redundant (i.e.
302 DIRLIST_COUNT == 1), we loop across all bit patterns dominated by
303 MASK, excluding MASK itself.
304 In either case, we loop down from MASK to 0. This has the effect
305 that the extra bits in the locale name are dropped in this order:
306 first the modifier, then the territory, then the codeset, then the
307 normalized_codeset. */
308 for (cnt = dirlist_count > 1 ? mask : mask - 1; cnt >= 0; --cnt)
309 if ((cnt & ~mask) == 0
310 && !((cnt & XPG_CODESET) != 0 && (cnt & XPG_NORM_CODESET) != 0))
312 if (dirlist_count > 1)
314 /* Iterate over all elements of the DIRLIST. */
317 while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir))
319 retval->successor[entries++]
320 = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1,
321 cnt, language, territory, codeset,
322 normalized_codeset, modifier, filename,
326 retval->successor[entries++]
327 = _nl_make_l10nflist (l10nfile_list, dirlist, dirlist_len,
328 cnt, language, territory, codeset,
329 normalized_codeset, modifier, filename, 1);
331 retval->successor[entries] = NULL;
336 /* Normalize codeset name. There is no standard for the codeset
337 names. Normalization allows the user to use any of the common
338 names. The return value is dynamically allocated and has to be
339 freed by the caller. */
341 _nl_normalize_codeset (const char *codeset, size_t name_len)
349 for (cnt = 0; cnt < name_len; ++cnt)
350 if (isalnum ((unsigned char) codeset[cnt]))
354 if (isalpha ((unsigned char) codeset[cnt]))
358 retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1);
363 wp = stpcpy (retval, "iso");
367 for (cnt = 0; cnt < name_len; ++cnt)
368 if (isalpha ((unsigned char) codeset[cnt]))
369 *wp++ = tolower ((unsigned char) codeset[cnt]);
370 else if (isdigit ((unsigned char) codeset[cnt]))
371 *wp++ = codeset[cnt];
376 return (const char *) retval;
380 /* @@ begin of epilog @@ */
382 /* We don't want libintl.a to depend on any other library. So we
383 avoid the non-standard function stpcpy. In GNU C Library this
384 function is available, though. Also allow the symbol HAVE_STPCPY
386 #if !_LIBC && !HAVE_STPCPY
388 stpcpy (char *dest, const char *src)
390 while ((*dest++ = *src++) != '\0')