segmentless smp fixes
[minix3.git] / lib / libc / locale / multibyte_sb.c
blob53b3fc41765ad0b67cdd7d8111e5e3b2de71039f
1 /* $NetBSD: multibyte_sb.c,v 1.5 2004/07/21 20:27:46 tshiozak Exp $ */
3 /*
4 * Copyright (c) 1991 The Regents of the University of California.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char *sccsid = "from: @(#)multibyte.c 5.1 (Berkeley) 2/18/91";
36 #else
37 __RCSID("$NetBSD: multibyte_sb.c,v 1.5 2004/07/21 20:27:46 tshiozak Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
41 #include <assert.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <wchar.h>
47 * Stub multibyte character functions.
48 * This cheezy implementation is fixed to the native single-byte
49 * character set.
52 /*ARGSUSED*/
53 int
54 mbsinit(ps)
55 const mbstate_t *ps;
58 return 1;
61 /*ARGSUSED*/
62 size_t
63 mbrlen(s, n, ps)
64 const char *s;
65 size_t n;
66 mbstate_t *ps;
69 /* ps appears to be unused */
71 if (s == NULL || *s == '\0')
72 return 0;
73 if (n == 0)
74 return (size_t)-1;
75 return 1;
78 int
79 mblen(s, n)
80 const char *s;
81 size_t n;
84 /* s may be NULL */
86 return mbrlen(s, n, NULL);
89 /*ARGSUSED*/
90 size_t
91 mbrtowc(pwc, s, n, ps)
92 wchar_t *pwc;
93 const char *s;
94 size_t n;
95 mbstate_t *ps;
98 /* pwc may be NULL */
99 /* s may be NULL */
100 /* ps appears to be unused */
102 if (s == NULL)
103 return 0;
104 if (n == 0)
105 return (size_t)-1;
106 if (pwc)
107 *pwc = (wchar_t) *s;
108 return (*s != '\0');
112 mbtowc(pwc, s, n)
113 wchar_t *pwc;
114 const char *s;
115 size_t n;
118 /* pwc may be NULL */
119 /* s may be NULL */
121 return mbrtowc(pwc, s, n, NULL);
124 /*ARGSUSED*/
125 size_t
126 wcrtomb(s, wchar, ps)
127 char *s;
128 wchar_t wchar;
129 mbstate_t *ps;
132 /* s may be NULL */
133 /* ps appears to be unused */
135 if (s == NULL)
136 return 0;
138 *s = (char) wchar;
139 return 1;
143 wctomb(s, wchar)
144 char *s;
145 wchar_t wchar;
148 /* s may be NULL */
150 return wcrtomb(s, wchar, NULL);
153 /*ARGSUSED*/
154 size_t
155 mbsrtowcs(pwcs, s, n, ps)
156 wchar_t *pwcs;
157 const char **s;
158 size_t n;
159 mbstate_t *ps;
161 int count = 0;
163 /* pwcs may be NULL */
164 /* s may be NULL */
165 /* ps appears to be unused */
167 if (!s || !*s)
168 return 0;
170 if (n != 0) {
171 if (pwcs != NULL) {
172 do {
173 if ((*pwcs++ = (wchar_t) *(*s)++) == 0)
174 break;
175 count++;
176 } while (--n != 0);
177 } else {
178 do {
179 if (((wchar_t)*(*s)++) == 0)
180 break;
181 count++;
182 } while (--n != 0);
186 return count;
189 size_t
190 mbstowcs(pwcs, s, n)
191 wchar_t *pwcs;
192 const char *s;
193 size_t n;
196 /* pwcs may be NULL */
197 /* s may be NULL */
199 return mbsrtowcs(pwcs, &s, n, NULL);
202 /*ARGSUSED*/
203 size_t
204 wcsrtombs(s, pwcs, n, ps)
205 char *s;
206 const wchar_t **pwcs;
207 size_t n;
208 mbstate_t *ps;
210 int count = 0;
212 /* s may be NULL */
213 /* pwcs may be NULL */
214 /* ps appears to be unused */
216 if (pwcs == NULL || *pwcs == NULL)
217 return (0);
219 if (s == NULL) {
220 while (*(*pwcs)++ != 0)
221 count++;
222 return(count);
225 if (n != 0) {
226 do {
227 if ((*s++ = (char) *(*pwcs)++) == 0)
228 break;
229 count++;
230 } while (--n != 0);
233 return count;
236 size_t
237 wcstombs(s, pwcs, n)
238 char *s;
239 const wchar_t *pwcs;
240 size_t n;
243 /* s may be NULL */
244 /* pwcs may be NULL */
246 return wcsrtombs(s, &pwcs, n, NULL);
249 wint_t
250 btowc(c)
251 int c;
253 if (c == EOF || c & ~0xFF)
254 return WEOF;
255 return (wint_t)c;
259 wctob(c)
260 wint_t c;
262 if (c == WEOF || c & ~0xFF)
263 return EOF;
264 return (int)c;