]>
git.ipfire.org Git - thirdparty/glibc.git/blob - locale/setlocale.c
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_extern (_nl_current_##category) weak_extern (_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);
173 return new == NULL
? NULL
: memcpy (new, newnames
[0], last_len
+ 1);
176 new = malloc (cumlen
);
180 for (i
= 0; i
< LC_ALL
; ++i
)
182 /* Add "CATEGORY=NAME;" to the string. */
183 char *name
= (category
== LC_ALL
? newnames
[i
] :
184 category
== i
? newnames
[0] :
185 (char *) _nl_current_names
[i
]);
186 p
= __stpcpy (p
, _nl_category_names
[i
]);
188 p
= __stpcpy (p
, name
);
191 p
[-1] = '\0'; /* Clobber the last ';'. */
196 /* Put NAME in _nl_current_names. */
198 setname (int category
, const char *name
)
200 if (_nl_current
[category
] == NULL
201 && _nl_current_names
[category
] != _nl_C_name
)
202 free ((void *) _nl_current_names
[category
]);
204 _nl_current_names
[category
] = name
;
208 /* Put DATA in *_nl_current[CATEGORY]. */
210 setdata (int category
, const struct locale_data
*data
)
212 if (_nl_current
[category
] != NULL
)
214 *_nl_current
[category
] = data
;
215 if (_nl_category_postload
[category
])
216 (*_nl_category_postload
[category
]) ();
222 setlocale (int category
, const char *locale
)
226 size_t locale_path_len
;
229 /* Sanity check for CATEGORY argument. */
230 if (category
< 0 || category
> LC_ALL
)
233 /* Does user want name of current locale? */
235 return (char *) _nl_current_names
[category
];
237 if (strcmp (locale
, _nl_current_names
[category
]) == 0)
238 /* Changing to the same thing. */
239 return (char *) _nl_current_names
[category
];
241 /* We perhaps really have to load some data. So we determine the
242 path in which to look for the data now. But this environment
243 variable must only be used when the binary has no SUID or SGID
248 locpath_var
= getenv ("LOCPATH");
249 if (locpath_var
!= NULL
&& locpath_var
[0] != '\0'
250 && __getuid () == __geteuid () && __getgid () == __getegid ())
251 if (__argz_create_sep (locpath_var
, ':',
252 &locale_path
, &locale_path_len
) != 0)
255 if (__argz_append (&locale_path
, &locale_path_len
,
256 LOCALE_PATH
, sizeof (LOCALE_PATH
)) != 0)
259 if (category
== LC_ALL
)
261 /* The user wants to set all categories. The desired locales
262 for the individual categories can be selected by using a
263 composite locale name. This is a semi-colon separated list
264 of entries of the form `CATEGORY=VALUE'. */
265 char *newnames
[LC_ALL
];
266 const struct locale_data
*newdata
[LC_ALL
];
268 /* Set all name pointers to the argument name. */
269 for (category
= 0; category
< LC_ALL
; ++category
)
270 newnames
[category
] = (char *) locale
;
272 if (strchr (locale
, ';') != NULL
)
274 /* This is a composite name. Make a copy and split it up. */
275 char *np
= strdupa (locale
);
279 while ((cp
= strchr (np
, '=')) != NULL
)
281 for (cnt
= 0; cnt
< LC_ALL
; ++cnt
)
282 if ((size_t) (cp
- np
) == _nl_category_name_sizes
[cnt
]
283 && memcmp (np
, _nl_category_names
[cnt
], cp
- np
) == 0)
287 /* Bogus category name. */
290 /* Found the category this clause sets. */
291 newnames
[cnt
] = ++cp
;
292 cp
= strchr (cp
, ';');
295 /* Examine the next clause. */
300 /* This was the last clause. We are done. */
304 for (cnt
= 0; cnt
< LC_ALL
; ++cnt
)
305 if (newnames
[cnt
] == locale
)
306 /* The composite name did not specify all categories. */
310 /* Load the new data for each category. */
311 while (category
-- > 0)
312 /* Only actually load the data if anything will use it. */
313 if (_nl_current
[category
] != NULL
)
315 newdata
[category
] = _nl_find_locale (locale_path
, locale_path_len
,
317 &newnames
[category
]);
319 if (newdata
[category
] == NULL
)
321 /* Loading this part of the locale failed. Abort the
327 while (++category
< LC_ALL
)
328 if (_nl_current
[category
] != NULL
)
329 _nl_free_locale (newdata
[category
]);
331 if (_nl_current
[category
] == NULL
332 && newnames
[category
] != _nl_C_name
)
333 free (newnames
[category
]);
341 /* The data is never used; just change the name. */
342 newnames
[category
] = clever_copy (newnames
[category
]);
343 if (newnames
[category
] == NULL
)
344 goto abort_composite
;
347 /* Create new composite name. */
348 composite
= new_composite_name (LC_ALL
, newnames
);
349 if (composite
== NULL
)
352 goto abort_composite
;
355 /* Now we have loaded all the new data. Put it in place. */
356 for (category
= 0; category
< LC_ALL
; ++category
)
358 setdata (category
, newdata
[category
]);
359 setname (category
, newnames
[category
]);
361 setname (LC_ALL
, composite
);
367 const struct locale_data
*newdata
= NULL
;
368 char *newname
= NULL
;
370 if (_nl_current
[category
] != NULL
)
372 /* Only actually load the data if anything will use it. */
373 newname
= (char *) locale
;
374 newdata
= _nl_find_locale (locale_path
, locale_path_len
, category
,
380 /* Create new composite name. */
381 composite
= new_composite_name (category
, &newname
);
382 if (composite
== NULL
)
384 /* If anything went wrong free what we managed to allocate
386 int save_errno
= errno
;
388 if (_nl_current
[category
] != NULL
)
389 _nl_free_locale (newdata
);
395 if (_nl_current
[category
] != NULL
)
396 setdata (category
, newdata
);
398 setname (category
, newname
);
399 setname (LC_ALL
, composite
);