1 /* 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. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Paul Borman at Krystal Technologies. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 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. 22 * 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 33 * SUCH DAMAGE. 34 */ 35 36 #include "lint.h" 37 #include <sys/types.h> 38 #include <sys/stat.h> 39 #include <errno.h> 40 #include <limits.h> 41 #include <locale.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 #include <stdio.h> 46 #include "mtlib.h" 47 #include "collate.h" 48 #include "lnumeric.h" /* for struct lc_numeric */ 49 #include "lctype.h" /* for struct lc_ctype */ 50 #include "setlocale.h" 51 #include "../i18n/_loc_path.h" 52 #include "localeimpl.h" 53 #include "../i18n/_locale.h" 54 55 /* 56 * Path to locale storage directory. See ../i18n/_loc_path.h 57 */ 58 char *_PathLocale = _DFLT_LOC_PATH; 59 60 static char *current_locale(locale_t, int); 61 static void install_legacy(locale_t, int); 62 63 static mutex_t setlocale_lock = DEFAULTMUTEX; 64 static locale_t setlocale_list = NULL; 65 66 char * 67 setlocale(int category, const char *locname) 68 { 69 locale_t loc; 70 locale_t srch; 71 int mask; 72 73 if (category < 0 || category > LC_ALL) { 74 errno = EINVAL; 75 return (NULL); 76 } 77 78 if (locname == NULL) 79 return (current_locale(___global_locale, category)); 80 81 mask = (category == LC_ALL ? LC_ALL_MASK : (1 << category)); 82 83 loc = newlocale(mask, locname, NULL); 84 if (loc == NULL) { 85 return (NULL); 86 } 87 88 /* 89 * This next logic looks to see if we have ever used the same locale 90 * settings before. If so, we reuse it. We avoid ever calling 91 * freelocale() on a locale setting built up by setlocale, this 92 * ensures that consumers (uselocale) will always be thread safe; 93 * the actual locale data objects are never freed, and unique 94 * locale objects are also never freed. We reuse to avoid leaking 95 * memory in applications that call setlocale repeatedly. 96 */ 97 lmutex_lock(&setlocale_lock); 98 for (srch = setlocale_list; srch != NULL; srch = srch->next) { 99 if (memcmp(srch->locdata, loc->locdata, 100 sizeof (loc->locdata[0]) * LC_ALL) != 0) { 101 continue; 102 } 103 break; 104 } 105 106 if (srch == NULL) { 107 /* this is a new locale, save it for reuse later */ 108 loc->next = setlocale_list; 109 setlocale_list = loc; 110 } else { 111 /* we already had it, toss the new, and use what we found */ 112 freelocale(loc); 113 loc = srch; 114 } 115 ___global_locale = loc; 116 117 install_legacy(loc, mask); 118 lmutex_unlock(&setlocale_lock); 119 120 return (current_locale(loc, category)); 121 } 122 123 static char * 124 current_locale(locale_t loc, int cat) 125 { 126 int composite = 0; 127 static char buf[LC_ALL * (ENCODING_LEN + 1 + 1)]; 128 129 if (cat != LC_ALL) { 130 return (loc->locdata[cat]->l_lname); 131 } 132 133 /* Look to see if any category is different */ 134 for (int i = 1; i < LC_ALL; ++i) { 135 if (strcmp(loc->locdata[0]->l_lname, 136 loc->locdata[i]->l_lname) != 0) { 137 composite = 1; 138 break; 139 } 140 } 141 142 if (composite) { 143 /* 144 * Note ordering of these follows the numeric order, 145 * if the order is changed, then setlocale() will need 146 * to be changed as well. 147 */ 148 (void) snprintf(buf, sizeof (buf), 149 "%s/%s/%s/%s/%s/%s", 150 loc->locdata[LC_CTYPE]->l_lname, 151 loc->locdata[LC_NUMERIC]->l_lname, 152 loc->locdata[LC_TIME]->l_lname, 153 loc->locdata[LC_COLLATE]->l_lname, 154 loc->locdata[LC_MONETARY]->l_lname, 155 loc->locdata[LC_MESSAGES]->l_lname); 156 return (buf); 157 } 158 159 return (loc->locdata[0]->l_lname); 160 } 161 162 static void 163 install_legacy(locale_t loc, int mask) 164 { 165 /* 166 * Update the legacy fixed variables that may be baked into 167 * legacy programs. This is really unfortunate, but we can't 168 * solve for them otherwise. Note that such legacy programs 169 * are only going to see the global locale settings, and cannot 170 * benefit from uselocale(). 171 */ 172 if (mask & LC_NUMERIC_MASK) { 173 struct lc_numeric *lnum; 174 lnum = loc->locdata[LC_NUMERIC]->l_data[0]; 175 _numeric[0] = *lnum->decimal_point; 176 _numeric[1] = *lnum->thousands_sep; 177 } 178 179 if (mask & LC_CTYPE_MASK) { 180 struct lc_ctype *lct; 181 lct = loc->locdata[LC_CTYPE]->l_data[0]; 182 for (int i = 0; i < _CACHED_RUNES; i++) { 183 /* ctype can only encode the lower 8 bits. */ 184 __ctype[i+1] = lct->lc_ctype_mask[i] & 0xff; 185 __ctype_mask[i] = lct->lc_ctype_mask[i]; 186 } 187 188 /* The bottom half is the toupper/lower array */ 189 for (int i = 0; i < _CACHED_RUNES; i++) { 190 int u, l; 191 __ctype[258 + i] = i; 192 u = lct->lc_trans_upper[i]; 193 l = lct->lc_trans_lower[i]; 194 if (u && u != i) 195 __ctype[258+i] = u; 196 if (l && l != i) 197 __ctype[258+i] = l; 198 199 /* Don't forget these annoyances either! */ 200 __trans_upper[i] = u; 201 __trans_lower[i] = l; 202 } 203 204 /* Maximum mblen, cswidth, weird legacy */ 205 __ctype[520] = lct->lc_max_mblen; 206 } 207 }