1 /* Copyright (C) 1991, 1992, 1995, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
22 #include <libc-lock.h>
28 #include "localeinfo.h"
30 /* For each category declare two external variables (with weak references):
31 extern const struct locale_data *_nl_current_CATEGORY;
32 This points to the current locale's in-core data for CATEGORY.
33 extern const struct locale_data _nl_C_CATEGORY;
34 This contains the built-in "C"/"POSIX" locale's data for CATEGORY.
35 Both are weak references; if &_nl_current_CATEGORY is zero,
36 then nothing is using the locale data. */
37 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
38 extern const struct locale_data *_nl_current_##category; \
39 extern const struct locale_data _nl_C_##category; \
40 weak_extern (_nl_current_##category) weak_extern (_nl_C_##category)
41 #include "categories.def"
42 #undef DEFINE_CATEGORY
44 /* Array indexed by category of pointers to _nl_current_CATEGORY slots.
45 Elements are zero for categories whose data is never used. */
46 static const struct locale_data
* *const _nl_current
[] =
48 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
49 [category] = &_nl_current_##category,
50 #include "categories.def"
51 #undef DEFINE_CATEGORY
54 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
55 Elements are zero for categories whose data is never used. */
56 const struct locale_data
*const _nl_C
[] =
58 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
59 [category] = &_nl_C_##category,
60 #include "categories.def"
61 #undef DEFINE_CATEGORY
65 /* Define an array of category names (also the environment variable names),
66 indexed by integral category. */
67 const char *const _nl_category_names
[] =
69 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
70 [category] = category_name,
71 #include "categories.def"
72 #undef DEFINE_CATEGORY
75 /* An array of their lengths, for convenience. */
76 const size_t _nl_category_name_sizes
[] =
78 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
79 [category] = sizeof (category_name) - 1,
80 #include "categories.def"
81 #undef DEFINE_CATEGORY
82 [LC_ALL
] = sizeof ("LC_ALL") - 1
86 /* Declare the postload functions used below. */
88 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
89 #define DEFINE_CATEGORY(category, category_name, items, postload, b, c, d) \
90 extern void postload (void);
91 #include "categories.def"
92 #undef DEFINE_CATEGORY
95 /* Define an array indexed by category of postload functions to call after
96 loading and installing that category's data. */
97 static void (*const _nl_category_postload
[]) (void) =
99 #define DEFINE_CATEGORY(category, category_name, items, postload, b, c, d) \
100 [category] = postload,
101 #include "categories.def"
102 #undef DEFINE_CATEGORY
106 /* Name of current locale for each individual category.
107 Each is malloc'd unless it is nl_C_name. */
108 static const char *_nl_current_names
[] =
110 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
111 [category] = _nl_C_name,
112 #include "categories.def"
113 #undef DEFINE_CATEGORY
114 [LC_ALL
] = _nl_C_name
/* For LC_ALL. */
118 /* Lock for protecting global data. */
119 __libc_lock_define_initialized (static, lock
)
122 /* Use this when we come along an error. */
123 #define ERROR_RETURN \
131 clever_copy (const char *string
)
136 if (strcmp (string
, "C") == 0 || strcmp (string
, "POSIX") == 0)
137 /* This return is dangerous because the returned string might be
138 placed in read-only memory. But everything should be set up to
140 return (char *) _nl_C_name
;
142 len
= strlen (string
) + 1;
143 new = (char *) malloc (len
);
144 return new != NULL
? memcpy (new, string
, len
) : NULL
;
148 /* Construct a new composite name. */
150 new_composite_name (int category
, char *newnames
[LC_ALL
])
158 for (i
= 0; i
< LC_ALL
; ++i
)
160 char *name
= (category
== LC_ALL
? newnames
[i
] :
161 category
== i
? newnames
[0] :
162 (char *) _nl_current_names
[i
]);
163 last_len
= strlen (name
);
164 cumlen
+= _nl_category_name_sizes
[i
] + 1 + last_len
+ 1;
165 if (i
> 0 && same
&& strcmp (name
, newnames
[0]) != 0)
171 /* All the categories use the same name. */
172 if (strcmp (newnames
[0], "C") == 0 || strcmp (newnames
[0], "POSIX") == 0)
173 return (char *) _nl_C_name
;
175 new = malloc (last_len
+ 1);
177 return new == NULL
? NULL
: memcpy (new, newnames
[0], last_len
+ 1);
180 new = malloc (cumlen
);
184 for (i
= 0; i
< LC_ALL
; ++i
)
186 /* Add "CATEGORY=NAME;" to the string. */
187 char *name
= (category
== LC_ALL
? newnames
[i
] :
188 category
== i
? newnames
[0] :
189 (char *) _nl_current_names
[i
]);
190 p
= __stpcpy (p
, _nl_category_names
[i
]);
192 p
= __stpcpy (p
, name
);
195 p
[-1] = '\0'; /* Clobber the last ';'. */
200 /* Put NAME in _nl_current_names. */
202 setname (int category
, const char *name
)
204 if (_nl_current
[category
] == NULL
205 && _nl_current_names
[category
] != _nl_C_name
)
206 free ((void *) _nl_current_names
[category
]);
208 _nl_current_names
[category
] = name
;
212 /* Put DATA in *_nl_current[CATEGORY]. */
214 setdata (int category
, const struct locale_data
*data
)
216 if (_nl_current
[category
] != NULL
)
218 *_nl_current
[category
] = data
;
219 if (_nl_category_postload
[category
])
220 (*_nl_category_postload
[category
]) ();
226 setlocale (int category
, const char *locale
)
229 size_t locale_path_len
;
232 /* Sanity check for CATEGORY argument. */
233 if (category
< 0 || category
> LC_ALL
)
236 /* Does user want name of current locale? */
238 return (char *) _nl_current_names
[category
];
240 if (strcmp (locale
, _nl_current_names
[category
]) == 0)
241 /* Changing to the same thing. */
242 return (char *) _nl_current_names
[category
];
244 /* We perhaps really have to load some data. So we determine the
245 path in which to look for the data now. The environment variable
246 `LOCPATH' must only be used when the binary has no SUID or SGID
251 if (!__libc_enable_secure
)
253 char *locpath_var
= getenv ("LOCPATH");
255 if (locpath_var
!= NULL
&& locpath_var
[0] != '\0')
256 if (__argz_create_sep (locpath_var
, ':',
257 &locale_path
, &locale_path_len
) != 0)
261 if (__argz_append (&locale_path
, &locale_path_len
,
262 LOCALE_PATH
, sizeof (LOCALE_PATH
)) != 0)
265 if (category
== LC_ALL
)
267 /* The user wants to set all categories. The desired locales
268 for the individual categories can be selected by using a
269 composite locale name. This is a semi-colon separated list
270 of entries of the form `CATEGORY=VALUE'. */
271 char *newnames
[LC_ALL
];
272 const struct locale_data
*newdata
[LC_ALL
];
274 /* Set all name pointers to the argument name. */
275 for (category
= 0; category
< LC_ALL
; ++category
)
276 newnames
[category
] = (char *) locale
;
278 if (strchr (locale
, ';') != NULL
)
280 /* This is a composite name. Make a copy and split it up. */
281 char *np
= strdupa (locale
);
285 while ((cp
= strchr (np
, '=')) != NULL
)
287 for (cnt
= 0; cnt
< LC_ALL
; ++cnt
)
288 if ((size_t) (cp
- np
) == _nl_category_name_sizes
[cnt
]
289 && memcmp (np
, _nl_category_names
[cnt
], cp
- np
) == 0)
293 /* Bogus category name. */
296 /* Found the category this clause sets. */
297 newnames
[cnt
] = ++cp
;
298 cp
= strchr (cp
, ';');
301 /* Examine the next clause. */
306 /* This was the last clause. We are done. */
310 for (cnt
= 0; cnt
< LC_ALL
; ++cnt
)
311 if (newnames
[cnt
] == locale
)
312 /* The composite name did not specify all categories. */
316 /* Protect global data. */
317 __libc_lock_lock (lock
);
319 /* Load the new data for each category. */
320 while (category
-- > 0)
321 /* Only actually load the data if anything will use it. */
322 if (_nl_current
[category
] != NULL
)
324 newdata
[category
] = _nl_find_locale (locale_path
, locale_path_len
,
326 &newnames
[category
]);
328 if (newdata
[category
] == NULL
)
329 goto abort_composite
;
333 /* The data is never used; just change the name. */
334 newnames
[category
] = clever_copy (newnames
[category
]);
335 if (newnames
[category
] == NULL
)
336 goto abort_composite
;
339 /* Create new composite name. */
340 composite
= new_composite_name (LC_ALL
, newnames
);
341 if (composite
== NULL
)
343 /* Loading this part of the locale failed. Abort the
351 while (++category
< LC_ALL
)
352 if (_nl_current
[category
] != NULL
353 && newdata
[category
] != _nl_C
[category
])
354 _nl_free_locale (newdata
[category
]);
356 if (_nl_current
[category
] == NULL
357 && newnames
[category
] != _nl_C_name
)
358 free (newnames
[category
]);
365 /* Now we have loaded all the new data. Put it in place. */
366 for (category
= 0; category
< LC_ALL
; ++category
)
368 setdata (category
, newdata
[category
]);
369 setname (category
, newnames
[category
]);
371 setname (LC_ALL
, composite
);
374 /* Critical section left. */
375 __libc_lock_unlock (lock
);
381 const struct locale_data
*newdata
= NULL
;
382 char *newname
= NULL
;
384 /* Protect global data. */
385 __libc_lock_lock (lock
);
387 if (_nl_current
[category
] != NULL
)
389 /* Only actually load the data if anything will use it. */
390 newname
= (char *) locale
;
391 newdata
= _nl_find_locale (locale_path
, locale_path_len
, category
,
397 /* Create new composite name. */
398 composite
= new_composite_name (category
, &newname
);
399 if (composite
== NULL
)
401 /* If anything went wrong free what we managed to allocate
403 int save_errno
= errno
;
405 if (_nl_current
[category
] != NULL
)
406 _nl_free_locale (newdata
);
414 if (_nl_current
[category
] != NULL
)
415 setdata (category
, newdata
);
417 setname (category
, newname
);
418 setname (LC_ALL
, composite
);
421 /* Critical section left. */
422 __libc_lock_unlock (lock
);