Print this page
Fix compile errors, code review feedback, and add basic libc test suite.
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libc/port/locale/setlocale.c
+++ new/usr/src/lib/libc/port/locale/setlocale.c
1 1 /*
2 2 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
3 3 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4 4 * Copyright (c) 1996 - 2002 FreeBSD Project
5 5 * Copyright (c) 1991, 1993
6 6 * The Regents of the University of California. All rights reserved.
7 7 *
8 8 * This code is derived from software contributed to Berkeley by
9 9 * Paul Borman at Krystal Technologies.
10 10 *
11 11 * Redistribution and use in source and binary forms, with or without
12 12 * modification, are permitted provided that the following conditions
13 13 * are met:
14 14 * 1. Redistributions of source code must retain the above copyright
15 15 * notice, this list of conditions and the following disclaimer.
16 16 * 2. Redistributions in binary form must reproduce the above copyright
17 17 * notice, this list of conditions and the following disclaimer in the
18 18 * documentation and/or other materials provided with the distribution.
19 19 * 4. Neither the name of the University nor the names of its contributors
20 20 * may be used to endorse or promote products derived from this software
21 21 * without specific prior written permission.
22 22 *
23 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 33 * SUCH DAMAGE.
34 34 */
35 35
36 36 #include "lint.h"
37 37 #include <sys/types.h>
38 38 #include <sys/stat.h>
39 39 #include <errno.h>
40 40 #include <limits.h>
41 41 #include <locale.h>
42 42 #include <stdlib.h>
43 43 #include <string.h>
44 44 #include <unistd.h>
45 45 #include <stdio.h>
46 46 #include "collate.h"
47 47 #include "lnumeric.h" /* for struct lc_numeric */
48 48 #include "lctype.h" /* for struct lc_ctype */
49 49 #include "setlocale.h"
50 50 #include "../i18n/_loc_path.h"
51 51 #include "localeimpl.h"
52 52 #include "../i18n/_locale.h"
53 53
54 54 /*
55 55 * Path to locale storage directory. See ../i18n/_loc_path.h
56 56 */
57 57 char *_PathLocale = _DFLT_LOC_PATH;
58 58
59 59 static char *current_locale(locale_t, int);
60 60 static void install_legacy(locale_t, int);
61 61
62 62 char *
63 63 setlocale(int category, const char *locname)
64 64 {
↓ open down ↓ |
64 lines elided |
↑ open up ↑ |
65 65 locale_t oldloc;
66 66 locale_t newloc;
67 67 int mask;
68 68
69 69 if (category < 0 || category > LC_ALL) {
70 70 errno = EINVAL;
71 71 return (NULL);
72 72 }
73 73
74 74 if (locname == NULL)
75 - return (current_locale(__global_locale, category));
75 + return (current_locale(___global_locale, category));
76 76
77 - if ((oldloc = duplocale(__global_locale)) == NULL)
77 + if ((oldloc = duplocale(___global_locale)) == NULL)
78 78 return (NULL);
79 79
80 80 mask = (category == LC_ALL ? LC_ALL_MASK : (1 << category));
81 81
82 82 newloc = newlocale(mask, locname, oldloc);
83 83 if (newloc == NULL) {
84 84 freelocale(oldloc);
85 85 return (NULL);
86 86 }
87 87
88 - oldloc = __global_locale;
89 - __global_locale = newloc;
88 + oldloc = ___global_locale;
89 + ___global_locale = newloc;
90 90
91 91 install_legacy(newloc, mask);
92 92
93 93 freelocale(oldloc);
94 94 return (current_locale(newloc, category));
95 95 }
96 96
97 97 static char *
98 98 current_locale(locale_t loc, int cat)
99 99 {
100 100 int composite = 0;
101 101 static char buf[LC_ALL * (ENCODING_LEN + 1 + 1)];
102 102
103 103 if (cat != LC_ALL) {
104 104 return (loc->locdata[cat]->l_lname);
105 105 }
106 106
107 107 /* Look to see if any category is different */
108 108 for (int i = 1; i < LC_ALL; ++i) {
109 109 if (strcmp(loc->locdata[0]->l_lname,
110 110 loc->locdata[i]->l_lname) != 0) {
111 111 composite = 1;
112 112 break;
113 113 }
114 114 }
115 115
116 116 if (composite) {
117 117 /*
118 118 * Note ordering of these follows the numeric order,
119 119 * if the order is changed, then setlocale() will need
120 120 * to be changed as well.
121 121 */
122 122 (void) snprintf(buf, sizeof (buf),
123 123 "%s/%s/%s/%s/%s/%s",
124 124 loc->locdata[LC_CTYPE]->l_lname,
125 125 loc->locdata[LC_NUMERIC]->l_lname,
126 126 loc->locdata[LC_TIME]->l_lname,
127 127 loc->locdata[LC_COLLATE]->l_lname,
128 128 loc->locdata[LC_MONETARY]->l_lname,
129 129 loc->locdata[LC_MESSAGES]->l_lname);
130 130 return (buf);
131 131 }
132 132
133 133 return (loc->locdata[0]->l_lname);
134 134 }
135 135
136 136 static void
137 137 install_legacy(locale_t loc, int mask)
138 138 {
139 139 /*
140 140 * Update the legacy fixed variables that may be baked into
141 141 * legacy programs. This is really unfortunate, but we can't
142 142 * solve for them otherwise. Note that such legacy programs
143 143 * are only going to see the global locale settings, and cannot
144 144 * benefit from uselocale().
145 145 */
146 146 if (mask & LC_NUMERIC_MASK) {
147 147 struct lc_numeric *lnum;
148 148 lnum = loc->locdata[LC_NUMERIC]->l_data[0];
149 149 _numeric[0] = *lnum->decimal_point;
150 150 _numeric[1] = *lnum->thousands_sep;
151 151 }
152 152
153 153 if (mask & LC_CTYPE_MASK) {
154 154 struct lc_ctype *lct;
155 155 lct = loc->locdata[LC_CTYPE]->l_data[0];
156 156 for (int i = 0; i < _CACHED_RUNES; i++) {
157 157 /* ctype can only encode the lower 8 bits. */
158 158 __ctype[i+1] = lct->lc_ctype_mask[i] & 0xff;
159 159 __ctype_mask[i] = lct->lc_ctype_mask[i];
160 160 }
161 161
162 162 /* The bottom half is the toupper/lower array */
163 163 for (int i = 0; i < _CACHED_RUNES; i++) {
164 164 int u, l;
165 165 __ctype[258 + i] = i;
166 166 u = lct->lc_trans_upper[i];
167 167 l = lct->lc_trans_lower[i];
168 168 if (u && u != i)
169 169 __ctype[258+i] = u;
170 170 if (l && l != i)
171 171 __ctype[258+i] = l;
172 172
173 173 /* Don't forget these annoyances either! */
174 174 __trans_upper[i] = u;
175 175 __trans_lower[i] = l;
176 176 }
177 177
178 178 /* Maximum mblen, cswidth, weird legacy */
179 179 __ctype[520] = lct->lc_max_mblen;
180 180 }
181 181 }
↓ open down ↓ |
82 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX