Blame locale/uselocale.c

Packit 6c4009
/* uselocale -- fetch and set the current per-thread locale
Packit 6c4009
   Copyright (C) 2002-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include "localeinfo.h"
Packit 6c4009
#include <ctype.h>
Packit 6c4009
Packit 6c4009
/* Switch the current thread's locale to DATASET.
Packit 6c4009
   If DATASET is null, instead just return the current setting.
Packit 6c4009
   The special value LC_GLOBAL_LOCALE is the initial setting
Packit 6c4009
   for all threads, and means the thread uses the global
Packit 6c4009
   setting controlled by `setlocale'.  */
Packit 6c4009
locale_t
Packit 6c4009
__uselocale (locale_t newloc)
Packit 6c4009
{
Packit 6c4009
  locale_t oldloc = _NL_CURRENT_LOCALE;
Packit 6c4009
Packit 6c4009
  if (newloc != NULL)
Packit 6c4009
    {
Packit 6c4009
      const locale_t locobj
Packit 6c4009
	= newloc == LC_GLOBAL_LOCALE ? &_nl_global_locale : newloc;
Packit 6c4009
      __libc_tsd_set (locale_t, LOCALE, locobj);
Packit 6c4009
Packit 6c4009
#ifdef NL_CURRENT_INDIRECT
Packit 6c4009
      /* Now we must update all the per-category thread-local variables to
Packit 6c4009
	 point into the new current locale for this thread.  The magic
Packit 6c4009
	 symbols _nl_current_LC_FOO_used are defined to meaningless values
Packit 6c4009
	 if _nl_current_LC_FOO was linked in.  By using weak references to
Packit 6c4009
	 both symbols and testing the address of _nl_current_LC_FOO_used,
Packit 6c4009
	 we can avoid accessing the _nl_current_LC_FOO thread-local
Packit 6c4009
	 variable at all when no code referring to it was linked in.  We
Packit 6c4009
	 need the special bogus symbol because while TLS symbols can be
Packit 6c4009
	 weak, there is no reasonable way to test for the default-zero
Packit 6c4009
	 value as with a heap symbol (taking the address would just use
Packit 6c4009
	 some bogus offset from our thread pointer).  */
Packit 6c4009
Packit 6c4009
# define DEFINE_CATEGORY(category, category_name, items, a) \
Packit 6c4009
      {									      \
Packit 6c4009
	extern char _nl_current_##category##_used;			      \
Packit 6c4009
	weak_extern (_nl_current_##category##_used)			      \
Packit 6c4009
	weak_extern (_nl_current_##category)				      \
Packit 6c4009
	if (&_nl_current_##category##_used != 0)			      \
Packit 6c4009
	  _nl_current_##category = &locobj->__locales[category];	      \
Packit 6c4009
      }
Packit 6c4009
# include "categories.def"
Packit 6c4009
# undef	DEFINE_CATEGORY
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
      /* Update the special tsd cache of some locale data.  */
Packit 6c4009
      __libc_tsd_set (const uint16_t *, CTYPE_B, (void *) locobj->__ctype_b);
Packit 6c4009
      __libc_tsd_set (const int32_t *, CTYPE_TOLOWER,
Packit 6c4009
		      (void *) locobj->__ctype_tolower);
Packit 6c4009
      __libc_tsd_set (const int32_t *, CTYPE_TOUPPER,
Packit 6c4009
		      (void *) locobj->__ctype_toupper);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return oldloc == &_nl_global_locale ? LC_GLOBAL_LOCALE : oldloc;
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (__uselocale)
Packit 6c4009
weak_alias (__uselocale, uselocale)