1 /* Copyright (C) 1995-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* Tell glibc's <string.h> to provide a prototype for stpcpy().
20 This must come before <config.h> because <config.h> may include
21 <features.h>, and once <features.h> has been included, it's too late. */
23 # define _GNU_SOURCE 1
32 #if defined _LIBC || defined HAVE_ARGZ_H
36 #include <sys/types.h>
41 /* On some strange systems still no definition of NULL is found. Sigh! */
43 # if defined __STDC__ && __STDC__
44 # define NULL ((void *) 0)
50 /* @@ end of prolog @@ */
53 /* Rename the non ANSI C functions. This is required by the standard
54 because some ANSI C functions will require linking with this object
55 file and the name space must not be polluted. */
57 # define stpcpy(dest, src) __stpcpy(dest, src)
61 static char *stpcpy
PARAMS ((char *dest
, const char *src
));
65 /* Define function which are usually not available. */
67 #if !defined _LIBC && !defined HAVE___ARGZ_COUNT
68 /* Returns the number of strings in ARGZ. */
69 static size_t argz_count__
PARAMS ((const char *argz
, size_t len
));
72 argz_count__ (argz
, len
)
79 size_t part_len
= strlen (argz
);
87 # define __argz_count(argz, len) argz_count__ (argz, len)
88 #endif /* !_LIBC && !HAVE___ARGZ_COUNT */
90 #if !defined _LIBC && !defined HAVE___ARGZ_STRINGIFY
91 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
92 except the last into the character SEP. */
93 static void argz_stringify__
PARAMS ((char *argz
, size_t len
, int sep
));
96 argz_stringify__ (argz
, len
, sep
)
103 size_t part_len
= strlen (argz
);
110 # undef __argz_stringify
111 # define __argz_stringify(argz, len, sep) argz_stringify__ (argz, len, sep)
112 #endif /* !_LIBC && !HAVE___ARGZ_STRINGIFY */
114 #if !defined _LIBC && !defined HAVE___ARGZ_NEXT
115 static char *argz_next__
PARAMS ((char *argz
, size_t argz_len
,
119 argz_next__ (argz
, argz_len
, entry
)
126 if (entry
< argz
+ argz_len
)
127 entry
= strchr (entry
, '\0') + 1;
129 return entry
>= argz
+ argz_len
? NULL
: (char *) entry
;
138 # define __argz_next(argz, len, entry) argz_next__ (argz, len, entry)
139 #endif /* !_LIBC && !HAVE___ARGZ_NEXT */
142 /* Return number of bits set in X. */
144 static int pop
PARAMS ((int x
));
150 /* We assume that no more than 16 bits are used. */
151 x
= ((x
& ~0x5555) >> 1) + (x
& 0x5555);
152 x
= ((x
& ~0x3333) >> 2) + (x
& 0x3333);
153 x
= ((x
>> 4) + x
) & 0x0f0f;
154 x
= ((x
>> 8) + x
) & 0xff;
161 struct loaded_l10nfile
*
162 _nl_make_l10nflist (l10nfile_list
, dirlist
, dirlist_len
, mask
, language
,
163 territory
, codeset
, normalized_codeset
, modifier
,
164 filename
, do_allocate
)
165 struct loaded_l10nfile
**l10nfile_list
;
169 const char *language
;
170 const char *territory
;
172 const char *normalized_codeset
;
173 const char *modifier
;
174 const char *filename
;
178 struct loaded_l10nfile
*last
= NULL
;
179 struct loaded_l10nfile
*retval
;
184 /* Allocate room for the full file name. */
185 abs_filename
= (char *) malloc (dirlist_len
187 + ((mask
& XPG_TERRITORY
) != 0
188 ? strlen (territory
) + 1 : 0)
189 + ((mask
& XPG_CODESET
) != 0
190 ? strlen (codeset
) + 1 : 0)
191 + ((mask
& XPG_NORM_CODESET
) != 0
192 ? strlen (normalized_codeset
) + 1 : 0)
193 + ((mask
& XPG_MODIFIER
) != 0
194 ? strlen (modifier
) + 1 : 0)
195 + 1 + strlen (filename
) + 1);
197 if (abs_filename
== NULL
)
203 /* Construct file name. */
204 memcpy (abs_filename
, dirlist
, dirlist_len
);
205 __argz_stringify (abs_filename
, dirlist_len
, ':');
206 cp
= abs_filename
+ (dirlist_len
- 1);
208 cp
= stpcpy (cp
, language
);
210 if ((mask
& XPG_TERRITORY
) != 0)
213 cp
= stpcpy (cp
, territory
);
215 if ((mask
& XPG_CODESET
) != 0)
218 cp
= stpcpy (cp
, codeset
);
220 if ((mask
& XPG_NORM_CODESET
) != 0)
223 cp
= stpcpy (cp
, normalized_codeset
);
225 if ((mask
& XPG_MODIFIER
) != 0)
228 cp
= stpcpy (cp
, modifier
);
232 stpcpy (cp
, filename
);
234 /* Look in list of already loaded domains whether it is already
237 for (retval
= *l10nfile_list
; retval
!= NULL
; retval
= retval
->next
)
238 if (retval
->filename
!= NULL
)
240 int compare
= strcmp (retval
->filename
, abs_filename
);
246 /* It's not in the list. */
254 if (retval
!= NULL
|| do_allocate
== 0)
260 retval
= (struct loaded_l10nfile
*)
261 malloc (sizeof (*retval
) + (__argz_count (dirlist
, dirlist_len
)
263 * sizeof (struct loaded_l10nfile
*)));
270 retval
->filename
= abs_filename
;
271 /* If more than one directory is in the list this is a pseudo-entry
272 which just references others. We do not try to load data for it,
274 retval
->decided
= (__argz_count (dirlist
, dirlist_len
) != 1
275 || ((mask
& XPG_CODESET
) != 0
276 && (mask
& XPG_NORM_CODESET
) != 0));
281 retval
->next
= *l10nfile_list
;
282 *l10nfile_list
= retval
;
286 retval
->next
= last
->next
;
291 /* If the DIRLIST is a real list the RETVAL entry corresponds not to
292 a real file. So we have to use the DIRLIST separation mechanism
293 of the inner loop. */
294 cnt
= __argz_count (dirlist
, dirlist_len
) == 1 ? mask
- 1 : mask
;
295 for (; cnt
>= 0; --cnt
)
296 if ((cnt
& ~mask
) == 0)
298 /* Iterate over all elements of the DIRLIST. */
301 while ((dir
= __argz_next ((char *) dirlist
, dirlist_len
, dir
))
303 retval
->successor
[entries
++]
304 = _nl_make_l10nflist (l10nfile_list
, dir
, strlen (dir
) + 1, cnt
,
305 language
, territory
, codeset
,
306 normalized_codeset
, modifier
, filename
, 1);
308 retval
->successor
[entries
] = NULL
;
313 /* Normalize codeset name. There is no standard for the codeset
314 names. Normalization allows the user to use any of the common
315 names. The return value is dynamically allocated and has to be
316 freed by the caller. */
318 _nl_normalize_codeset (codeset
, name_len
)
328 locale_t locale
= newlocale (0, "C", NULL
);
330 # define locale _nl_C_locobj_ptr
333 for (cnt
= 0; cnt
< name_len
; ++cnt
)
334 if (__isalnum_l ((unsigned char) codeset
[cnt
], locale
))
338 if (! __isdigit_l ((unsigned char) codeset
[cnt
], locale
))
342 retval
= (char *) malloc ((only_digit
? 3 : 0) + len
+ 1);
348 wp
= stpcpy (wp
, "iso");
350 for (cnt
= 0; cnt
< name_len
; ++cnt
)
351 if (__isalpha_l ((unsigned char) codeset
[cnt
], locale
))
352 *wp
++ = __tolower_l ((unsigned char) codeset
[cnt
], locale
);
353 else if (__isdigit_l ((unsigned char) codeset
[cnt
], locale
))
354 *wp
++ = codeset
[cnt
];
359 return (const char *) retval
;
363 /* @@ begin of epilog @@ */
365 /* We don't want libintl.a to depend on any other library. So we
366 avoid the non-standard function stpcpy. In GNU C Library this
367 function is available, though. Also allow the symbol HAVE_STPCPY
369 #if !_LIBC && !HAVE_STPCPY
375 while ((*dest
++ = *src
++) != '\0')