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. */
27 #include "localeinfo.h"
29 /* For each category declare two external variables (with weak references):
30 extern const struct locale_data *_nl_current_CATEGORY;
31 This points to the current locale's in-core data for CATEGORY.
32 extern const struct locale_data _nl_C_CATEGORY;
33 This contains the built-in "C"/"POSIX" locale's data for CATEGORY.
34 Both are weak references; if &_nl_current_CATEGORY is zero,
35 then nothing is using the locale data. */
36 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
37 extern const struct locale_data *_nl_current_##category; \
38 extern const struct locale_data _nl_C_##category; \
39 weak_symbol (_nl_current_##category) weak_symbol (_nl_C_##category)
40 #include "categories.def"
41 #undef DEFINE_CATEGORY
43 /* Array indexed by category of pointers to _nl_current_CATEGORY slots.
44 Elements are zero for categories whose data is never used. */
45 static const struct locale_data
* *const _nl_current
[] =
47 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
48 [category] = &_nl_current_##category,
49 #include "categories.def"
50 #undef DEFINE_CATEGORY
53 /* Array indexed by category of pointers to _nl_C_CATEGORY slots.
54 Elements are zero for categories whose data is never used. */
55 const struct locale_data
*const _nl_C
[] =
57 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
58 [category] = &_nl_C_##category,
59 #include "categories.def"
60 #undef DEFINE_CATEGORY
64 /* Define an array of category names (also the environment variable names),
65 indexed by integral category. */
66 const char *const _nl_category_names
[] =
68 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
69 [category] = category_name,
70 #include "categories.def"
71 #undef DEFINE_CATEGORY
74 /* An array of their lengths, for convenience. */
75 const size_t _nl_category_name_sizes
[] =
77 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
78 [category] = sizeof (category_name) - 1,
79 #include "categories.def"
80 #undef DEFINE_CATEGORY
81 [LC_ALL
] = sizeof ("LC_ALL") - 1
85 /* Declare the postload functions used below. */
87 #define NO_POSTLOAD _nl_postload_ctype /* Harmless thing known to exist. */
88 #define DEFINE_CATEGORY(category, category_name, items, postload, b, c, d) \
89 extern void postload (void);
90 #include "categories.def"
91 #undef DEFINE_CATEGORY
94 /* Define an array indexed by category of postload functions to call after
95 loading and installing that category's data. */
96 void (*const _nl_category_postload
[]) (void) =
98 #define DEFINE_CATEGORY(category, category_name, items, postload, b, c, d) \
99 [category] = postload,
100 #include "categories.def"
101 #undef DEFINE_CATEGORY
105 /* Name of current locale for each individual category.
106 Each is malloc'd unless it is nl_C_name. */
107 static const char *_nl_current_names
[] =
109 #define DEFINE_CATEGORY(category, category_name, items, a, b, c, d) \
110 [category] = _nl_C_name,
111 #include "categories.def"
112 #undef DEFINE_CATEGORY
113 [LC_ALL
] = _nl_C_name
/* For LC_ALL. */
118 /* Use this when we come along an error. */
119 #define ERROR_RETURN \
127 clever_copy (const char *string
)
132 if (strcmp (string
, "C") == 0 || strcmp (string
, "POSIX") == 0)
133 /* This return is dangerous because the returned string might be
134 placed in read-only memory. But everything should be set up to
136 return (char *) _nl_C_name
;
138 len
= strlen (string
) + 1;
139 new = (char *) malloc (len
);
140 return new != NULL
? memcpy (new, string
, len
) : NULL
;
144 /* Construct a new composite name. */
146 new_composite_name (int category
, char *newnames
[LC_ALL
])
154 for (i
= 0; i
< LC_ALL
; ++i
)
156 char *name
= (category
== LC_ALL
? newnames
[i
] :
157 category
== i
? newnames
[0] :
158 (char *) _nl_current_names
[i
]);
159 last_len
= strlen (name
);
160 cumlen
+= _nl_category_name_sizes
[i
] + 1 + last_len
+ 1;
161 if (i
> 0 && same
&& strcmp (name
, newnames
[0]) != 0)
167 /* All the categories use the same name. */
168 if (strcmp (newnames
[0], "C") == 0 || strcmp (newnames
[0], "POSIX") == 0)
169 return (char *) _nl_C_name
;
171 new = malloc (last_len
+ 1);
175 memcpy (new, newnames
[0], last_len
+ 1);
179 new = malloc (cumlen
);
183 for (i
= 0; i
< LC_ALL
; ++i
)
185 /* Add "CATEGORY=NAME;" to the string. */
186 char *name
= (category
== LC_ALL
? newnames
[i
] :
187 category
== i
? newnames
[0] :
188 (char *) _nl_current_names
[i
]);
189 p
= __stpcpy (p
, _nl_category_names
[i
]);
191 p
= __stpcpy (p
, name
);
194 p
[-1] = '\0'; /* Clobber the last ';'. */
199 /* Put NAME in _nl_current_names. */
201 setname (int category
, const char *name
)
203 if (_nl_current
[category
] == NULL
204 && _nl_current_names
[category
] != _nl_C_name
)
205 free ((void *) _nl_current_names
[category
]);
207 _nl_current_names
[category
] = name
;
211 /* Put DATA in *_nl_current[CATEGORY]. */
213 setdata (int category
, const struct locale_data
*data
)
215 if (_nl_current
[category
] != NULL
)
217 *_nl_current
[category
] = data
;
218 if (_nl_category_postload
[category
])
219 (*_nl_category_postload
[category
]) ();
225 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. But this environment
246 variable must only be used when the binary has no SUID or SGID
251 locpath_var
= getenv ("LOCPATH");
252 if (locpath_var
!= NULL
&& locpath_var
[0] != '\0'
253 && __getuid () == __geteuid () && __getgid () == __getegid ())
254 if (__argz_create_sep (locpath_var
, ':',
255 &locale_path
, &locale_path_len
) != 0)
258 if (__argz_append (&locale_path
, &locale_path_len
,
259 LOCALE_PATH
, sizeof (LOCALE_PATH
)) != 0)
262 if (category
== LC_ALL
)
264 /* The user wants to set all categories. The desired locales
265 for the individual categories can be selected by using a
266 composite locale name. This is a semi-colon separated list
267 of entries of the form `CATEGORY=VALUE'. */
268 char *newnames
[LC_ALL
];
269 const struct locale_data
*newdata
[LC_ALL
];
271 /* Set all name pointers to the argument name. */
272 for (category
= 0; category
< LC_ALL
; ++category
)
273 newnames
[category
] = (char *) locale
;
275 if (strchr (locale
, ';') != NULL
)
277 /* This is a composite name. Make a copy and split it up. */
278 char *np
= strdupa (locale
);
282 while ((cp
= strchr (np
, '=')) != NULL
)
284 for (cnt
= 0; cnt
< LC_ALL
; ++cnt
)
285 if (cp
- np
== _nl_category_name_sizes
[cnt
]
286 && memcmp (np
, _nl_category_names
[cnt
], cp
- np
) == 0)
290 /* Bogus category name. */
293 /* Found the category this clause sets. */
294 newnames
[cnt
] = ++cp
;
295 cp
= strchr (cp
, ';');
298 /* Examine the next clause. */
303 /* This was the last clause. We are done. */
307 for (cnt
= 0; cnt
< LC_ALL
; ++cnt
)
308 if (newnames
[cnt
] == locale
)
309 /* The composite name did not specify all categories. */
313 /* Load the new data for each category. */
314 while (category
-- > 0)
315 /* Only actually load the data if anything will use it. */
316 if (_nl_current
[category
] != NULL
)
318 newdata
[category
] = _nl_find_locale (locale_path
, locale_path_len
,
320 &newnames
[category
]);
322 if (newdata
[category
] == NULL
)
324 /* Loading this part of the locale failed. Abort the
330 while (++category
< LC_ALL
)
331 if (_nl_current
[category
] != NULL
)
332 _nl_free_locale (newdata
[category
]);
334 if (_nl_current
[category
] == NULL
335 && newnames
[category
] != _nl_C_name
)
336 free (newnames
[category
]);
344 /* The data is never used; just change the name. */
345 newnames
[category
] = clever_copy (newnames
[category
]);
346 if (newnames
[category
] == NULL
)
347 goto abort_composite
;
350 /* Create new composite name. */
351 composite
= new_composite_name (LC_ALL
, newnames
);
352 if (composite
== NULL
)
355 goto abort_composite
;
358 /* Now we have loaded all the new data. Put it in place. */
359 for (category
= 0; category
< LC_ALL
; ++category
)
361 setdata (category
, newdata
[category
]);
362 setname (category
, newnames
[category
]);
364 setname (LC_ALL
, composite
);
370 const struct locale_data
*newdata
;
373 if (_nl_current
[category
] != NULL
)
375 /* Only actually load the data if anything will use it. */
376 newname
= (char *) locale
;
377 newdata
= _nl_find_locale (locale_path
, locale_path_len
, category
,
383 /* Create new composite name. */
384 composite
= new_composite_name (category
, &newname
);
385 if (composite
== NULL
)
387 /* If anything went wrong free what we managed to allocate
389 int save_errno
= errno
;
391 if (_nl_current
[category
] != NULL
)
392 _nl_free_locale (newdata
);
398 if (_nl_current
[category
] != NULL
)
399 setdata (category
, newdata
);
401 setname (category
, newname
);
402 setname (LC_ALL
, composite
);