1 /* Copyright (C) 1991-2022 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 <https://www.gnu.org/licenses/>. */
21 #include <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 struct catnamestr_t _nl_category_names attribute_hidden
=
70 #define DEFINE_CATEGORY(category, category_name, items, a) \
72 #include "categories.def"
73 #undef DEFINE_CATEGORY
76 const uint8_t _nl_category_name_idxs
[__LC_LAST
] attribute_hidden
=
78 #define DEFINE_CATEGORY(category, category_name, items, a) \
79 [category] = offsetof (struct catnamestr_t, CATNAMEMF (__LINE__)),
80 #include "categories.def"
81 #undef DEFINE_CATEGORY
84 /* An array of their lengths, for convenience. */
85 const uint8_t _nl_category_name_sizes
[] attribute_hidden
=
87 #define DEFINE_CATEGORY(category, category_name, items, a) \
88 [category] = sizeof (category_name) - 1,
89 #include "categories.def"
90 #undef DEFINE_CATEGORY
91 [LC_ALL
] = sizeof ("LC_ALL") - 1
95 #ifdef NL_CURRENT_INDIRECT
96 # define WEAK_POSTLOAD(postload) weak_extern (postload)
98 # define WEAK_POSTLOAD(postload) /* Need strong refs in static linking. */
101 /* Declare the postload functions used below. */
103 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
104 #define DEFINE_CATEGORY(category, category_name, items, postload) \
105 extern void postload (void); WEAK_POSTLOAD (postload)
106 #include "categories.def"
107 #undef DEFINE_CATEGORY
110 /* Define an array indexed by category of postload functions to call after
111 loading and installing that category's data. */
112 static void (*const _nl_category_postload
[]) (void) =
114 #define DEFINE_CATEGORY(category, category_name, items, postload) \
115 [category] = postload,
116 #include "categories.def"
117 #undef DEFINE_CATEGORY
121 /* Lock for protecting global data. */
122 __libc_rwlock_define_initialized (, __libc_setlocale_lock attribute_hidden
)
124 /* Defined in loadmsgcat.c. */
125 extern int _nl_msg_cat_cntr
;
128 /* Use this when we come along an error. */
129 #define ERROR_RETURN \
131 __set_errno (EINVAL); \
136 /* Construct a new composite name. */
138 new_composite_name (int category
, const char **newnames
)
146 for (i
= 0; i
< __LC_LAST
; ++i
)
149 const char *name
= (category
== LC_ALL
? newnames
[i
]
150 : category
== i
? newnames
[0]
151 : _nl_global_locale
.__names
[i
]);
152 last_len
= strlen (name
);
153 cumlen
+= _nl_category_name_sizes
[i
] + 1 + last_len
+ 1;
154 if (same
&& name
!= newnames
[0] && strcmp (name
, newnames
[0]) != 0)
160 /* All the categories use the same name. */
161 if (strcmp (newnames
[0], _nl_C_name
) == 0
162 || strcmp (newnames
[0], _nl_POSIX_name
) == 0)
163 return (char *) _nl_C_name
;
165 new = malloc (last_len
+ 1);
167 return new == NULL
? NULL
: memcpy (new, newnames
[0], last_len
+ 1);
170 new = malloc (cumlen
);
174 for (i
= 0; i
< __LC_LAST
; ++i
)
177 /* Add "CATEGORY=NAME;" to the string. */
178 const char *name
= (category
== LC_ALL
? newnames
[i
]
179 : category
== i
? newnames
[0]
180 : _nl_global_locale
.__names
[i
]);
181 p
= __stpcpy (p
, _nl_category_names_get (i
));
183 p
= __stpcpy (p
, name
);
186 p
[-1] = '\0'; /* Clobber the last ';'. */
191 /* Put NAME in _nl_global_locale.__names. */
193 setname (int category
, const char *name
)
195 if (_nl_global_locale
.__names
[category
] == name
)
198 if (_nl_global_locale
.__names
[category
] != _nl_C_name
)
199 free ((char *) _nl_global_locale
.__names
[category
]);
201 _nl_global_locale
.__names
[category
] = name
;
204 /* Put DATA in *_nl_current[CATEGORY]. */
206 setdata (int category
, struct __locale_data
*data
)
208 if (CATEGORY_USED (category
))
210 _nl_global_locale
.__locales
[category
] = data
;
211 if (_nl_category_postload
[category
])
212 (*_nl_category_postload
[category
]) ();
217 setlocale (int category
, const char *locale
)
220 size_t locale_path_len
;
221 const char *locpath_var
;
224 /* Sanity check for CATEGORY argument. */
225 if (__builtin_expect (category
, 0) < 0
226 || __builtin_expect (category
, 0) >= __LC_LAST
)
229 /* Does user want name of current locale? */
231 return (char *) _nl_global_locale
.__names
[category
];
233 /* Protect global data. */
234 __libc_rwlock_wrlock (__libc_setlocale_lock
);
236 if (strcmp (locale
, _nl_global_locale
.__names
[category
]) == 0)
238 /* Changing to the same thing. */
239 __libc_rwlock_unlock (__libc_setlocale_lock
);
241 return (char *) _nl_global_locale
.__names
[category
];
244 /* We perhaps really have to load some data. So we determine the
245 path in which to look for the data now. The environment variable
246 `LOCPATH' must only be used when the binary has no SUID or SGID
247 bit set. If using the default path, we tell _nl_find_locale
248 by passing null and it can check the canonical locale archive. */
252 locpath_var
= getenv ("LOCPATH");
253 if (locpath_var
!= NULL
&& locpath_var
[0] != '\0')
255 if (__argz_create_sep (locpath_var
, ':',
256 &locale_path
, &locale_path_len
) != 0
257 || __argz_add_sep (&locale_path
, &locale_path_len
,
258 _nl_default_locale_path
, ':') != 0)
260 __libc_rwlock_unlock (__libc_setlocale_lock
);
265 if (category
== LC_ALL
)
267 /* The user wants to set all categories. The desired locales
268 for the individual categories can be selected by using a
269 composite locale name. This is a semi-colon separated list
270 of entries of the form `CATEGORY=VALUE'. */
271 const char *newnames
[__LC_LAST
];
272 struct __locale_data
*newdata
[__LC_LAST
];
273 /* Copy of the locale argument, for in-place splitting. */
274 char *locale_copy
= NULL
;
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 (__glibc_unlikely (strchr (locale
, ';') != NULL
))
283 /* This is a composite name. Make a copy and split it up. */
284 locale_copy
= __strdup (locale
);
285 if (__glibc_unlikely (locale_copy
== NULL
))
287 __libc_rwlock_unlock (__libc_setlocale_lock
);
290 char *np
= locale_copy
;
294 while ((cp
= strchr (np
, '=')) != NULL
)
296 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
298 && (size_t) (cp
- np
) == _nl_category_name_sizes
[cnt
]
299 && (memcmp (np
, (_nl_category_names_get (cnt
)), cp
- np
)
303 if (cnt
== __LC_LAST
)
306 __libc_rwlock_unlock (__libc_setlocale_lock
);
309 /* Bogus category name. */
313 /* Found the category this clause sets. */
314 newnames
[cnt
] = ++cp
;
315 cp
= strchr (cp
, ';');
318 /* Examine the next clause. */
323 /* This was the last clause. We are done. */
327 for (cnt
= 0; cnt
< __LC_LAST
; ++cnt
)
328 if (cnt
!= LC_ALL
&& newnames
[cnt
] == locale
)
329 /* The composite name did not specify all categories. */
333 /* Load the new data for each category. */
334 while (category
-- > 0)
335 if (category
!= LC_ALL
)
337 newdata
[category
] = _nl_find_locale (locale_path
, locale_path_len
,
339 &newnames
[category
]);
341 if (newdata
[category
] == NULL
)
343 #ifdef NL_CURRENT_INDIRECT
344 if (newnames
[category
] == _nl_C_name
)
345 /* Null because it's the weak value of _nl_C_LC_FOO. */
351 /* We must not simply free a global locale since we have
352 no control over the usage. So we mark it as
353 un-deletable. And yes, the 'if' is needed, the data
354 might be in read-only memory. */
355 if (newdata
[category
]->usage_count
!= UNDELETABLE
)
356 newdata
[category
]->usage_count
= UNDELETABLE
;
358 /* Make a copy of locale name. */
359 if (newnames
[category
] != _nl_C_name
)
361 if (strcmp (newnames
[category
],
362 _nl_global_locale
.__names
[category
]) == 0)
363 newnames
[category
] = _nl_global_locale
.__names
[category
];
366 newnames
[category
] = __strdup (newnames
[category
]);
367 if (newnames
[category
] == NULL
)
373 /* Create new composite name. */
374 composite
= (category
>= 0
375 ? NULL
: new_composite_name (LC_ALL
, newnames
));
376 if (composite
!= NULL
)
378 /* Now we have loaded all the new data. Put it in place. */
379 for (category
= 0; category
< __LC_LAST
; ++category
)
380 if (category
!= LC_ALL
)
382 setdata (category
, newdata
[category
]);
383 setname (category
, newnames
[category
]);
385 setname (LC_ALL
, composite
);
387 /* We successfully loaded a new locale. Let the message catalog
388 functions know about this. */
392 for (++category
; category
< __LC_LAST
; ++category
)
393 if (category
!= LC_ALL
&& newnames
[category
] != _nl_C_name
394 && newnames
[category
] != _nl_global_locale
.__names
[category
])
395 free ((char *) newnames
[category
]);
397 /* Critical section left. */
398 __libc_rwlock_unlock (__libc_setlocale_lock
);
400 /* Free the resources. */
408 struct __locale_data
*newdata
= NULL
;
409 const char *newname
[1] = { locale
};
411 if (CATEGORY_USED (category
))
413 /* Only actually load the data if anything will use it. */
414 newdata
= _nl_find_locale (locale_path
, locale_path_len
, category
,
419 /* We must not simply free a global locale since we have no
420 control over the usage. So we mark it as un-deletable.
422 Note: do not remove the `if', it's necessary to cope with
423 the builtin locale data. */
424 if (newdata
->usage_count
!= UNDELETABLE
)
425 newdata
->usage_count
= UNDELETABLE
;
428 /* Make a copy of locale name. */
429 if (newname
[0] != _nl_C_name
)
431 newname
[0] = __strdup (newname
[0]);
432 if (newname
[0] == NULL
)
436 /* Create new composite name. */
437 composite
= new_composite_name (category
, newname
);
438 if (composite
== NULL
)
440 if (newname
[0] != _nl_C_name
)
441 free ((char *) newname
[0]);
443 /* Say that we don't have any data loaded. */
449 if (CATEGORY_USED (category
))
450 setdata (category
, newdata
);
452 setname (category
, newname
[0]);
453 setname (LC_ALL
, composite
);
455 /* We successfully loaded a new locale. Let the message catalog
456 functions know about this. */
460 /* Critical section left. */
461 __libc_rwlock_unlock (__libc_setlocale_lock
);
463 /* Free the resources (the locale path variable. */
466 return (char *) newname
[0];
469 libc_hidden_def (setlocale
)
471 static void __libc_freeres_fn_section
472 free_category (int category
,
473 struct __locale_data
*here
, struct __locale_data
*c_data
)
475 struct loaded_l10nfile
*runp
= _nl_locale_file_list
[category
];
477 /* If this category is already "C" don't do anything. */
480 /* We have to be prepared that sometime later we still
481 might need the locale information. */
482 setdata (category
, c_data
);
483 setname (category
, _nl_C_name
);
488 struct loaded_l10nfile
*curr
= runp
;
489 struct __locale_data
*data
= (struct __locale_data
*) runp
->data
;
491 if (data
!= NULL
&& data
!= c_data
)
492 _nl_unload_locale (category
, data
);
494 free ((char *) curr
->filename
);
499 /* This is called from iconv/gconv_db.c's free_mem, as locales must
500 be freed before freeing gconv steps arrays. */
501 void __libc_freeres_fn_section
502 _nl_locale_subfreeres (void)
504 #ifdef NL_CURRENT_INDIRECT
505 /* We don't use the loop because we want to have individual weak
506 symbol references here. */
507 # define DEFINE_CATEGORY(category, category_name, items, a) \
508 if (CATEGORY_USED (category)) \
510 extern struct __locale_data _nl_C_##category; \
511 weak_extern (_nl_C_##category) \
512 free_category (category, *_nl_current_##category, &_nl_C_##category); \
514 # include "categories.def"
515 # undef DEFINE_CATEGORY
519 for (category
= 0; category
< __LC_LAST
; ++category
)
520 if (category
!= LC_ALL
)
521 free_category (category
, _NL_CURRENT_DATA (category
),
522 _nl_C_locobj
.__locales
[category
]);
525 setname (LC_ALL
, _nl_C_name
);
527 /* This frees the data structures associated with the locale archive.
528 The locales from the archive are not in the file list, so we have
529 not called _nl_unload_locale on them above. */
530 _nl_archive_subfreeres ();