1 /* Copyright (C) 1991-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
21 #include <bits/libc-lock.h>
27 #include "localeinfo.h"
29 #ifdef NL_CURRENT_INDIRECT
31 /* For each category declare a special external symbol
32 _nl_current_CATEGORY_used with a weak reference.
33 This symbol will is defined in lc-CATEGORY.c and will be linked in
34 if anything uses _nl_current_CATEGORY (also defined in that module).
35 Also use a weak reference for the _nl_current_CATEGORY thread variable. */
37 # define DEFINE_CATEGORY(category, category_name, items, a) \
38 extern char _nl_current_##category##_used; \
39 weak_extern (_nl_current_##category##_used) \
40 weak_extern (_nl_current_##category)
41 # include "categories.def"
42 # undef DEFINE_CATEGORY
44 /* Now define a table of flags based on those special weak symbols' values.
45 _nl_current_used[CATEGORY] will be zero if _nl_current_CATEGORY is not
47 static char *const _nl_current_used
[] =
49 # define DEFINE_CATEGORY(category, category_name, items, a) \
50 [category] = &_nl_current_##category##_used,
51 # include "categories.def"
52 # undef DEFINE_CATEGORY
55 # define CATEGORY_USED(category) (_nl_current_used[category] != 0)
59 /* The shared library always loads all the categories,
60 and the current global settings are kept in _nl_global_locale. */
62 # define CATEGORY_USED(category) (1)
67 /* Define an array of category names (also the environment variable names). */
68 const union catnamestr_t _nl_category_names attribute_hidden
=
71 #define DEFINE_CATEGORY(category, category_name, items, a) \
73 #include "categories.def"
74 #undef DEFINE_CATEGORY
78 const uint8_t _nl_category_name_idxs
[__LC_LAST
] attribute_hidden
=
80 #define DEFINE_CATEGORY(category, category_name, items, a) \
81 [category] = offsetof (union catnamestr_t, CATNAMEMF (__LINE__)),
82 #include "categories.def"
83 #undef DEFINE_CATEGORY
86 /* An array of their lengths, for convenience. */
87 const uint8_t _nl_category_name_sizes
[] attribute_hidden
=
89 #define DEFINE_CATEGORY(category, category_name, items, a) \
90 [category] = sizeof (category_name) - 1,
91 #include "categories.def"
92 #undef DEFINE_CATEGORY
93 [LC_ALL
] = sizeof ("LC_ALL") - 1
97 #ifdef NL_CURRENT_INDIRECT
98 # define WEAK_POSTLOAD(postload) weak_extern (postload)
100 # define WEAK_POSTLOAD(postload) /* Need strong refs in static linking. */
103 /* Declare the postload functions used below. */
105 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
106 #define DEFINE_CATEGORY(category, category_name, items, postload) \
107 extern void postload (void); WEAK_POSTLOAD (postload)
108 #include "categories.def"
109 #undef DEFINE_CATEGORY
112 /* Define an array indexed by category of postload functions to call after
113 loading and installing that category's data. */
114 static void (*const _nl_category_postload
[]) (void) =
116 #define DEFINE_CATEGORY(category, category_name, items, postload) \
117 [category] = postload,
118 #include "categories.def"
119 #undef DEFINE_CATEGORY
123 /* Lock for protecting global data. */
124 __libc_rwlock_define_initialized (, __libc_setlocale_lock attribute_hidden
)
126 /* Defined in loadmsgcat.c. */
127 extern int _nl_msg_cat_cntr
;
130 /* Use this when we come along an error. */
131 #define ERROR_RETURN \
133 __set_errno (EINVAL); \
138 /* Construct a new composite name. */
140 new_composite_name (int category
, const char *newnames
[__LC_LAST
])
148 for (i
= 0; i
< __LC_LAST
; ++i
)
151 const char *name
= (category
== LC_ALL
? newnames
[i
] :
152 category
== i
? newnames
[0] :
153 _nl_global_locale
.__names
[i
]);
154 last_len
= strlen (name
);
155 cumlen
+= _nl_category_name_sizes
[i
] + 1 + last_len
+ 1;
156 if (same
&& name
!= newnames
[0] && strcmp (name
, newnames
[0]) != 0)
162 /* All the categories use the same name. */
163 if (strcmp (newnames
[0], _nl_C_name
) == 0
164 || strcmp (newnames
[0], _nl_POSIX_name
) == 0)
165 return (char *) _nl_C_name
;
167 new = malloc (last_len
+ 1);
169 return new == NULL
? NULL
: memcpy (new, newnames
[0], last_len
+ 1);
172 new = malloc (cumlen
);
176 for (i
= 0; i
< __LC_LAST
; ++i
)
179 /* Add "CATEGORY=NAME;" to the string. */
180 const char *name
= (category
== LC_ALL
? newnames
[i
] :
181 category
== i
? newnames
[0] :
182 _nl_global_locale
.__names
[i
]);
183 p
= __stpcpy (p
, _nl_category_names
.str
+ _nl_category_name_idxs
[i
]);
185 p
= __stpcpy (p
, name
);
188 p
[-1] = '\0'; /* Clobber the last ';'. */
193 /* Put NAME in _nl_global_locale.__names. */
195 setname (int category
, const char *name
)
197 if (_nl_global_locale
.__names
[category
] == name
)
200 if (_nl_global_locale
.__names
[category
] != _nl_C_name
)
201 free ((char *) _nl_global_locale
.__names
[category
]);
203 _nl_global_locale
.__names
[category
] = name
;
206 /* Put DATA in *_nl_current[CATEGORY]. */
208 setdata (int category
, struct __locale_data
*data
)
210 if (CATEGORY_USED (category
))
212 _nl_global_locale
.__locales
[category
] = data
;
213 if (_nl_category_postload
[category
])
214 (*_nl_category_postload
[category
]) ();
219 setlocale (int category
, const char *locale
)
222 size_t locale_path_len
;
223 const char *locpath_var
;
226 /* Sanity check for CATEGORY argument. */
227 if (__builtin_expect (category
, 0) < 0
228 || __builtin_expect (category
, 0) >= __LC_LAST
)
231 /* Does user want name of current locale? */
233 return (char *) _nl_global_locale
.__names
[category
];
235 /* Protect global data. */
236 __libc_rwlock_wrlock (__libc_setlocale_lock
);
238 if (strcmp (locale
, _nl_global_locale
.__names
[category
]) == 0)
240 /* Changing to the same thing. */
241 __libc_rwlock_unlock (__libc_setlocale_lock
);
243 return (char *) _nl_global_locale
.__names
[category
];
246 /* We perhaps really have to load some data. So we determine the
247 path in which to look for the data now. The environment variable
248 `LOCPATH' must only be used when the binary has no SUID or SGID
249 bit set. If using the default path, we tell _nl_find_locale
250 by passing null and it can check the canonical locale archive. */
254 locpath_var
= getenv ("LOCPATH");
255 if (locpath_var
!= NULL
&& locpath_var
[0] != '\0')
257 if (__argz_create_sep (locpath_var
, ':',
258 &locale_path
, &locale_path_len
) != 0
259 || __argz_add_sep (&locale_path
, &locale_path_len
,
260 _nl_default_locale_path
, ':') != 0)
262 __libc_rwlock_unlock (__libc_setlocale_lock
);
267 if (category
== LC_ALL
)
269 /* The user wants to set all categories. The desired locales
270 for the individual categories can be selected by using a
271 composite locale name. This is a semi-colon separated list
272 of entries of the form `CATEGORY=VALUE'. */
273 const char *newnames
[__LC_LAST
];
274 struct __locale_data
*newdata
[__LC_LAST
];
276 /* Set all name pointers to the argument name. */
277 for (category
= 0; category
< __LC_LAST
; ++category
)
278 if (category
!= LC_ALL
)
279 newnames
[category
] = (char *) locale
;
281 if (__builtin_expect (strchr (locale
, ';') != NULL
, 0))
283 /* This is a composite name. Make a copy and split it up. */
284 char *np
= strdupa (locale
);
288 while ((cp
= strchr (np
, '=')) != NULL
)
290 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
292 && (size_t) (cp
- np
) == _nl_category_name_sizes
[cnt
]
293 && (memcmp (np
, (_nl_category_names
.str
294 + _nl_category_name_idxs
[cnt
]), cp
- np
)
298 if (cnt
== __LC_LAST
)
301 __libc_rwlock_unlock (__libc_setlocale_lock
);
303 /* Bogus category name. */
307 /* Found the category this clause sets. */
308 newnames
[cnt
] = ++cp
;
309 cp
= strchr (cp
, ';');
312 /* Examine the next clause. */
317 /* This was the last clause. We are done. */
321 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
322 if (cnt
!= LC_ALL
&& newnames
[cnt
] == locale
)
323 /* The composite name did not specify all categories. */
327 /* Load the new data for each category. */
328 while (category
-- > 0)
329 if (category
!= LC_ALL
)
331 newdata
[category
] = _nl_find_locale (locale_path
, locale_path_len
,
333 &newnames
[category
]);
335 if (newdata
[category
] == NULL
)
337 #ifdef NL_CURRENT_INDIRECT
338 if (newnames
[category
] == _nl_C_name
)
339 /* Null because it's the weak value of _nl_C_LC_FOO. */
345 /* We must not simply free a global locale since we have
346 no control over the usage. So we mark it as
347 un-deletable. And yes, the 'if' is needed, the data
348 might be in read-only memory. */
349 if (newdata
[category
]->usage_count
!= UNDELETABLE
)
350 newdata
[category
]->usage_count
= UNDELETABLE
;
352 /* Make a copy of locale name. */
353 if (newnames
[category
] != _nl_C_name
)
355 if (strcmp (newnames
[category
],
356 _nl_global_locale
.__names
[category
]) == 0)
357 newnames
[category
] = _nl_global_locale
.__names
[category
];
360 newnames
[category
] = __strdup (newnames
[category
]);
361 if (newnames
[category
] == NULL
)
367 /* Create new composite name. */
368 composite
= (category
>= 0
369 ? NULL
: new_composite_name (LC_ALL
, newnames
));
370 if (composite
!= NULL
)
372 /* Now we have loaded all the new data. Put it in place. */
373 for (category
= 0; category
< __LC_LAST
; ++category
)
374 if (category
!= LC_ALL
)
376 setdata (category
, newdata
[category
]);
377 setname (category
, newnames
[category
]);
379 setname (LC_ALL
, composite
);
381 /* We successfully loaded a new locale. Let the message catalog
382 functions know about this. */
386 for (++category
; category
< __LC_LAST
; ++category
)
387 if (category
!= LC_ALL
&& newnames
[category
] != _nl_C_name
388 && newnames
[category
] != _nl_global_locale
.__names
[category
])
389 free ((char *) newnames
[category
]);
391 /* Critical section left. */
392 __libc_rwlock_unlock (__libc_setlocale_lock
);
394 /* Free the resources (the locale path variable). */
401 struct __locale_data
*newdata
= NULL
;
402 const char *newname
[1] = { locale
};
404 if (CATEGORY_USED (category
))
406 /* Only actually load the data if anything will use it. */
407 newdata
= _nl_find_locale (locale_path
, locale_path_len
, category
,
412 /* We must not simply free a global locale since we have no
413 control over the usage. So we mark it as un-deletable.
415 Note: do not remove the `if', it's necessary to copy with
416 the builtin locale data. */
417 if (newdata
->usage_count
!= UNDELETABLE
)
418 newdata
->usage_count
= UNDELETABLE
;
421 /* Make a copy of locale name. */
422 if (newname
[0] != _nl_C_name
)
424 newname
[0] = __strdup (newname
[0]);
425 if (newname
[0] == NULL
)
429 /* Create new composite name. */
430 composite
= new_composite_name (category
, newname
);
431 if (composite
== NULL
)
433 if (newname
[0] != _nl_C_name
)
434 free ((char *) newname
[0]);
436 /* Say that we don't have any data loaded. */
442 if (CATEGORY_USED (category
))
443 setdata (category
, newdata
);
445 setname (category
, newname
[0]);
446 setname (LC_ALL
, composite
);
448 /* We successfully loaded a new locale. Let the message catalog
449 functions know about this. */
453 /* Critical section left. */
454 __libc_rwlock_unlock (__libc_setlocale_lock
);
456 /* Free the resources (the locale path variable. */
459 return (char *) newname
[0];
462 libc_hidden_def (setlocale
)
464 static void __libc_freeres_fn_section
465 free_category (int category
,
466 struct __locale_data
*here
, struct __locale_data
*c_data
)
468 struct loaded_l10nfile
*runp
= _nl_locale_file_list
[category
];
470 /* If this category is already "C" don't do anything. */
473 /* We have to be prepared that sometime later we still
474 might need the locale information. */
475 setdata (category
, c_data
);
476 setname (category
, _nl_C_name
);
481 struct loaded_l10nfile
*curr
= runp
;
482 struct __locale_data
*data
= (struct __locale_data
*) runp
->data
;
484 if (data
!= NULL
&& data
!= c_data
)
485 _nl_unload_locale (data
);
487 free ((char *) curr
->filename
);
492 /* This is called from iconv/gconv_db.c's free_mem, as locales must
493 be freed before freeing gconv steps arrays. */
494 void __libc_freeres_fn_section
495 _nl_locale_subfreeres (void)
497 #ifdef NL_CURRENT_INDIRECT
498 /* We don't use the loop because we want to have individual weak
499 symbol references here. */
500 # define DEFINE_CATEGORY(category, category_name, items, a) \
501 if (CATEGORY_USED (category)) \
503 extern struct __locale_data _nl_C_##category; \
504 weak_extern (_nl_C_##category) \
505 free_category (category, *_nl_current_##category, &_nl_C_##category); \
507 # include "categories.def"
508 # undef DEFINE_CATEGORY
512 for (category
= 0; category
< __LC_LAST
; ++category
)
513 if (category
!= LC_ALL
)
514 free_category (category
, _NL_CURRENT_DATA (category
),
515 _nl_C_locobj
.__locales
[category
]);
518 setname (LC_ALL
, _nl_C_name
);
520 /* This frees the data structures associated with the locale archive.
521 The locales from the archive are not in the file list, so we have
522 not called _nl_unload_locale on them above. */
523 _nl_archive_subfreeres ();