xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame^] | 1 | /* Return a reference to locale information record. |
| 2 | Copyright (C) 1996-2016 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <argz.h> |
| 21 | #include <libc-lock.h> |
| 22 | #include <errno.h> |
| 23 | #include <locale.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include "localeinfo.h" |
| 28 | |
| 29 | |
| 30 | /* Lock for protecting global data. */ |
| 31 | __libc_rwlock_define (extern , __libc_setlocale_lock attribute_hidden) |
| 32 | |
| 33 | |
| 34 | /* Use this when we come along an error. */ |
| 35 | #define ERROR_RETURN \ |
| 36 | do { \ |
| 37 | __set_errno (EINVAL); \ |
| 38 | return NULL; \ |
| 39 | } while (0) |
| 40 | |
| 41 | |
| 42 | __locale_t |
| 43 | __newlocale (int category_mask, const char *locale, __locale_t base) |
| 44 | { |
| 45 | /* Intermediate memory for result. */ |
| 46 | const char *newnames[__LC_LAST]; |
| 47 | struct __locale_struct result; |
| 48 | __locale_t result_ptr; |
| 49 | char *locale_path; |
| 50 | size_t locale_path_len; |
| 51 | const char *locpath_var; |
| 52 | int cnt; |
| 53 | size_t names_len; |
| 54 | |
| 55 | /* We treat LC_ALL in the same way as if all bits were set. */ |
| 56 | if (category_mask == 1 << LC_ALL) |
| 57 | category_mask = (1 << __LC_LAST) - 1 - (1 << LC_ALL); |
| 58 | |
| 59 | /* Sanity check for CATEGORY argument. */ |
| 60 | if ((category_mask & ~((1 << __LC_LAST) - 1 - (1 << LC_ALL))) != 0) |
| 61 | ERROR_RETURN; |
| 62 | |
| 63 | /* `newlocale' does not support asking for the locale name. */ |
| 64 | if (locale == NULL) |
| 65 | ERROR_RETURN; |
| 66 | |
| 67 | if (base == _nl_C_locobj_ptr) |
| 68 | /* We're to modify BASE, returned for a previous call with "C". |
| 69 | We can't really modify the read-only structure, so instead |
| 70 | start over by copying it. */ |
| 71 | base = NULL; |
| 72 | |
| 73 | if ((base == NULL || category_mask == (1 << __LC_LAST) - 1 - (1 << LC_ALL)) |
| 74 | && (category_mask == 0 || !strcmp (locale, "C"))) |
| 75 | /* Asking for the "C" locale needn't allocate a new object. */ |
| 76 | return _nl_C_locobj_ptr; |
| 77 | |
| 78 | /* Allocate memory for the result. */ |
| 79 | if (base != NULL) |
| 80 | result = *base; |
| 81 | else |
| 82 | /* Fill with pointers to C locale data. */ |
| 83 | result = _nl_C_locobj; |
| 84 | |
| 85 | /* If no category is to be set we return BASE if available or a |
| 86 | dataset using the C locale data. */ |
| 87 | if (category_mask == 0) |
| 88 | { |
| 89 | result_ptr = (__locale_t) malloc (sizeof (struct __locale_struct)); |
| 90 | if (result_ptr == NULL) |
| 91 | return NULL; |
| 92 | *result_ptr = result; |
| 93 | |
| 94 | goto update; |
| 95 | } |
| 96 | |
| 97 | /* We perhaps really have to load some data. So we determine the |
| 98 | path in which to look for the data now. The environment variable |
| 99 | `LOCPATH' must only be used when the binary has no SUID or SGID |
| 100 | bit set. If using the default path, we tell _nl_find_locale |
| 101 | by passing null and it can check the canonical locale archive. */ |
| 102 | locale_path = NULL; |
| 103 | locale_path_len = 0; |
| 104 | |
| 105 | locpath_var = getenv ("LOCPATH"); |
| 106 | if (locpath_var != NULL && locpath_var[0] != '\0') |
| 107 | { |
| 108 | if (__argz_create_sep (locpath_var, ':', |
| 109 | &locale_path, &locale_path_len) != 0) |
| 110 | return NULL; |
| 111 | |
| 112 | if (__argz_add_sep (&locale_path, &locale_path_len, |
| 113 | _nl_default_locale_path, ':') != 0) |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | /* Get the names for the locales we are interested in. We either |
| 118 | allow a composite name or a single name. */ |
| 119 | for (cnt = 0; cnt < __LC_LAST; ++cnt) |
| 120 | if (cnt != LC_ALL) |
| 121 | newnames[cnt] = locale; |
| 122 | if (strchr (locale, ';') != NULL) |
| 123 | { |
| 124 | /* This is a composite name. Make a copy and split it up. */ |
| 125 | char *np = strdupa (locale); |
| 126 | char *cp; |
| 127 | int specified_mask = 0; |
| 128 | |
| 129 | while ((cp = strchr (np, '=')) != NULL) |
| 130 | { |
| 131 | for (cnt = 0; cnt < __LC_LAST; ++cnt) |
| 132 | if (cnt != LC_ALL |
| 133 | && (size_t) (cp - np) == _nl_category_name_sizes[cnt] |
| 134 | && memcmp (np, (_nl_category_names.str |
| 135 | + _nl_category_name_idxs[cnt]), cp - np) == 0) |
| 136 | break; |
| 137 | |
| 138 | if (cnt == __LC_LAST) |
| 139 | /* Bogus category name. */ |
| 140 | ERROR_RETURN; |
| 141 | |
| 142 | /* Found the category this clause sets. */ |
| 143 | specified_mask |= 1 << cnt; |
| 144 | newnames[cnt] = ++cp; |
| 145 | cp = strchr (cp, ';'); |
| 146 | if (cp != NULL) |
| 147 | { |
| 148 | /* Examine the next clause. */ |
| 149 | *cp = '\0'; |
| 150 | np = cp + 1; |
| 151 | } |
| 152 | else |
| 153 | /* This was the last clause. We are done. */ |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | if (category_mask &~ specified_mask) |
| 158 | /* The composite name did not specify all categories we need. */ |
| 159 | ERROR_RETURN; |
| 160 | } |
| 161 | |
| 162 | /* Protect global data. */ |
| 163 | __libc_rwlock_wrlock (__libc_setlocale_lock); |
| 164 | |
| 165 | /* Now process all categories we are interested in. */ |
| 166 | names_len = 0; |
| 167 | for (cnt = 0; cnt < __LC_LAST; ++cnt) |
| 168 | { |
| 169 | if ((category_mask & 1 << cnt) != 0) |
| 170 | { |
| 171 | result.__locales[cnt] = _nl_find_locale (locale_path, |
| 172 | locale_path_len, |
| 173 | cnt, &newnames[cnt]); |
| 174 | if (result.__locales[cnt] == NULL) |
| 175 | { |
| 176 | free_cnt_data_and_exit: |
| 177 | while (cnt-- > 0) |
| 178 | if (((category_mask & 1 << cnt) != 0) |
| 179 | && result.__locales[cnt]->usage_count != UNDELETABLE) |
| 180 | /* We can remove the data. */ |
| 181 | _nl_remove_locale (cnt, result.__locales[cnt]); |
| 182 | |
| 183 | /* Critical section left. */ |
| 184 | __libc_rwlock_unlock (__libc_setlocale_lock); |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | if (newnames[cnt] != _nl_C_name) |
| 189 | names_len += strlen (newnames[cnt]) + 1; |
| 190 | } |
| 191 | else if (cnt != LC_ALL && result.__names[cnt] != _nl_C_name) |
| 192 | /* Tally up the unchanged names from BASE as well. */ |
| 193 | names_len += strlen (result.__names[cnt]) + 1; |
| 194 | } |
| 195 | |
| 196 | /* We successfully loaded all required data. Allocate a new structure. |
| 197 | We can't just reuse the BASE pointer, because the name strings are |
| 198 | changing and we need the old name string area intact so we can copy |
| 199 | out of it into the new one without overlap problems should some |
| 200 | category's name be getting longer. */ |
| 201 | result_ptr = malloc (sizeof (struct __locale_struct) + names_len); |
| 202 | if (result_ptr == NULL) |
| 203 | { |
| 204 | cnt = __LC_LAST; |
| 205 | goto free_cnt_data_and_exit; |
| 206 | } |
| 207 | |
| 208 | if (base == NULL) |
| 209 | { |
| 210 | /* Fill in this new structure from scratch. */ |
| 211 | |
| 212 | char *namep = (char *) (result_ptr + 1); |
| 213 | |
| 214 | /* Install copied new names in the new structure's __names array. |
| 215 | If resolved to "C", that is already in RESULT.__names to start. */ |
| 216 | for (cnt = 0; cnt < __LC_LAST; ++cnt) |
| 217 | if ((category_mask & 1 << cnt) != 0 && newnames[cnt] != _nl_C_name) |
| 218 | { |
| 219 | result.__names[cnt] = namep; |
| 220 | namep = __stpcpy (namep, newnames[cnt]) + 1; |
| 221 | } |
| 222 | |
| 223 | *result_ptr = result; |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | /* We modify the base structure. */ |
| 228 | |
| 229 | char *namep = (char *) (result_ptr + 1); |
| 230 | |
| 231 | for (cnt = 0; cnt < __LC_LAST; ++cnt) |
| 232 | if ((category_mask & 1 << cnt) != 0) |
| 233 | { |
| 234 | if (base->__locales[cnt]->usage_count != UNDELETABLE) |
| 235 | /* We can remove the old data. */ |
| 236 | _nl_remove_locale (cnt, base->__locales[cnt]); |
| 237 | result_ptr->__locales[cnt] = result.__locales[cnt]; |
| 238 | |
| 239 | if (newnames[cnt] == _nl_C_name) |
| 240 | result_ptr->__names[cnt] = _nl_C_name; |
| 241 | else |
| 242 | { |
| 243 | result_ptr->__names[cnt] = namep; |
| 244 | namep = __stpcpy (namep, newnames[cnt]) + 1; |
| 245 | } |
| 246 | } |
| 247 | else if (cnt != LC_ALL) |
| 248 | { |
| 249 | /* The RESULT members point into the old BASE structure. */ |
| 250 | result_ptr->__locales[cnt] = result.__locales[cnt]; |
| 251 | if (result.__names[cnt] == _nl_C_name) |
| 252 | result_ptr->__names[cnt] = _nl_C_name; |
| 253 | else |
| 254 | { |
| 255 | result_ptr->__names[cnt] = namep; |
| 256 | namep = __stpcpy (namep, result.__names[cnt]) + 1; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | free (base); |
| 261 | } |
| 262 | |
| 263 | /* Critical section left. */ |
| 264 | __libc_rwlock_unlock (__libc_setlocale_lock); |
| 265 | |
| 266 | /* Update the special members. */ |
| 267 | update: |
| 268 | { |
| 269 | union locale_data_value *ctypes = result_ptr->__locales[LC_CTYPE]->values; |
| 270 | result_ptr->__ctype_b = (const unsigned short int *) |
| 271 | ctypes[_NL_ITEM_INDEX (_NL_CTYPE_CLASS)].string + 128; |
| 272 | result_ptr->__ctype_tolower = (const int *) |
| 273 | ctypes[_NL_ITEM_INDEX (_NL_CTYPE_TOLOWER)].string + 128; |
| 274 | result_ptr->__ctype_toupper = (const int *) |
| 275 | ctypes[_NL_ITEM_INDEX (_NL_CTYPE_TOUPPER)].string + 128; |
| 276 | } |
| 277 | |
| 278 | return result_ptr; |
| 279 | } |
| 280 | weak_alias (__newlocale, newlocale) |