4 #include "locale_impl.h"
8 static char buf[2+4*(LOCALE_NAME_MAX+1)];
10 char *setlocale(int cat, const char *name)
12 struct __locale_map *lm;
15 if (!libc.global_locale.messages_name) {
16 libc.global_locale.messages_name =
17 buf + 2 + 3*(LOCALE_NAME_MAX+1);
20 if ((unsigned)cat > LC_ALL) return 0;
22 /* For LC_ALL, setlocale is required to return a string which
23 * encodes the current setting for all categories. The format of
24 * this string is unspecified, and only the following code, which
25 * performs both the serialization and deserialization, depends
26 * on the format, so it can easily be changed if needed. */
29 char part[LOCALE_NAME_MAX+1];
30 if (name[0] && name[1]==';'
31 && strlen(name) > 2 + 3*(LOCALE_NAME_MAX+1)) {
34 setlocale(LC_CTYPE, part);
35 part[LOCALE_NAME_MAX] = 0;
36 for (i=LC_TIME; i<LC_MESSAGES; i++) {
37 memcpy(part, name + 2 + (i-2)*(LOCALE_NAME_MAX+1), LOCALE_NAME_MAX);
38 for (j=LOCALE_NAME_MAX-1; j && part[j]==';'; j--)
42 setlocale(LC_MESSAGES, name + 2 + 3*(LOCALE_NAME_MAX+1));
44 for (i=0; i<LC_ALL; i++)
48 memset(buf, ';', 2 + 3*(LOCALE_NAME_MAX+1));
49 buf[0] = libc.global_locale.ctype_utf8 ? 'U' : 'C';
50 for (i=LC_TIME; i<LC_MESSAGES; i++) {
51 lm = libc.global_locale.cat[i-2];
52 if (lm) memcpy(buf + 2 + (i-2)*(LOCALE_NAME_MAX+1),
53 lm->name, strlen(lm->name));
59 int adj = libc.global_locale.ctype_utf8;
60 __setlocalecat(&libc.global_locale, cat, name);
61 adj -= libc.global_locale.ctype_utf8;
62 if (adj) a_fetch_add(&libc.bytelocale_cnt_minus_1, adj);
67 return libc.global_locale.ctype_utf8 ? "C.UTF-8" : "C";
71 return libc.global_locale.messages_name[0]
72 ? libc.global_locale.messages_name : "C";
74 lm = libc.global_locale.cat[cat-2];
75 return lm ? lm->name : "C";