Back to index

glibc  2.9
setlocale.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1992, 1995-2000, 2002, 2003, 2004, 2006, 2008
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <alloca.h>
00021 #include <argz.h>
00022 #include <errno.h>
00023 #include <bits/libc-lock.h>
00024 #include <locale.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <unistd.h>
00028 
00029 #include "localeinfo.h"
00030 
00031 #ifdef NL_CURRENT_INDIRECT
00032 
00033 /* For each category declare a special external symbol
00034    _nl_current_CATEGORY_used with a weak reference.
00035    This symbol will is defined in lc-CATEGORY.c and will be linked in
00036    if anything uses _nl_current_CATEGORY (also defined in that module).
00037    Also use a weak reference for the _nl_current_CATEGORY thread variable.  */
00038 
00039 # define DEFINE_CATEGORY(category, category_name, items, a) \
00040     extern char _nl_current_##category##_used; \
00041     weak_extern (_nl_current_##category##_used) \
00042     weak_extern (_nl_current_##category)
00043 # include "categories.def"
00044 # undef       DEFINE_CATEGORY
00045 
00046 /* Now define a table of flags based on those special weak symbols' values.
00047    _nl_current_used[CATEGORY] will be zero if _nl_current_CATEGORY is not
00048    linked in.  */
00049 static char *const _nl_current_used[] =
00050   {
00051 # define DEFINE_CATEGORY(category, category_name, items, a) \
00052     [category] = &_nl_current_##category##_used,
00053 # include "categories.def"
00054 # undef       DEFINE_CATEGORY
00055   };
00056 
00057 # define CATEGORY_USED(category)   (_nl_current_used[category] != 0)
00058 
00059 #else
00060 
00061 /* The shared library always loads all the categories,
00062    and the current global settings are kept in _nl_global_locale.  */
00063 
00064 # define CATEGORY_USED(category)   (1)
00065 
00066 #endif
00067 
00068 
00069 /* Define an array of category names (also the environment variable names).  */
00070 const union catnamestr_t _nl_category_names attribute_hidden =
00071   {
00072     {
00073 #define DEFINE_CATEGORY(category, category_name, items, a) \
00074       category_name,
00075 #include "categories.def"
00076 #undef DEFINE_CATEGORY
00077     }
00078   };
00079 
00080 const uint8_t _nl_category_name_idxs[__LC_LAST] attribute_hidden =
00081   {
00082 #define DEFINE_CATEGORY(category, category_name, items, a) \
00083     [category] = offsetof (union catnamestr_t, CATNAMEMF (__LINE__)),
00084 #include "categories.def"
00085 #undef DEFINE_CATEGORY
00086   };
00087 
00088 /* An array of their lengths, for convenience.  */
00089 const uint8_t _nl_category_name_sizes[] attribute_hidden =
00090   {
00091 #define DEFINE_CATEGORY(category, category_name, items, a) \
00092     [category] = sizeof (category_name) - 1,
00093 #include "categories.def"
00094 #undef DEFINE_CATEGORY
00095     [LC_ALL] = sizeof ("LC_ALL") - 1
00096   };
00097 
00098 
00099 #ifdef NL_CURRENT_INDIRECT
00100 # define WEAK_POSTLOAD(postload) weak_extern (postload)
00101 #else
00102 # define WEAK_POSTLOAD(postload) /* Need strong refs in static linking.  */
00103 #endif
00104 
00105 /* Declare the postload functions used below.  */
00106 #undef NO_POSTLOAD
00107 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist.  */
00108 #define DEFINE_CATEGORY(category, category_name, items, postload) \
00109 extern void postload (void); WEAK_POSTLOAD (postload)
00110 #include "categories.def"
00111 #undef DEFINE_CATEGORY
00112 #undef NO_POSTLOAD
00113 
00114 /* Define an array indexed by category of postload functions to call after
00115    loading and installing that category's data.  */
00116 static void (*const _nl_category_postload[]) (void) =
00117   {
00118 #define DEFINE_CATEGORY(category, category_name, items, postload) \
00119     [category] = postload,
00120 #include "categories.def"
00121 #undef DEFINE_CATEGORY
00122   };
00123 
00124 
00125 /* Lock for protecting global data.  */
00126 __libc_rwlock_define_initialized (, __libc_setlocale_lock attribute_hidden)
00127 
00128 /* Defined in loadmsgcat.c.  */
00129 extern int _nl_msg_cat_cntr;
00130 
00131 
00132 /* Use this when we come along an error.  */
00133 #define ERROR_RETURN                                                 \
00134   do {                                                               \
00135     __set_errno (EINVAL);                                            \
00136     return NULL;                                                     \
00137   } while (0)
00138 
00139 
00140 /* Construct a new composite name.  */
00141 static char *
00142 new_composite_name (int category, const char *newnames[__LC_LAST])
00143 {
00144   size_t last_len = 0;
00145   size_t cumlen = 0;
00146   int i;
00147   char *new, *p;
00148   int same = 1;
00149 
00150   for (i = 0; i < __LC_LAST; ++i)
00151     if (i != LC_ALL)
00152       {
00153        const char *name = (category == LC_ALL ? newnames[i] :
00154                          category == i ? newnames[0] :
00155                          _nl_global_locale.__names[i]);
00156        last_len = strlen (name);
00157        cumlen += _nl_category_name_sizes[i] + 1 + last_len + 1;
00158        if (i > 0 && same && strcmp (name, newnames[0]) != 0)
00159          same = 0;
00160       }
00161 
00162   if (same)
00163     {
00164       /* All the categories use the same name.  */
00165       if (strcmp (newnames[0], _nl_C_name) == 0
00166          || strcmp (newnames[0], _nl_POSIX_name) == 0)
00167        return (char *) _nl_C_name;
00168 
00169       new = malloc (last_len + 1);
00170 
00171       return new == NULL ? NULL : memcpy (new, newnames[0], last_len + 1);
00172     }
00173 
00174   new = malloc (cumlen);
00175   if (new == NULL)
00176     return NULL;
00177   p = new;
00178   for (i = 0; i < __LC_LAST; ++i)
00179     if (i != LC_ALL)
00180       {
00181        /* Add "CATEGORY=NAME;" to the string.  */
00182        const char *name = (category == LC_ALL ? newnames[i] :
00183                          category == i ? newnames[0] :
00184                          _nl_global_locale.__names[i]);
00185        p = __stpcpy (p, _nl_category_names.str + _nl_category_name_idxs[i]);
00186        *p++ = '=';
00187        p = __stpcpy (p, name);
00188        *p++ = ';';
00189       }
00190   p[-1] = '\0';             /* Clobber the last ';'.  */
00191   return new;
00192 }
00193 
00194 
00195 /* Put NAME in _nl_global_locale.__names.  */
00196 static void
00197 setname (int category, const char *name)
00198 {
00199   if (_nl_global_locale.__names[category] == name)
00200     return;
00201 
00202   if (_nl_global_locale.__names[category] != _nl_C_name)
00203     free ((char *) _nl_global_locale.__names[category]);
00204 
00205   _nl_global_locale.__names[category] = name;
00206 }
00207 
00208 /* Put DATA in *_nl_current[CATEGORY].  */
00209 static inline void
00210 setdata (int category, struct locale_data *data)
00211 {
00212   if (CATEGORY_USED (category))
00213     {
00214       _nl_global_locale.__locales[category] = data;
00215       if (_nl_category_postload[category])
00216        (*_nl_category_postload[category]) ();
00217     }
00218 }
00219 
00220 char *
00221 setlocale (int category, const char *locale)
00222 {
00223   char *locale_path;
00224   size_t locale_path_len;
00225   const char *locpath_var;
00226   char *composite;
00227 
00228   /* Sanity check for CATEGORY argument.  */
00229   if (__builtin_expect (category, 0) < 0
00230       || __builtin_expect (category, 0) >= __LC_LAST)
00231     ERROR_RETURN;
00232 
00233   /* Does user want name of current locale?  */
00234   if (locale == NULL)
00235     return (char *) _nl_global_locale.__names[category];
00236 
00237   /* Protect global data.  */
00238   __libc_rwlock_wrlock (__libc_setlocale_lock);
00239 
00240   if (strcmp (locale, _nl_global_locale.__names[category]) == 0)
00241     {
00242       /* Changing to the same thing.  */
00243       __libc_rwlock_unlock (__libc_setlocale_lock);
00244 
00245       return (char *) _nl_global_locale.__names[category];
00246     }
00247 
00248   /* We perhaps really have to load some data.  So we determine the
00249      path in which to look for the data now.  The environment variable
00250      `LOCPATH' must only be used when the binary has no SUID or SGID
00251      bit set.  If using the default path, we tell _nl_find_locale
00252      by passing null and it can check the canonical locale archive.  */
00253   locale_path = NULL;
00254   locale_path_len = 0;
00255 
00256   locpath_var = getenv ("LOCPATH");
00257   if (locpath_var != NULL && locpath_var[0] != '\0')
00258     {
00259       if (__argz_create_sep (locpath_var, ':',
00260                           &locale_path, &locale_path_len) != 0
00261          || __argz_add_sep (&locale_path, &locale_path_len,
00262                           _nl_default_locale_path, ':') != 0)
00263        {
00264          __libc_rwlock_unlock (__libc_setlocale_lock);
00265          return NULL;
00266        }
00267     }
00268 
00269   if (category == LC_ALL)
00270     {
00271       /* The user wants to set all categories.  The desired locales
00272         for the individual categories can be selected by using a
00273         composite locale name.  This is a semi-colon separated list
00274         of entries of the form `CATEGORY=VALUE'.  */
00275       const char *newnames[__LC_LAST];
00276       struct locale_data *newdata[__LC_LAST];
00277 
00278       /* Set all name pointers to the argument name.  */
00279       for (category = 0; category < __LC_LAST; ++category)
00280        if (category != LC_ALL)
00281          newnames[category] = (char *) locale;
00282 
00283       if (__builtin_expect (strchr (locale, ';') != NULL, 0))
00284        {
00285          /* This is a composite name.  Make a copy and split it up.  */
00286          char *np = strdupa (locale);
00287          char *cp;
00288          int cnt;
00289 
00290          while ((cp = strchr (np, '=')) != NULL)
00291            {
00292              for (cnt = 0; cnt < __LC_LAST; ++cnt)
00293               if (cnt != LC_ALL
00294                   && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
00295                   && (memcmp (np, (_nl_category_names.str
00296                                  + _nl_category_name_idxs[cnt]), cp - np)
00297                      == 0))
00298                 break;
00299 
00300              if (cnt == __LC_LAST)
00301               {
00302               error_return:
00303                 __libc_rwlock_unlock (__libc_setlocale_lock);
00304 
00305                 /* Bogus category name.  */
00306                 ERROR_RETURN;
00307               }
00308 
00309              /* Found the category this clause sets.  */
00310              newnames[cnt] = ++cp;
00311              cp = strchr (cp, ';');
00312              if (cp != NULL)
00313               {
00314                 /* Examine the next clause.  */
00315                 *cp = '\0';
00316                 np = cp + 1;
00317               }
00318              else
00319               /* This was the last clause.  We are done.  */
00320               break;
00321            }
00322 
00323          for (cnt = 0; cnt < __LC_LAST; ++cnt)
00324            if (cnt != LC_ALL && newnames[cnt] == locale)
00325              /* The composite name did not specify all categories.  */
00326              goto error_return;
00327        }
00328 
00329       /* Load the new data for each category.  */
00330       while (category-- > 0)
00331        if (category != LC_ALL)
00332          {
00333            newdata[category] = _nl_find_locale (locale_path, locale_path_len,
00334                                            category,
00335                                            &newnames[category]);
00336 
00337            if (newdata[category] == NULL)
00338              {
00339 #ifdef NL_CURRENT_INDIRECT
00340               if (newnames[category] == _nl_C_name)
00341                 /* Null because it's the weak value of _nl_C_LC_FOO.  */
00342                 continue;
00343 #endif
00344               break;
00345              }
00346 
00347            /* We must not simply free a global locale since we have
00348               no control over the usage.  So we mark it as
00349               un-deletable.  And yes, the 'if' is needed, the data
00350               might be in read-only memory.  */
00351            if (newdata[category]->usage_count != UNDELETABLE)
00352              newdata[category]->usage_count = UNDELETABLE;
00353 
00354            /* Make a copy of locale name.  */
00355            if (newnames[category] != _nl_C_name)
00356              {
00357               if (strcmp (newnames[category],
00358                          _nl_global_locale.__names[category]) == 0)
00359                 newnames[category] = _nl_global_locale.__names[category];
00360               else
00361                 {
00362                   newnames[category] = __strdup (newnames[category]);
00363                   if (newnames[category] == NULL)
00364                     break;
00365                 }
00366              }
00367          }
00368 
00369       /* Create new composite name.  */
00370       composite = (category >= 0
00371                  ? NULL : new_composite_name (LC_ALL, newnames));
00372       if (composite != NULL)
00373        {
00374          /* Now we have loaded all the new data.  Put it in place.  */
00375          for (category = 0; category < __LC_LAST; ++category)
00376            if (category != LC_ALL)
00377              {
00378               setdata (category, newdata[category]);
00379               setname (category, newnames[category]);
00380              }
00381          setname (LC_ALL, composite);
00382 
00383          /* We successfully loaded a new locale.  Let the message catalog
00384             functions know about this.  */
00385          ++_nl_msg_cat_cntr;
00386        }
00387       else
00388        for (++category; category < __LC_LAST; ++category)
00389          if (category != LC_ALL && newnames[category] != _nl_C_name
00390              && newnames[category] != _nl_global_locale.__names[category])
00391            free ((char *) newnames[category]);
00392 
00393       /* Critical section left.  */
00394       __libc_rwlock_unlock (__libc_setlocale_lock);
00395 
00396       /* Free the resources (the locale path variable).  */
00397       free (locale_path);
00398 
00399       return composite;
00400     }
00401   else
00402     {
00403       struct locale_data *newdata = NULL;
00404       const char *newname[1] = { locale };
00405 
00406       if (CATEGORY_USED (category))
00407        {
00408          /* Only actually load the data if anything will use it.  */
00409          newdata = _nl_find_locale (locale_path, locale_path_len, category,
00410                                  &newname[0]);
00411          if (newdata == NULL)
00412            goto abort_single;
00413 
00414          /* We must not simply free a global locale since we have no
00415             control over the usage.  So we mark it as un-deletable.
00416 
00417             Note: do not remove the `if', it's necessary to copy with
00418             the builtin locale data.  */
00419          if (newdata->usage_count != UNDELETABLE)
00420            newdata->usage_count = UNDELETABLE;
00421        }
00422 
00423       /* Make a copy of locale name.  */
00424       if (newname[0] != _nl_C_name)
00425        {
00426          newname[0] = __strdup (newname[0]);
00427          if (newname[0] == NULL)
00428            goto abort_single;
00429        }
00430 
00431       /* Create new composite name.  */
00432       composite = new_composite_name (category, newname);
00433       if (composite == NULL)
00434        {
00435          if (newname[0] != _nl_C_name)
00436            free ((char *) newname[0]);
00437 
00438          /* Say that we don't have any data loaded.  */
00439        abort_single:
00440          newname[0] = NULL;
00441        }
00442       else
00443        {
00444          if (CATEGORY_USED (category))
00445            setdata (category, newdata);
00446 
00447          setname (category, newname[0]);
00448          setname (LC_ALL, composite);
00449 
00450          /* We successfully loaded a new locale.  Let the message catalog
00451             functions know about this.  */
00452          ++_nl_msg_cat_cntr;
00453        }
00454 
00455       /* Critical section left.  */
00456       __libc_rwlock_unlock (__libc_setlocale_lock);
00457 
00458       /* Free the resources (the locale path variable.  */
00459       free (locale_path);
00460 
00461       return (char *) newname[0];
00462     }
00463 }
00464 libc_hidden_def (setlocale)
00465 
00466 static void __libc_freeres_fn_section
00467 free_category (int category,
00468               struct locale_data *here, struct locale_data *c_data)
00469 {
00470   struct loaded_l10nfile *runp = _nl_locale_file_list[category];
00471 
00472   /* If this category is already "C" don't do anything.  */
00473   if (here != c_data)
00474     {
00475       /* We have to be prepared that sometime later we still
00476         might need the locale information.  */
00477       setdata (category, c_data);
00478       setname (category, _nl_C_name);
00479     }
00480 
00481   while (runp != NULL)
00482     {
00483       struct loaded_l10nfile *curr = runp;
00484       struct locale_data *data = (struct locale_data *) runp->data;
00485 
00486       if (data != NULL && data != c_data)
00487        _nl_unload_locale (data);
00488       runp = runp->next;
00489       free ((char *) curr->filename);
00490       free (curr);
00491     }
00492 }
00493 
00494 /* This is called from iconv/gconv_db.c's free_mem, as locales must
00495    be freed before freeing gconv steps arrays.  */
00496 void __libc_freeres_fn_section
00497 _nl_locale_subfreeres (void)
00498 {
00499 #ifdef NL_CURRENT_INDIRECT
00500   /* We don't use the loop because we want to have individual weak
00501      symbol references here.  */
00502 # define DEFINE_CATEGORY(category, category_name, items, a)                 \
00503   if (CATEGORY_USED (category))                                             \
00504     {                                                                \
00505       extern struct locale_data _nl_C_##category;                           \
00506       weak_extern (_nl_C_##category)                                        \
00507       free_category (category, *_nl_current_##category, &_nl_C_##category);   \
00508     }
00509 # include "categories.def"
00510 # undef       DEFINE_CATEGORY
00511 #else
00512   int category;
00513 
00514   for (category = 0; category < __LC_LAST; ++category)
00515     if (category != LC_ALL)
00516       free_category (category, _NL_CURRENT_DATA (category),
00517                    _nl_C_locobj.__locales[category]);
00518 #endif
00519 
00520   setname (LC_ALL, _nl_C_name);
00521 
00522   /* This frees the data structures associated with the locale archive.
00523      The locales from the archive are not in the file list, so we have
00524      not called _nl_unload_locale on them above.  */
00525   _nl_archive_subfreeres ();
00526 }