]>
git.saurik.com Git - apple/libc.git/blob - locale.subproj/setlocale.c
7dc8b93fc377cfd17ab2b83106e613046398cdc2
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1991, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
27 * Paul Borman at Krystal Technologies.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * Category names for getenv()
68 static char *categories
[_LC_LAST
] = {
78 * Current locales for each category
80 static char current_categories
[_LC_LAST
][32] = {
90 * The locales we are going to try and load
92 static char new_categories
[_LC_LAST
][32];
94 static char current_locale_string
[_LC_LAST
* 33];
95 static char *PathLocale
;
97 static char *currentlocale
__P((void));
98 static char *loadlocale
__P((int));
101 setlocale(category
, locale
)
109 PathLocale
= _PATH_LOCALE
;
111 if (category
< 0 || category
>= _LC_LAST
)
116 current_categories
[category
] : currentlocale());
119 * Default to the current locale for everything.
121 for (i
= 1; i
< _LC_LAST
; ++i
)
122 (void)strcpy(new_categories
[i
], current_categories
[i
]);
125 * Now go fill up new_categories from the locale argument
128 env
= getenv(categories
[category
]);
131 env
= getenv(categories
[0]);
134 env
= getenv("LANG");
139 (void) strncpy(new_categories
[category
], env
, 31);
140 new_categories
[category
][31] = 0;
142 for (i
= 1; i
< _LC_LAST
; ++i
) {
143 if (!(env
= getenv(categories
[i
])))
144 env
= new_categories
[0];
145 (void)strncpy(new_categories
[i
], env
, 31);
146 new_categories
[i
][31] = 0;
149 } else if (category
) {
150 (void)strncpy(new_categories
[category
], locale
, 31);
151 new_categories
[category
][31] = 0;
153 if ((r
= strchr(locale
, '/')) == 0) {
154 for (i
= 1; i
< _LC_LAST
; ++i
) {
155 (void)strncpy(new_categories
[i
], locale
, 31);
156 new_categories
[i
][31] = 0;
159 for (i
= 1; r
[1] == '/'; ++r
);
161 return (NULL
); /* Hmm, just slashes... */
163 len
= r
- locale
> 31 ? 31 : r
- locale
;
164 (void)strncpy(new_categories
[i
++], locale
, len
);
165 new_categories
[i
++][len
] = 0;
167 while (*locale
== '/')
169 while (*++r
&& *r
!= '/');
172 (void)strcpy(new_categories
[i
],
173 new_categories
[i
-1]);
178 return (loadlocale(category
));
181 for (i
= 1; i
< _LC_LAST
; ++i
)
182 if (loadlocale(i
) != NULL
)
185 return (currentlocale());
194 (void)strcpy(current_locale_string
, current_categories
[1]);
196 for (i
= 2; i
< _LC_LAST
; ++i
)
197 if (strcmp(current_categories
[1], current_categories
[i
])) {
198 (void)snprintf(current_locale_string
,
199 sizeof(current_locale_string
), "%s/%s/%s/%s/%s",
200 current_categories
[1], current_categories
[2],
201 current_categories
[3], current_categories
[4],
202 current_categories
[5]);
205 return (current_locale_string
);
214 if (strcmp(new_categories
[category
],
215 current_categories
[category
]) == 0)
216 return (current_categories
[category
]);
218 if (category
== LC_CTYPE
) {
219 if (setrunelocale(new_categories
[LC_CTYPE
]))
221 (void)strcpy(current_categories
[LC_CTYPE
],
222 new_categories
[LC_CTYPE
]);
223 return (current_categories
[LC_CTYPE
]);
226 if (!strcmp(new_categories
[category
], "C") ||
227 !strcmp(new_categories
[category
], "POSIX")) {
230 * Some day this will need to reset the locale to the default
231 * C locale. Since we have no way to change them as of yet,
232 * there is no need to reset them.
234 (void)strcpy(current_categories
[category
],
235 new_categories
[category
]);
236 return (current_categories
[category
]);
240 * Some day we will actually look at this file.
242 (void)snprintf(name
, sizeof(name
), "%s/%s/%s",
243 PathLocale
, new_categories
[category
], categories
[category
]);