Initial import from FreeBSD RELENG_4:
[games.git] / lib / libcr / locale / ansi.c
1 /*-
2  * Copyright (c) 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Paul Borman at Krystal Technologies.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)ansi.c      8.1 (Berkeley) 6/27/93";
39 #endif /* LIBC_SCCS and not lint */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD: src/lib/libc/locale/ansi.c,v 1.3.6.1 2003/04/05 08:28:24 tjr Exp $");
43
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <limits.h>
47 #include <stddef.h>
48 #include <rune.h>
49
50 int
51 mblen(s, n)
52         const char *s;
53         size_t n;
54 {
55         char const *e;
56
57         if (s == 0 || *s == 0)
58                 return (0);     /* No support for state dependent encodings. */
59
60         if (sgetrune(s, n, &e) == _INVALID_RUNE)
61                 return (s - e);
62         return (e - s);
63 }
64
65 int
66 mbtowc(pwc, s, n)
67         wchar_t *pwc;
68         const char *s;
69         size_t n;
70 {
71         char const *e;
72         rune_t r;
73
74         if (s == 0 || *s == 0)
75                 return (0);     /* No support for state dependent encodings. */
76
77         if ((r = sgetrune(s, n, &e)) == _INVALID_RUNE)
78                 return (s - e);
79         if (pwc)
80                 *pwc = r;
81         return (e - s);
82 }
83
84 int
85 wctomb(s, wchar)
86         char *s;
87         wchar_t wchar;
88 {
89         char *e;
90
91         if (s == 0)
92                 return (0);     /* No support for state dependent encodings. */
93
94         if (wchar == 0) {
95                 *s = 0;
96                 return (1);
97         }
98
99         sputrune(wchar, s, MB_CUR_MAX, &e);
100         return (e ? e - s : -1);
101 }
102
103 size_t
104 mbstowcs(pwcs, s, n)
105         wchar_t *pwcs;
106         const char *s;
107         size_t n;
108 {
109         const char *e;
110         int cnt;
111         rune_t r;
112
113         if (s == NULL) {
114                 errno = EINVAL;
115                 return (-1);
116         }
117
118         if (pwcs == NULL) {
119                 /* Convert and count only, do not store. */
120                 cnt = 0;
121                 while ((r = sgetrune(s, MB_LEN_MAX, &e)) != _INVALID_RUNE &&
122                     r != 0) {
123                         s = e;
124                         cnt++;
125                 }
126                 if (r == _INVALID_RUNE) {
127                         errno = EILSEQ;
128                         return (-1);
129                 }
130                 return (cnt);
131         }
132
133         /* Convert, store and count characters. */
134         cnt = 0;
135         while (n-- > 0) {
136                 *pwcs = sgetrune(s, MB_LEN_MAX, &e);
137                 if (*pwcs == _INVALID_RUNE) {
138                         errno = EILSEQ;
139                         return (-1);
140                 }
141                 if (*pwcs++ == L'\0')
142                         break;
143                 s = e;
144                 ++cnt;
145         }
146
147         return (cnt);
148 }
149
150 size_t
151 wcstombs(s, pwcs, n)
152         char *s;
153         const wchar_t *pwcs;
154         size_t n;
155 {
156         char buf[MB_LEN_MAX];
157         char *e;
158         int cnt, nb;
159
160         if (pwcs == NULL || n > INT_MAX) {
161                 errno = EINVAL;
162                 return (-1);
163         }
164
165         cnt = 0;
166
167         if (s == NULL) {
168                 /* Convert and count only, do not store. */
169                 while (*pwcs != L'\0') {
170                         if (sputrune(*pwcs++, buf, MB_LEN_MAX, &e) == 0) {
171                                 errno = EILSEQ;
172                                 return (-1);
173                         }
174                         cnt += e - buf;
175                 }
176                 return (cnt);
177         }
178
179         /* Convert, store and count characters. */
180         nb = n;
181         while (nb > 0) {
182                 if (*pwcs == L'\0') {
183                         *s = '\0';
184                         break;
185                 }
186                 if (sputrune(*pwcs++, s, nb, &e) == 0) {
187                         errno = EILSEQ;
188                         return (-1);
189                 }
190                 if (e == NULL)          /* too long */
191                         return (cnt);
192                 cnt += e - s;
193                 nb -= e - s;
194                 s = e;
195         }
196
197         return (cnt);
198 }