1 /* Copyright (C) 1991-2014 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
];
275 /* Copy of the locale argument, for in-place splitting. */
276 char *locale_copy
= NULL
;
278 /* Set all name pointers to the argument name. */
279 for (category
= 0; category
< __LC_LAST
; ++category
)
280 if (category
!= LC_ALL
)
281 newnames
[category
] = (char *) locale
;
283 if (__glibc_unlikely (strchr (locale
, ';') != NULL
))
285 /* This is a composite name. Make a copy and split it up. */
286 locale_copy
= strdup (locale
);
287 if (__glibc_unlikely (locale_copy
== NULL
))
289 __libc_rwlock_unlock (__libc_setlocale_lock
);
292 char *np
= locale_copy
;
296 while ((cp
= strchr (np
, '=')) != NULL
)
298 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
300 && (size_t) (cp
- np
) == _nl_category_name_sizes
[cnt
]
301 && (memcmp (np
, (_nl_category_names
.str
302 + _nl_category_name_idxs
[cnt
]), cp
- np
)
306 if (cnt
== __LC_LAST
)
309 __libc_rwlock_unlock (__libc_setlocale_lock
);
312 /* Bogus category name. */
316 /* Found the category this clause sets. */
317 newnames
[cnt
] = ++cp
;
318 cp
= strchr (cp
, ';');
321 /* Examine the next clause. */
326 /* This was the last clause. We are done. */
330 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
331 if (cnt
!= LC_ALL
&& newnames
[cnt
] == locale
)
332 /* The composite name did not specify all categories. */
336 /* Load the new data for each category. */
337 while (category
-- > 0)
338 if (category
!= LC_ALL
)
340 newdata
[category
] = _nl_find_locale (locale_path
, locale_path_len
,
342 &newnames
[category
]);
344 if (newdata
[category
] == NULL
)
346 #ifdef NL_CURRENT_INDIRECT
347 if (newnames
[category
] == _nl_C_name
)
348 /* Null because it's the weak value of _nl_C_LC_FOO. */
354 /* We must not simply free a global locale since we have
355 no control over the usage. So we mark it as
356 un-deletable. And yes, the 'if' is needed, the data
357 might be in read-only memory. */
358 if (newdata
[category
]->usage_count
!= UNDELETABLE
)
359 newdata
[category
]->usage_count
= UNDELETABLE
;
361 /* Make a copy of locale name. */
362 if (newnames
[category
] != _nl_C_name
)
364 if (strcmp (newnames
[category
],
365 _nl_global_locale
.__names
[category
]) == 0)
366 newnames
[category
] = _nl_global_locale
.__names
[category
];
369 newnames
[category
] = __strdup (newnames
[category
]);
370 if (newnames
[category
] == NULL
)
376 /* Create new composite name. */
377 composite
= (category
>= 0
378 ? NULL
: new_composite_name (LC_ALL
, newnames
));
379 if (composite
!= NULL
)
381 /* Now we have loaded all the new data. Put it in place. */
382 for (category
= 0; category
< __LC_LAST
; ++category
)
383 if (category
!= LC_ALL
)
385 setdata (category
, newdata
[category
]);
386 setname (category
, newnames
[category
]);
388 setname (LC_ALL
, composite
);
390 /* We successfully loaded a new locale. Let the message catalog
391 functions know about this. */
395 for (++category
; category
< __LC_LAST
; ++category
)
396 if (category
!= LC_ALL
&& newnames
[category
] != _nl_C_name
397 && newnames
[category
] != _nl_global_locale
.__names
[category
])
398 free ((char *) newnames
[category
]);
400 /* Critical section left. */
401 __libc_rwlock_unlock (__libc_setlocale_lock
);
403 /* Free the resources. */
411 struct __locale_data
*newdata
= NULL
;
412 const char *newname
[1] = { locale
};
414 if (CATEGORY_USED (category
))
416 /* Only actually load the data if anything will use it. */
417 newdata
= _nl_find_locale (locale_path
, locale_path_len
, category
,
422 /* We must not simply free a global locale since we have no
423 control over the usage. So we mark it as un-deletable.
425 Note: do not remove the `if', it's necessary to cope with
426 the builtin locale data. */
427 if (newdata
->usage_count
!= UNDELETABLE
)
428 newdata
->usage_count
= UNDELETABLE
;
431 /* Make a copy of locale name. */
432 if (newname
[0] != _nl_C_name
)
434 newname
[0] = __strdup (newname
[0]);
435 if (newname
[0] == NULL
)
439 /* Create new composite name. */
440 composite
= new_composite_name (category
, newname
);
441 if (composite
== NULL
)
443 if (newname
[0] != _nl_C_name
)
444 free ((char *) newname
[0]);
446 /* Say that we don't have any data loaded. */
452 if (CATEGORY_USED (category
))
453 setdata (category
, newdata
);
455 setname (category
, newname
[0]);
456 setname (LC_ALL
, composite
);
458 /* We successfully loaded a new locale. Let the message catalog
459 functions know about this. */
463 /* Critical section left. */
464 __libc_rwlock_unlock (__libc_setlocale_lock
);
466 /* Free the resources (the locale path variable. */
469 return (char *) newname
[0];
472 libc_hidden_def (setlocale
)
474 static void __libc_freeres_fn_section
475 free_category (int category
,
476 struct __locale_data
*here
, struct __locale_data
*c_data
)
478 struct loaded_l10nfile
*runp
= _nl_locale_file_list
[category
];
480 /* If this category is already "C" don't do anything. */
483 /* We have to be prepared that sometime later we still
484 might need the locale information. */
485 setdata (category
, c_data
);
486 setname (category
, _nl_C_name
);
491 struct loaded_l10nfile
*curr
= runp
;
492 struct __locale_data
*data
= (struct __locale_data
*) runp
->data
;
494 if (data
!= NULL
&& data
!= c_data
)
495 _nl_unload_locale (data
);
497 free ((char *) curr
->filename
);
502 /* This is called from iconv/gconv_db.c's free_mem, as locales must
503 be freed before freeing gconv steps arrays. */
504 void __libc_freeres_fn_section
505 _nl_locale_subfreeres (void)
507 #ifdef NL_CURRENT_INDIRECT
508 /* We don't use the loop because we want to have individual weak
509 symbol references here. */
510 # define DEFINE_CATEGORY(category, category_name, items, a) \
511 if (CATEGORY_USED (category)) \
513 extern struct __locale_data _nl_C_##category; \
514 weak_extern (_nl_C_##category) \
515 free_category (category, *_nl_current_##category, &_nl_C_##category); \
517 # include "categories.def"
518 # undef DEFINE_CATEGORY
522 for (category
= 0; category
< __LC_LAST
; ++category
)
523 if (category
!= LC_ALL
)
524 free_category (category
, _NL_CURRENT_DATA (category
),
525 _nl_C_locobj
.__locales
[category
]);
528 setname (LC_ALL
, _nl_C_name
);
530 /* This frees the data structures associated with the locale archive.
531 The locales from the archive are not in the file list, so we have
532 not called _nl_unload_locale on them above. */
533 _nl_archive_subfreeres ();