Back to index

glibc  2.9
findlocale.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996-2001, 2002, 2003, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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 <assert.h>
00021 #include <locale.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 #ifdef _POSIX_MAPPED_FILES
00026 # include <sys/mman.h>
00027 #endif
00028 
00029 #include "localeinfo.h"
00030 #include "../iconv/gconv_charset.h"
00031 #include "../iconv/gconv_int.h"
00032 
00033 
00034 #ifdef NL_CURRENT_INDIRECT
00035 # define DEFINE_CATEGORY(category, category_name, items, a) \
00036 extern struct locale_data _nl_C_##category; \
00037 weak_extern (_nl_C_##category)
00038 # include "categories.def"
00039 # undef       DEFINE_CATEGORY
00040 
00041 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
00042    Elements are zero for categories whose data is never used.  */
00043 struct locale_data *const _nl_C[] attribute_hidden =
00044   {
00045 # define DEFINE_CATEGORY(category, category_name, items, a) \
00046     [category] = &_nl_C_##category,
00047 # include "categories.def"
00048 # undef       DEFINE_CATEGORY
00049   };
00050 #else
00051 # define _nl_C              (_nl_C_locobj.__locales)
00052 #endif
00053 
00054 
00055 /* For each category we keep a list of records for the locale files
00056    which are somehow addressed.  */
00057 struct loaded_l10nfile *_nl_locale_file_list[__LC_LAST];
00058 
00059 const char _nl_default_locale_path[] attribute_hidden = LOCALEDIR;
00060 
00061 
00062 struct locale_data *
00063 internal_function
00064 _nl_find_locale (const char *locale_path, size_t locale_path_len,
00065                int category, const char **name)
00066 {
00067   int mask;
00068   /* Name of the locale for this category.  */
00069   char *loc_name;
00070   const char *language;
00071   const char *modifier;
00072   const char *territory;
00073   const char *codeset;
00074   const char *normalized_codeset;
00075   struct loaded_l10nfile *locale_file;
00076 
00077   if ((*name)[0] == '\0')
00078     {
00079       /* The user decides which locale to use by setting environment
00080         variables.  */
00081       *name = getenv ("LC_ALL");
00082       if (*name == NULL || (*name)[0] == '\0')
00083        *name = getenv (_nl_category_names.str
00084                      + _nl_category_name_idxs[category]);
00085       if (*name == NULL || (*name)[0] == '\0')
00086        *name = getenv ("LANG");
00087     }
00088 
00089   if (*name == NULL || (*name)[0] == '\0'
00090       || (__builtin_expect (__libc_enable_secure, 0)
00091          && strchr (*name, '/') != NULL))
00092     *name = (char *) _nl_C_name;
00093 
00094   if (__builtin_expect (strcmp (*name, _nl_C_name), 1) == 0
00095       || __builtin_expect (strcmp (*name, _nl_POSIX_name), 1) == 0)
00096     {
00097       /* We need not load anything.  The needed data is contained in
00098         the library itself.  */
00099       *name = (char *) _nl_C_name;
00100       return _nl_C[category];
00101     }
00102 
00103   /* We really have to load some data.  First we try the archive,
00104      but only if there was no LOCPATH environment variable specified.  */
00105   if (__builtin_expect (locale_path == NULL, 1))
00106     {
00107       struct locale_data *data = _nl_load_locale_from_archive (category, name);
00108       if (__builtin_expect (data != NULL, 1))
00109        return data;
00110 
00111       /* Nothing in the archive.  Set the default path to search below.  */
00112       locale_path = _nl_default_locale_path;
00113       locale_path_len = sizeof _nl_default_locale_path;
00114     }
00115 
00116   /* We really have to load some data.  First see whether the name is
00117      an alias.  Please note that this makes it impossible to have "C"
00118      or "POSIX" as aliases.  */
00119   loc_name = (char *) _nl_expand_alias (*name);
00120   if (loc_name == NULL)
00121     /* It is no alias.  */
00122     loc_name = (char *) *name;
00123 
00124   /* Make a writable copy of the locale name.  */
00125   loc_name = strdupa (loc_name);
00126 
00127   /* LOCALE can consist of up to four recognized parts for the XPG syntax:
00128 
00129               language[_territory[.codeset]][@modifier]
00130 
00131      Beside the first all of them are allowed to be missing.  If the
00132      full specified locale is not found, the less specific one are
00133      looked for.  The various part will be stripped off according to
00134      the following order:
00135               (1) codeset
00136               (2) normalized codeset
00137               (3) territory
00138               (4) modifier
00139    */
00140   mask = _nl_explode_name (loc_name, &language, &modifier, &territory,
00141                         &codeset, &normalized_codeset);
00142 
00143   /* If exactly this locale was already asked for we have an entry with
00144      the complete name.  */
00145   locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
00146                                 locale_path, locale_path_len, mask,
00147                                 language, territory, codeset,
00148                                 normalized_codeset, modifier,
00149                                 _nl_category_names.str
00150                                 + _nl_category_name_idxs[category], 0);
00151 
00152   if (locale_file == NULL)
00153     {
00154       /* Find status record for addressed locale file.  We have to search
00155         through all directories in the locale path.  */
00156       locale_file = _nl_make_l10nflist (&_nl_locale_file_list[category],
00157                                    locale_path, locale_path_len, mask,
00158                                    language, territory, codeset,
00159                                    normalized_codeset, modifier,
00160                                    _nl_category_names.str
00161                                    + _nl_category_name_idxs[category], 1);
00162       if (locale_file == NULL)
00163        /* This means we are out of core.  */
00164        return NULL;
00165     }
00166 
00167   /* The space for normalized_codeset is dynamically allocated.  Free it.  */
00168   if (mask & XPG_NORM_CODESET)
00169     free ((void *) normalized_codeset);
00170 
00171   if (locale_file->decided == 0)
00172     _nl_load_locale (locale_file, category);
00173 
00174   if (locale_file->data == NULL)
00175     {
00176       int cnt;
00177       for (cnt = 0; locale_file->successor[cnt] != NULL; ++cnt)
00178        {
00179          if (locale_file->successor[cnt]->decided == 0)
00180            _nl_load_locale (locale_file->successor[cnt], category);
00181          if (locale_file->successor[cnt]->data != NULL)
00182            break;
00183        }
00184       /* Move the entry we found (or NULL) to the first place of
00185         successors.  */
00186       locale_file->successor[0] = locale_file->successor[cnt];
00187       locale_file = locale_file->successor[cnt];
00188 
00189       if (locale_file == NULL)
00190        return NULL;
00191     }
00192 
00193   /* The LC_CTYPE category allows to check whether a locale is really
00194      usable.  If the locale name contains a charset name and the
00195      charset name used in the locale (present in the LC_CTYPE data) is
00196      not the same (after resolving aliases etc) we reject the locale
00197      since using it would irritate users expecting the charset named
00198      in the locale name.  */
00199   if (codeset != NULL)
00200     {
00201       /* Get the codeset information from the locale file.  */
00202       static const int codeset_idx[] =
00203        {
00204          [__LC_CTYPE] = _NL_ITEM_INDEX (CODESET),
00205          [__LC_NUMERIC] = _NL_ITEM_INDEX (_NL_NUMERIC_CODESET),
00206          [__LC_TIME] = _NL_ITEM_INDEX (_NL_TIME_CODESET),
00207          [__LC_COLLATE] = _NL_ITEM_INDEX (_NL_COLLATE_CODESET),
00208          [__LC_MONETARY] = _NL_ITEM_INDEX (_NL_MONETARY_CODESET),
00209          [__LC_MESSAGES] = _NL_ITEM_INDEX (_NL_MESSAGES_CODESET),
00210          [__LC_PAPER] = _NL_ITEM_INDEX (_NL_PAPER_CODESET),
00211          [__LC_NAME] = _NL_ITEM_INDEX (_NL_NAME_CODESET),
00212          [__LC_ADDRESS] = _NL_ITEM_INDEX (_NL_ADDRESS_CODESET),
00213          [__LC_TELEPHONE] = _NL_ITEM_INDEX (_NL_TELEPHONE_CODESET),
00214          [__LC_MEASUREMENT] = _NL_ITEM_INDEX (_NL_MEASUREMENT_CODESET),
00215          [__LC_IDENTIFICATION] = _NL_ITEM_INDEX (_NL_IDENTIFICATION_CODESET)
00216        };
00217       const struct locale_data *data;
00218       const char *locale_codeset;
00219       char *clocale_codeset;
00220       char *ccodeset;
00221 
00222       data = (const struct locale_data *) locale_file->data;
00223       locale_codeset =
00224        (const char *) data->values[codeset_idx[category]].string;
00225       assert (locale_codeset != NULL);
00226       /* Note the length of the allocated memory: +3 for up to two slashes
00227         and the NUL byte.  */
00228       clocale_codeset = (char *) alloca (strlen (locale_codeset) + 3);
00229       strip (clocale_codeset, locale_codeset);
00230 
00231       ccodeset = (char *) alloca (strlen (codeset) + 3);
00232       strip (ccodeset, codeset);
00233 
00234       if (__gconv_compare_alias (upstr (ccodeset, ccodeset),
00235                              upstr (clocale_codeset,
00236                                    clocale_codeset)) != 0)
00237        /* The codesets are not identical, don't use the locale.  */
00238        return NULL;
00239     }
00240 
00241   /* Determine the locale name for which loading succeeded.  This
00242      information comes from the file name.  The form is
00243      <path>/<locale>/LC_foo.  We must extract the <locale> part.  */
00244   if (((const struct locale_data *) locale_file->data)->name == NULL)
00245     {
00246       char *cp, *endp;
00247 
00248       endp = strrchr (locale_file->filename, '/');
00249       cp = endp - 1;
00250       while (cp[-1] != '/')
00251        --cp;
00252       ((struct locale_data *) locale_file->data)->name = __strndup (cp,
00253                                                             endp - cp);
00254     }
00255 
00256   /* Determine whether the user wants transliteration or not.  */
00257   if (modifier != NULL && __strcasecmp (modifier, "TRANSLIT") == 0)
00258     ((struct locale_data *) locale_file->data)->use_translit = 1;
00259 
00260   /* Increment the usage count.  */
00261   if (((const struct locale_data *) locale_file->data)->usage_count
00262       < MAX_USAGE_COUNT)
00263     ++((struct locale_data *) locale_file->data)->usage_count;
00264 
00265   return (struct locale_data *) locale_file->data;
00266 }
00267 
00268 
00269 /* Calling this function assumes the lock for handling global locale data
00270    is acquired.  */
00271 void
00272 internal_function
00273 _nl_remove_locale (int locale, struct locale_data *data)
00274 {
00275   if (--data->usage_count == 0)
00276     {
00277       if (data->alloc != ld_archive)
00278        {
00279          /* First search the entry in the list of loaded files.  */
00280          struct loaded_l10nfile *ptr = _nl_locale_file_list[locale];
00281 
00282          /* Search for the entry.  It must be in the list.  Otherwise it
00283             is a bug and we crash badly.  */
00284          while ((struct locale_data *) ptr->data != data)
00285            ptr = ptr->next;
00286 
00287          /* Mark the data as not available anymore.  So when the data has
00288             to be used again it is reloaded.  */
00289          ptr->decided = 0;
00290          ptr->data = NULL;
00291        }
00292 
00293       /* This does the real work.  */
00294       _nl_unload_locale (data);
00295     }
00296 }