1*47e23bbeSschwarze /* $OpenBSD: mbtowc.c,v 1.3 2016/02/27 14:02:13 schwarze Exp $ */
2c9b8e388Sstsp
3c9b8e388Sstsp /*-
4c9b8e388Sstsp * Copyright (c) 2002-2004 Tim J. Robbins.
5c9b8e388Sstsp * All rights reserved.
6c9b8e388Sstsp *
7c9b8e388Sstsp * Redistribution and use in source and binary forms, with or without
8c9b8e388Sstsp * modification, are permitted provided that the following conditions
9c9b8e388Sstsp * are met:
10c9b8e388Sstsp * 1. Redistributions of source code must retain the above copyright
11c9b8e388Sstsp * notice, this list of conditions and the following disclaimer.
12c9b8e388Sstsp * 2. Redistributions in binary form must reproduce the above copyright
13c9b8e388Sstsp * notice, this list of conditions and the following disclaimer in the
14c9b8e388Sstsp * documentation and/or other materials provided with the distribution.
15c9b8e388Sstsp *
16c9b8e388Sstsp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17c9b8e388Sstsp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c9b8e388Sstsp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c9b8e388Sstsp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20c9b8e388Sstsp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c9b8e388Sstsp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c9b8e388Sstsp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c9b8e388Sstsp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c9b8e388Sstsp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c9b8e388Sstsp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c9b8e388Sstsp * SUCH DAMAGE.
27c9b8e388Sstsp */
28c9b8e388Sstsp
29c9b8e388Sstsp
30c9b8e388Sstsp #include <stdlib.h>
31c9b8e388Sstsp #include <string.h>
32c9b8e388Sstsp #include <wchar.h>
33c9b8e388Sstsp #include <errno.h>
34c9b8e388Sstsp
35c9b8e388Sstsp int
mbtowc(wchar_t * __restrict pwc,const char * __restrict s,size_t n)36c9b8e388Sstsp mbtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n)
37c9b8e388Sstsp {
38c9b8e388Sstsp static mbstate_t mbs;
39c9b8e388Sstsp size_t rval;
40c9b8e388Sstsp
41c9b8e388Sstsp if (s == NULL) {
42c9b8e388Sstsp /* No support for state dependent encodings. */
43c9b8e388Sstsp memset(&mbs, 0, sizeof(mbs));
44c9b8e388Sstsp return (0);
45c9b8e388Sstsp }
46c9b8e388Sstsp rval = mbrtowc(pwc, s, n, &mbs);
47*47e23bbeSschwarze
48*47e23bbeSschwarze switch (rval) {
49*47e23bbeSschwarze case (size_t)-2:
50*47e23bbeSschwarze errno = EILSEQ;
51*47e23bbeSschwarze /* FALLTHROUGH */
52*47e23bbeSschwarze case (size_t)-1:
53*47e23bbeSschwarze return -1;
54*47e23bbeSschwarze default:
55*47e23bbeSschwarze return (int)rval;
56*47e23bbeSschwarze }
57c9b8e388Sstsp }
58