1 /* Copyright (C) 1991, 1992, 1995-2000, 2002, 2003, 2004, 2006, 2008, 2010, 2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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/>. */
22 #include <bits/libc-lock.h>
28 #include "localeinfo.h"
30 #ifdef NL_CURRENT_INDIRECT
32 /* For each category declare a special external symbol
33 _nl_current_CATEGORY_used with a weak reference.
34 This symbol will is defined in lc-CATEGORY.c and will be linked in
35 if anything uses _nl_current_CATEGORY (also defined in that module).
36 Also use a weak reference for the _nl_current_CATEGORY thread variable. */
38 # define DEFINE_CATEGORY(category, category_name, items, a) \
39 extern char _nl_current_##category##_used; \
40 weak_extern (_nl_current_##category##_used) \
41 weak_extern (_nl_current_##category)
42 # include "categories.def"
43 # undef DEFINE_CATEGORY
45 /* Now define a table of flags based on those special weak symbols' values.
46 _nl_current_used[CATEGORY] will be zero if _nl_current_CATEGORY is not
48 static char *const _nl_current_used
[] =
50 # define DEFINE_CATEGORY(category, category_name, items, a) \
51 [category] = &_nl_current_##category##_used,
52 # include "categories.def"
53 # undef DEFINE_CATEGORY
56 # define CATEGORY_USED(category) (_nl_current_used[category] != 0)
60 /* The shared library always loads all the categories,
61 and the current global settings are kept in _nl_global_locale. */
63 # define CATEGORY_USED(category) (1)
68 /* Define an array of category names (also the environment variable names). */
69 const union catnamestr_t _nl_category_names attribute_hidden
=
72 #define DEFINE_CATEGORY(category, category_name, items, a) \
74 #include "categories.def"
75 #undef DEFINE_CATEGORY
79 const uint8_t _nl_category_name_idxs
[__LC_LAST
] attribute_hidden
=
81 #define DEFINE_CATEGORY(category, category_name, items, a) \
82 [category] = offsetof (union catnamestr_t, CATNAMEMF (__LINE__)),
83 #include "categories.def"
84 #undef DEFINE_CATEGORY
87 /* An array of their lengths, for convenience. */
88 const uint8_t _nl_category_name_sizes
[] attribute_hidden
=
90 #define DEFINE_CATEGORY(category, category_name, items, a) \
91 [category] = sizeof (category_name) - 1,
92 #include "categories.def"
93 #undef DEFINE_CATEGORY
94 [LC_ALL
] = sizeof ("LC_ALL") - 1
98 #ifdef NL_CURRENT_INDIRECT
99 # define WEAK_POSTLOAD(postload) weak_extern (postload)
101 # define WEAK_POSTLOAD(postload) /* Need strong refs in static linking. */
104 /* Declare the postload functions used below. */
106 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
107 #define DEFINE_CATEGORY(category, category_name, items, postload) \
108 extern void postload (void); WEAK_POSTLOAD (postload)
109 #include "categories.def"
110 #undef DEFINE_CATEGORY
113 /* Define an array indexed by category of postload functions to call after
114 loading and installing that category's data. */
115 static void (*const _nl_category_postload
[]) (void) =
117 #define DEFINE_CATEGORY(category, category_name, items, postload) \
118 [category] = postload,
119 #include "categories.def"
120 #undef DEFINE_CATEGORY
124 /* Lock for protecting global data. */
125 __libc_rwlock_define_initialized (, __libc_setlocale_lock attribute_hidden
)
127 /* Defined in loadmsgcat.c. */
128 extern int _nl_msg_cat_cntr
;
131 /* Use this when we come along an error. */
132 #define ERROR_RETURN \
134 __set_errno (EINVAL); \
139 /* Construct a new composite name. */
141 new_composite_name (int category
, const char *newnames
[__LC_LAST
])
149 for (i
= 0; i
< __LC_LAST
; ++i
)
152 const char *name
= (category
== LC_ALL
? newnames
[i
] :
153 category
== i
? newnames
[0] :
154 _nl_global_locale
.__names
[i
]);
155 last_len
= strlen (name
);
156 cumlen
+= _nl_category_name_sizes
[i
] + 1 + last_len
+ 1;
157 if (same
&& name
!= newnames
[0] && strcmp (name
, newnames
[0]) != 0)
163 /* All the categories use the same name. */
164 if (strcmp (newnames
[0], _nl_C_name
) == 0
165 || strcmp (newnames
[0], _nl_POSIX_name
) == 0)
166 return (char *) _nl_C_name
;
168 new = malloc (last_len
+ 1);
170 return new == NULL
? NULL
: memcpy (new, newnames
[0], last_len
+ 1);
173 new = malloc (cumlen
);
177 for (i
= 0; i
< __LC_LAST
; ++i
)
180 /* Add "CATEGORY=NAME;" to the string. */
181 const char *name
= (category
== LC_ALL
? newnames
[i
] :
182 category
== i
? newnames
[0] :
183 _nl_global_locale
.__names
[i
]);
184 p
= __stpcpy (p
, _nl_category_names
.str
+ _nl_category_name_idxs
[i
]);
186 p
= __stpcpy (p
, name
);
189 p
[-1] = '\0'; /* Clobber the last ';'. */
194 /* Put NAME in _nl_global_locale.__names. */
196 setname (int category
, const char *name
)
198 if (_nl_global_locale
.__names
[category
] == name
)
201 if (_nl_global_locale
.__names
[category
] != _nl_C_name
)
202 free ((char *) _nl_global_locale
.__names
[category
]);
204 _nl_global_locale
.__names
[category
] = name
;
207 /* Put DATA in *_nl_current[CATEGORY]. */
209 setdata (int category
, struct __locale_data
*data
)
211 if (CATEGORY_USED (category
))
213 _nl_global_locale
.__locales
[category
] = data
;
214 if (_nl_category_postload
[category
])
215 (*_nl_category_postload
[category
]) ();
220 setlocale (int category
, const char *locale
)
223 size_t locale_path_len
;
224 const char *locpath_var
;
227 /* Sanity check for CATEGORY argument. */
228 if (__builtin_expect (category
, 0) < 0
229 || __builtin_expect (category
, 0) >= __LC_LAST
)
232 /* Does user want name of current locale? */
234 return (char *) _nl_global_locale
.__names
[category
];
236 /* Protect global data. */
237 __libc_rwlock_wrlock (__libc_setlocale_lock
);
239 if (strcmp (locale
, _nl_global_locale
.__names
[category
]) == 0)
241 /* Changing to the same thing. */
242 __libc_rwlock_unlock (__libc_setlocale_lock
);
244 return (char *) _nl_global_locale
.__names
[category
];
247 /* We perhaps really have to load some data. So we determine the
248 path in which to look for the data now. The environment variable
249 `LOCPATH' must only be used when the binary has no SUID or SGID
250 bit set. If using the default path, we tell _nl_find_locale
251 by passing null and it can check the canonical locale archive. */
255 locpath_var
= getenv ("LOCPATH");
256 if (locpath_var
!= NULL
&& locpath_var
[0] != '\0')
258 if (__argz_create_sep (locpath_var
, ':',
259 &locale_path
, &locale_path_len
) != 0
260 || __argz_add_sep (&locale_path
, &locale_path_len
,
261 _nl_default_locale_path
, ':') != 0)
263 __libc_rwlock_unlock (__libc_setlocale_lock
);
268 if (category
== LC_ALL
)
270 /* The user wants to set all categories. The desired locales
271 for the individual categories can be selected by using a
272 composite locale name. This is a semi-colon separated list
273 of entries of the form `CATEGORY=VALUE'. */
274 const char *newnames
[__LC_LAST
];
275 struct __locale_data
*newdata
[__LC_LAST
];
277 /* Set all name pointers to the argument name. */
278 for (category
= 0; category
< __LC_LAST
; ++category
)
279 if (category
!= LC_ALL
)
280 newnames
[category
] = (char *) locale
;
282 if (__builtin_expect (strchr (locale
, ';') != NULL
, 0))
284 /* This is a composite name. Make a copy and split it up. */
285 char *np
= strdupa (locale
);
289 while ((cp
= strchr (np
, '=')) != NULL
)
291 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
293 && (size_t) (cp
- np
) == _nl_category_name_sizes
[cnt
]
294 && (memcmp (np
, (_nl_category_names
.str
295 + _nl_category_name_idxs
[cnt
]), cp
- np
)
299 if (cnt
== __LC_LAST
)
302 __libc_rwlock_unlock (__libc_setlocale_lock
);
304 /* Bogus category name. */
308 /* Found the category this clause sets. */
309 newnames
[cnt
] = ++cp
;
310 cp
= strchr (cp
, ';');
313 /* Examine the next clause. */
318 /* This was the last clause. We are done. */
322 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
323 if (cnt
!= LC_ALL
&& newnames
[cnt
] == locale
)
324 /* The composite name did not specify all categories. */
328 /* Load the new data for each category. */
329 while (category
-- > 0)
330 if (category
!= LC_ALL
)
332 newdata
[category
] = _nl_find_locale (locale_path
, locale_path_len
,
334 &newnames
[category
]);
336 if (newdata
[category
] == NULL
)
338 #ifdef NL_CURRENT_INDIRECT
339 if (newnames
[category
] == _nl_C_name
)
340 /* Null because it's the weak value of _nl_C_LC_FOO. */
346 /* We must not simply free a global locale since we have
347 no control over the usage. So we mark it as
348 un-deletable. And yes, the 'if' is needed, the data
349 might be in read-only memory. */
350 if (newdata
[category
]->usage_count
!= UNDELETABLE
)
351 newdata
[category
]->usage_count
= UNDELETABLE
;
353 /* Make a copy of locale name. */
354 if (newnames
[category
] != _nl_C_name
)
356 if (strcmp (newnames
[category
],
357 _nl_global_locale
.__names
[category
]) == 0)
358 newnames
[category
] = _nl_global_locale
.__names
[category
];
361 newnames
[category
] = __strdup (newnames
[category
]);
362 if (newnames
[category
] == NULL
)
368 /* Create new composite name. */
369 composite
= (category
>= 0
370 ? NULL
: new_composite_name (LC_ALL
, newnames
));
371 if (composite
!= NULL
)
373 /* Now we have loaded all the new data. Put it in place. */
374 for (category
= 0; category
< __LC_LAST
; ++category
)
375 if (category
!= LC_ALL
)
377 setdata (category
, newdata
[category
]);
378 setname (category
, newnames
[category
]);
380 setname (LC_ALL
, composite
);
382 /* We successfully loaded a new locale. Let the message catalog
383 functions know about this. */
387 for (++category
; category
< __LC_LAST
; ++category
)
388 if (category
!= LC_ALL
&& newnames
[category
] != _nl_C_name
389 && newnames
[category
] != _nl_global_locale
.__names
[category
])
390 free ((char *) newnames
[category
]);
392 /* Critical section left. */
393 __libc_rwlock_unlock (__libc_setlocale_lock
);
395 /* Free the resources (the locale path variable). */
402 struct __locale_data
*newdata
= NULL
;
403 const char *newname
[1] = { locale
};
405 if (CATEGORY_USED (category
))
407 /* Only actually load the data if anything will use it. */
408 newdata
= _nl_find_locale (locale_path
, locale_path_len
, category
,
413 /* We must not simply free a global locale since we have no
414 control over the usage. So we mark it as un-deletable.
416 Note: do not remove the `if', it's necessary to copy with
417 the builtin locale data. */
418 if (newdata
->usage_count
!= UNDELETABLE
)
419 newdata
->usage_count
= UNDELETABLE
;
422 /* Make a copy of locale name. */
423 if (newname
[0] != _nl_C_name
)
425 newname
[0] = __strdup (newname
[0]);
426 if (newname
[0] == NULL
)
430 /* Create new composite name. */
431 composite
= new_composite_name (category
, newname
);
432 if (composite
== NULL
)
434 if (newname
[0] != _nl_C_name
)
435 free ((char *) newname
[0]);
437 /* Say that we don't have any data loaded. */
443 if (CATEGORY_USED (category
))
444 setdata (category
, newdata
);
446 setname (category
, newname
[0]);
447 setname (LC_ALL
, composite
);
449 /* We successfully loaded a new locale. Let the message catalog
450 functions know about this. */
454 /* Critical section left. */
455 __libc_rwlock_unlock (__libc_setlocale_lock
);
457 /* Free the resources (the locale path variable. */
460 return (char *) newname
[0];
463 libc_hidden_def (setlocale
)
465 static void __libc_freeres_fn_section
466 free_category (int category
,
467 struct __locale_data
*here
, struct __locale_data
*c_data
)
469 struct loaded_l10nfile
*runp
= _nl_locale_file_list
[category
];
471 /* If this category is already "C" don't do anything. */
474 /* We have to be prepared that sometime later we still
475 might need the locale information. */
476 setdata (category
, c_data
);
477 setname (category
, _nl_C_name
);
482 struct loaded_l10nfile
*curr
= runp
;
483 struct __locale_data
*data
= (struct __locale_data
*) runp
->data
;
485 if (data
!= NULL
&& data
!= c_data
)
486 _nl_unload_locale (data
);
488 free ((char *) curr
->filename
);
493 /* This is called from iconv/gconv_db.c's free_mem, as locales must
494 be freed before freeing gconv steps arrays. */
495 void __libc_freeres_fn_section
496 _nl_locale_subfreeres (void)
498 #ifdef NL_CURRENT_INDIRECT
499 /* We don't use the loop because we want to have individual weak
500 symbol references here. */
501 # define DEFINE_CATEGORY(category, category_name, items, a) \
502 if (CATEGORY_USED (category)) \
504 extern struct __locale_data _nl_C_##category; \
505 weak_extern (_nl_C_##category) \
506 free_category (category, *_nl_current_##category, &_nl_C_##category); \
508 # include "categories.def"
509 # undef DEFINE_CATEGORY
513 for (category
= 0; category
< __LC_LAST
; ++category
)
514 if (category
!= LC_ALL
)
515 free_category (category
, _NL_CURRENT_DATA (category
),
516 _nl_C_locobj
.__locales
[category
]);
519 setname (LC_ALL
, _nl_C_name
);
521 /* This frees the data structures associated with the locale archive.
522 The locales from the archive are not in the file list, so we have
523 not called _nl_unload_locale on them above. */
524 _nl_archive_subfreeres ();