SysV shared memory definitions for Linux/PA.
[glibc.git] / locale / newlocale.c
blob3b8676ceebd4ab8dabf7f6336386ab6ed8ffab86
1 /* Return a reference to locale information record.
2 Copyright (C) 1996,1997,1999,2000,2001,2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <argz.h>
22 #include <errno.h>
23 #include <locale.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #include "localeinfo.h"
30 /* Use this when we come along an error. */
31 #define ERROR_RETURN \
32 do { \
33 __set_errno (EINVAL); \
34 return NULL; \
35 } while (0)
38 __locale_t
39 __newlocale (int category_mask, const char *locale, __locale_t base)
41 /* Intermediate memory for result. */
42 const char *newnames[__LC_LAST];
43 struct __locale_struct result;
44 __locale_t result_ptr;
45 char *locale_path;
46 size_t locale_path_len;
47 const char *locpath_var;
48 int cnt;
49 size_t names_len;
51 /* We treat LC_ALL in the same way as if all bits were set. */
52 if (category_mask == 1 << LC_ALL)
53 category_mask = (1 << __LC_LAST) - 1 - (1 << LC_ALL);
55 /* Sanity check for CATEGORY argument. */
56 if ((category_mask & ~((1 << __LC_LAST) - 1 - (1 << LC_ALL))) != 0)
57 ERROR_RETURN;
59 /* `newlocale' does not support asking for the locale name. */
60 if (locale == NULL)
61 ERROR_RETURN;
63 /* Allocate memory for the result. */
64 if (base != NULL)
65 result = *base;
66 else
67 /* Fill with pointers to C locale data. */
68 result = _nl_C_locobj;
70 /* If no category is to be set we return BASE if available or a
71 dataset using the C locale data. */
72 if (category_mask == 0)
74 result_ptr = (__locale_t) malloc (sizeof (struct __locale_struct));
75 if (result_ptr == NULL)
76 return NULL;
77 *result_ptr = result;
79 goto update;
82 /* We perhaps really have to load some data. So we determine the
83 path in which to look for the data now. The environment variable
84 `LOCPATH' must only be used when the binary has no SUID or SGID
85 bit set. If using the default path, we tell _nl_find_locale
86 by passing null and it can check the canonical locale archive. */
87 locale_path = NULL;
88 locale_path_len = 0;
90 locpath_var = getenv ("LOCPATH");
91 if (locpath_var != NULL && locpath_var[0] != '\0')
93 if (__argz_create_sep (locpath_var, ':',
94 &locale_path, &locale_path_len) != 0)
95 return NULL;
97 if (__argz_add_sep (&locale_path, &locale_path_len,
98 _nl_default_locale_path, ':') != 0)
99 return NULL;
102 /* Get the names for the locales we are interested in. We either
103 allow a composite name or a single name. */
104 for (cnt = 0; cnt < __LC_LAST; ++cnt)
105 if (cnt != LC_ALL)
106 newnames[cnt] = locale;
107 if (strchr (locale, ';') != NULL)
109 /* This is a composite name. Make a copy and split it up. */
110 char *np = strdupa (locale);
111 char *cp;
112 int specified_mask = 0;
114 while ((cp = strchr (np, '=')) != NULL)
116 for (cnt = 0; cnt < __LC_LAST; ++cnt)
117 if (cnt != LC_ALL
118 && (size_t) (cp - np) == _nl_category_name_sizes[cnt]
119 && memcmp (np, _nl_category_names[cnt], cp - np) == 0)
120 break;
122 if (cnt == __LC_LAST)
123 /* Bogus category name. */
124 ERROR_RETURN;
126 /* Found the category this clause sets. */
127 specified_mask |= 1 << cnt;
128 newnames[cnt] = ++cp;
129 cp = strchr (cp, ';');
130 if (cp != NULL)
132 /* Examine the next clause. */
133 *cp = '\0';
134 np = cp + 1;
136 else
137 /* This was the last clause. We are done. */
138 break;
141 if (category_mask &~ specified_mask)
142 /* The composite name did not specify all categories we need. */
143 ERROR_RETURN;
146 /* Now process all categories we are interested in. */
147 names_len = 0;
148 for (cnt = 0; cnt < __LC_LAST; ++cnt)
150 if ((category_mask & 1 << cnt) != 0)
152 result.__locales[cnt] = _nl_find_locale (locale_path,
153 locale_path_len,
154 cnt, &newnames[cnt]);
155 if (result.__locales[cnt] == NULL)
157 free_cnt_data_and_exit:
158 while (cnt-- > 0)
159 if (((category_mask & 1 << cnt) != 0)
160 && result.__locales[cnt]->usage_count != UNDELETABLE)
161 /* We can remove the data. */
162 _nl_remove_locale (cnt, result.__locales[cnt]);
163 return NULL;
166 if (newnames[cnt] != _nl_C_name)
167 names_len += strlen (newnames[cnt]) + 1;
169 else if (cnt != LC_ALL && result.__names[cnt] != _nl_C_name)
170 /* Tally up the unchanged names from BASE as well. */
171 names_len += strlen (result.__names[cnt]) + 1;
174 /* We successfully loaded all required data. Allocate a new structure.
175 We can't just reuse the BASE pointer, because the name strings are
176 changing and we need the old name string area intact so we can copy
177 out of it into the new one without overlap problems should some
178 category's name be getting longer. */
179 result_ptr = malloc (sizeof (struct __locale_struct) + names_len);
180 if (result_ptr == NULL)
182 cnt = __LC_LAST;
183 goto free_cnt_data_and_exit;
186 if (base == NULL)
188 /* Fill in this new structure from scratch. */
190 char *namep = (char *) (result_ptr + 1);
192 /* Install copied new names in the new structure's __names array.
193 If resolved to "C", that is already in RESULT.__names to start. */
194 for (cnt = 0; cnt < __LC_LAST; ++cnt)
195 if ((category_mask & 1 << cnt) != 0 && newnames[cnt] != _nl_C_name)
197 result.__names[cnt] = namep;
198 namep = __stpcpy (namep, newnames[cnt]) + 1;
201 *result_ptr = result;
203 else
205 /* We modify the base structure. */
207 char *namep = (char *) (result_ptr + 1);
209 for (cnt = 0; cnt < __LC_LAST; ++cnt)
210 if ((category_mask & 1 << cnt) != 0)
212 if (base->__locales[cnt]->usage_count != UNDELETABLE)
213 /* We can remove the old data. */
214 _nl_remove_locale (cnt, base->__locales[cnt]);
215 result_ptr->__locales[cnt] = result.__locales[cnt];
217 if (newnames[cnt] == _nl_C_name)
218 result_ptr->__names[cnt] = _nl_C_name;
219 else
221 result_ptr->__names[cnt] = namep;
222 namep = __stpcpy (namep, newnames[cnt]) + 1;
225 else if (cnt != LC_ALL)
227 /* The RESULT members point into the old BASE structure. */
228 result_ptr->__locales[cnt] = result.__locales[cnt];
229 if (result.__names[cnt] == _nl_C_name)
230 result_ptr->__names[cnt] = _nl_C_name;
231 else
233 result_ptr->__names[cnt] = namep;
234 namep = __stpcpy (namep, result.__names[cnt]) + 1;
238 free (base);
241 /* Update the special members. */
242 update:
244 union locale_data_value *ctypes = result_ptr->__locales[LC_CTYPE]->values;
245 result_ptr->__ctype_b = (const unsigned short int *)
246 ctypes[_NL_ITEM_INDEX (_NL_CTYPE_CLASS)].string + 128;
247 result_ptr->__ctype_tolower = (const int *)
248 ctypes[_NL_ITEM_INDEX (_NL_CTYPE_TOLOWER)].string + 128;
249 result_ptr->__ctype_toupper = (const int *)
250 ctypes[_NL_ITEM_INDEX (_NL_CTYPE_TOUPPER)].string + 128;
253 return result_ptr;
255 weak_alias (__newlocale, newlocale)