Print this page
locale stuff should use libc safe lmalloc. Other fixes from tests.
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libc/port/locale/lmonetary.c
+++ new/usr/src/lib/libc/port/locale/lmonetary.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) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
5 5 * All rights reserved.
6 6 *
7 7 * Redistribution and use in source and binary forms, with or without
8 8 * modification, are permitted provided that the following conditions
9 9 * are met:
10 10 * 1. Redistributions of source code must retain the above copyright
11 11 * notice, this list of conditions and the following disclaimer.
12 12 * 2. Redistributions in binary form must reproduce the above copyright
13 13 * notice, this list of conditions and the following disclaimer in the
14 14 * documentation and/or other materials provided with the distribution.
15 15 *
16 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
↓ open down ↓ |
25 lines elided |
↑ open up ↑ |
26 26 * SUCH DAMAGE.
27 27 */
28 28
29 29 #include "lint.h"
30 30 #include <limits.h>
31 31 #include <stddef.h>
32 32 #include <stdlib.h>
33 33 #include <stdio.h>
34 34 #include <string.h>
35 35 #include <errno.h>
36 +#include "libc.h"
36 37 #include "ldpart.h"
37 38 #include "lmonetary.h"
38 39 #include "localeimpl.h"
39 40
40 41 extern const char *__fix_locale_grouping_str(const char *);
41 42
42 43 #define LCMONETARY_SIZE_FULL (sizeof (struct lc_monetary) / sizeof (char *))
43 44 #define LCMONETARY_SIZE_MIN \
44 45 (offsetof(struct lc_monetary, int_p_cs_precedes) / sizeof (char *))
45 46
46 47 static char empty[] = "";
47 48 static char numempty[] = { CHAR_MAX, '\0' };
48 49
49 50 struct lc_monetary lc_monetary_posix = {
50 51 empty, /* int_curr_symbol */
51 52 empty, /* currency_symbol */
52 53 empty, /* mon_decimal_point */
53 54 empty, /* mon_thousands_sep */
54 55 numempty, /* mon_grouping */
55 56 empty, /* positive_sign */
56 57 empty, /* negative_sign */
57 58 numempty, /* int_frac_digits */
58 59 numempty, /* frac_digits */
59 60 numempty, /* p_cs_precedes */
60 61 numempty, /* p_sep_by_space */
61 62 numempty, /* n_cs_precedes */
62 63 numempty, /* n_sep_by_space */
63 64 numempty, /* p_sign_posn */
64 65 numempty, /* n_sign_posn */
65 66 numempty, /* int_p_cs_precedes */
66 67 numempty, /* int_n_cs_precedes */
67 68 numempty, /* int_p_sep_by_space */
68 69 numempty, /* int_n_sep_by_space */
69 70 numempty, /* int_p_sign_posn */
70 71 numempty, /* int_n_sign_posn */
71 72 empty /* crncystr */
72 73 };
73 74
74 75 struct locdata __posix_monetary_locdata = {
75 76 .l_lname = "C",
76 77 .l_data = { &lc_monetary_posix }
77 78 };
78 79
79 80 static char
80 81 cnv(const char *str)
81 82 {
82 83 int i = strtol(str, NULL, 10);
83 84
84 85 if (i == -1)
85 86 i = CHAR_MAX;
86 87 return ((char)i);
87 88 }
88 89
89 90 struct locdata *
90 91 __lc_monetary_load(const char *name)
91 92 {
92 93 int ret;
93 94 int clen;
94 95 struct lc_monetary *lmon;
95 96 struct locdata *ldata;
96 97
97 98 if ((ldata = __locdata_alloc(name, sizeof (*lmon))) == NULL) {
98 99 return (NULL);
99 100 }
100 101 lmon = ldata->l_data[0];
101 102
102 103 ret = __part_load_locale(name, (char **)&ldata->l_data[1],
103 104 "LC_MONETARY", LCMONETARY_SIZE_FULL, LCMONETARY_SIZE_MIN,
↓ open down ↓ |
58 lines elided |
↑ open up ↑ |
104 105 (const char **)lmon);
105 106
106 107 if (ret != _LDP_LOADED) {
107 108 __locdata_free(ldata);
108 109 errno = EINVAL;
109 110 return (NULL);
110 111 }
111 112
112 113 /* special storage for currency string */
113 114 clen = strlen(lmon->currency_symbol) + 2;
114 - ldata->l_data[2] = malloc(clen);
115 + ldata->l_data[2] = libc_malloc(clen);
115 116 lmon->crncystr = ldata->l_data[2];
116 - lmon->crncystr[0] = '\0';
117 117
118 118 lmon->mon_grouping = __fix_locale_grouping_str(lmon->mon_grouping);
119 119
120 120 #define M_ASSIGN_CHAR(NAME) \
121 121 (((char *)lmon->NAME)[0] = cnv(lmon->NAME))
122 122
123 123 M_ASSIGN_CHAR(int_frac_digits);
124 124 M_ASSIGN_CHAR(frac_digits);
125 125 M_ASSIGN_CHAR(p_cs_precedes);
126 126 M_ASSIGN_CHAR(p_sep_by_space);
127 127 M_ASSIGN_CHAR(n_cs_precedes);
128 128 M_ASSIGN_CHAR(n_sep_by_space);
129 129 M_ASSIGN_CHAR(p_sign_posn);
130 130 M_ASSIGN_CHAR(n_sign_posn);
131 131
132 132 /*
133 133 * The six additional C99 international monetary formatting
134 134 * parameters default to the national parameters when
135 135 * reading FreeBSD LC_MONETARY data files.
136 136 */
137 137 #define M_ASSIGN_ICHAR(NAME) \
138 138 if (lmon->int_##NAME == NULL) \
139 139 lmon->int_##NAME = lmon->NAME; \
140 140 else \
141 141 M_ASSIGN_CHAR(int_##NAME);
142 142
143 143 M_ASSIGN_ICHAR(p_cs_precedes);
144 144 M_ASSIGN_ICHAR(n_cs_precedes);
145 145 M_ASSIGN_ICHAR(p_sep_by_space);
146 146 M_ASSIGN_ICHAR(n_sep_by_space);
147 147 M_ASSIGN_ICHAR(p_sign_posn);
148 148 M_ASSIGN_ICHAR(n_sign_posn);
149 149
150 150 /*
151 151 * Now calculate the currency string (CRNCYSTR) for nl_langinfo.
152 152 * This is a legacy SUSv2 interface.
153 153 */
154 154 if ((lmon->p_cs_precedes[0] == lmon->n_cs_precedes[0]) &&
155 155 (lmon->currency_symbol[0] != '\0')) {
156 156 char sign = '\0';
157 157 switch (lmon->p_cs_precedes[0]) {
158 158 case 0:
159 159 sign = '-';
160 160 break;
161 161 case 1:
162 162 sign = '+';
163 163 break;
164 164 case CHAR_MAX:
165 165 /*
166 166 * Substitute currency string for radix character.
167 167 * To the best of my knowledge, no locale uses this.
168 168 */
169 169 if (strcmp(lmon->mon_decimal_point,
170 170 lmon->currency_symbol) == 0)
171 171 sign = '.';
172 172 break;
173 173 }
174 174 (void) snprintf(lmon->crncystr, clen, "%c%s", sign,
175 175 lmon->currency_symbol);
176 176 }
177 177
178 178 return (ldata);
179 179 }
↓ open down ↓ |
53 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX