2 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
3 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4 * Copyright (c) 1996 - 2002 FreeBSD Project
5 * Copyright (c) 1991, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Paul Borman at Krystal Technologies.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/types.h>
48 #include "lnumeric.h" /* for struct lc_numeric */
49 #include "lctype.h" /* for struct lc_ctype */
51 #include "setlocale.h"
52 #include "../i18n/_loc_path.h"
53 #include "localeimpl.h"
54 #include "../i18n/_locale.h"
58 * Path to locale storage directory. See ../i18n/_loc_path.h
60 char *_PathLocale
= _DFLT_LOC_PATH
;
62 static void install_legacy(locale_t
, int);
64 static mutex_t setlocale_lock
= DEFAULTMUTEX
;
65 static locale_t setlocale_list
= NULL
;
68 setlocale(int category
, const char *locname
)
74 if (category
< 0 || category
> LC_ALL
) {
80 return (current_locale(___global_locale
, category
));
82 mask
= (category
== LC_ALL
? LC_ALL_MASK
: (1 << category
));
84 loc
= newlocale(mask
, locname
, NULL
);
90 * This next logic looks to see if we have ever used the same locale
91 * settings before. If so, we reuse it. We avoid ever calling
92 * freelocale() on a locale setting built up by setlocale, this
93 * ensures that consumers (uselocale) will always be thread safe;
94 * the actual locale data objects are never freed, and unique
95 * locale objects are also never freed. We reuse to avoid leaking
96 * memory in applications that call setlocale repeatedly.
98 lmutex_lock(&setlocale_lock
);
99 for (srch
= setlocale_list
; srch
!= NULL
; srch
= srch
->next
) {
100 if (strcmp(srch
->locname
, loc
->locname
) == 0) {
106 /* this is a new locale, save it for reuse later */
107 loc
->next
= setlocale_list
;
109 setlocale_list
= loc
;
111 /* we already had it, toss the new, and use what we found */
115 ___global_locale
= loc
;
117 install_legacy(loc
, mask
);
118 lmutex_unlock(&setlocale_lock
);
120 return (current_locale(loc
, category
));
124 current_locale(locale_t loc
, int cat
)
133 return (loc
->locdata
[cat
]->l_lname
);
135 return (loc
->locname
);
142 install_legacy(locale_t loc
, int mask
)
145 * Update the legacy fixed variables that may be baked into
146 * legacy programs. This is really unfortunate, but we can't
147 * solve for them otherwise. Note that such legacy programs
148 * are only going to see the global locale settings, and cannot
149 * benefit from uselocale().
151 if (mask
& LC_NUMERIC_MASK
) {
152 struct lc_numeric
*lnum
;
153 lnum
= loc
->locdata
[LC_NUMERIC
]->l_data
[0];
154 _numeric
[0] = *lnum
->decimal_point
;
155 _numeric
[1] = *lnum
->thousands_sep
;
158 if (mask
& LC_CTYPE_MASK
) {
159 struct lc_ctype
*lct
;
160 lct
= loc
->locdata
[LC_CTYPE
]->l_data
[0];
161 for (int i
= 0; i
< _CACHED_RUNES
; i
++) {
162 /* ctype can only encode the lower 8 bits. */
163 __ctype
[i
+1] = lct
->lc_ctype_mask
[i
] & 0xff;
164 __ctype_mask
[i
] = lct
->lc_ctype_mask
[i
];
167 /* The bottom half is the toupper/lower array */
168 for (int i
= 0; i
< _CACHED_RUNES
; i
++) {
170 __ctype
[258 + i
] = i
;
171 u
= lct
->lc_trans_upper
[i
];
172 l
= lct
->lc_trans_lower
[i
];
178 /* Don't forget these annoyances either! */
179 __trans_upper
[i
] = u
;
180 __trans_lower
[i
] = l
;
183 /* Maximum mblen, cswidth, weird legacy */
184 __ctype
[520] = lct
->lc_max_mblen
;