1102697Stjr/*-
2127944Stjr * Copyright (c) 2002-2004 Tim J. Robbins.
3118591Stjr * All rights reserved.
4102697Stjr *
5227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
6227753Stheraven * All rights reserved.
7227753Stheraven * Portions of this software were developed by David Chisnall
8227753Stheraven * under sponsorship from the FreeBSD Foundation.
9227753Stheraven *
10102697Stjr * Redistribution and use in source and binary forms, with or without
11102697Stjr * modification, are permitted provided that the following conditions
12102697Stjr * are met:
13102697Stjr * 1. Redistributions of source code must retain the above copyright
14102697Stjr *    notice, this list of conditions and the following disclaimer.
15102697Stjr * 2. Redistributions in binary form must reproduce the above copyright
16102697Stjr *    notice, this list of conditions and the following disclaimer in the
17102697Stjr *    documentation and/or other materials provided with the distribution.
18102697Stjr *
19118591Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20102697Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21102697Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22118591Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23102697Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24102697Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25102697Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26102697Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27102697Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28102697Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29102697Stjr * SUCH DAMAGE.
30102697Stjr */
31102697Stjr
32102697Stjr#include <sys/cdefs.h>
33102697Stjr__FBSDID("$FreeBSD: releng/10.3/lib/libc/locale/mbtowc.c 227753 2011-11-20 14:45:42Z theraven $");
34102697Stjr
35102697Stjr#include <stdlib.h>
36118591Stjr#include <wchar.h>
37129154Stjr#include "mblocal.h"
38102697Stjr
39102697Stjrint
40227753Stheravenmbtowc_l(wchar_t * __restrict pwc, const char * __restrict s, size_t n, locale_t locale)
41102697Stjr{
42127944Stjr	static const mbstate_t initial;
43118591Stjr	size_t rval;
44227753Stheraven	FIX_LOCALE(locale);
45102697Stjr
46127944Stjr	if (s == NULL) {
47106032Stjr		/* No support for state dependent encodings. */
48227753Stheraven		locale->mbtowc = initial;
49118591Stjr		return (0);
50127944Stjr	}
51227753Stheraven	rval = XLOCALE_CTYPE(locale)->__mbrtowc(pwc, s, n, &locale->mbtowc);
52118591Stjr	if (rval == (size_t)-1 || rval == (size_t)-2)
53106077Stjr		return (-1);
54118591Stjr	return ((int)rval);
55102697Stjr}
56227753Stheravenint
57227753Stheravenmbtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n)
58227753Stheraven{
59227753Stheraven	return mbtowc_l(pwc, s, n, __get_locale());
60227753Stheraven}
61