1101267Stjr/*-
2118589Stjr * Copyright (c) 2002, 2003 Tim J. Robbins.
3101267Stjr * All rights reserved.
4101267Stjr *
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 *
10101267Stjr * Redistribution and use in source and binary forms, with or without
11101267Stjr * modification, are permitted provided that the following conditions
12101267Stjr * are met:
13101267Stjr * 1. Redistributions of source code must retain the above copyright
14101267Stjr *    notice, this list of conditions and the following disclaimer.
15101267Stjr * 2. Redistributions in binary form must reproduce the above copyright
16101267Stjr *    notice, this list of conditions and the following disclaimer in the
17101267Stjr *    documentation and/or other materials provided with the distribution.
18101267Stjr *
19101267Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20101267Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21101267Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22101267Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23101267Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24101267Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25101267Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26101267Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27101267Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28101267Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29101267Stjr * SUCH DAMAGE.
30101267Stjr */
31101267Stjr
32101267Stjr#include <sys/cdefs.h>
33101267Stjr__FBSDID("$FreeBSD$");
34101267Stjr
35118589Stjr#include <stdio.h>
36101267Stjr#include <wchar.h>
37129154Stjr#include "mblocal.h"
38101267Stjr
39101267Stjrwint_t
40227753Stheravenbtowc_l(int c, locale_t l)
41101267Stjr{
42127944Stjr	static const mbstate_t initial;
43127944Stjr	mbstate_t mbs = initial;
44101267Stjr	char cc;
45118589Stjr	wchar_t wc;
46227753Stheraven	FIX_LOCALE(l);
47101267Stjr
48101267Stjr	if (c == EOF)
49101267Stjr		return (WEOF);
50118589Stjr	/*
51118589Stjr	 * We expect mbrtowc() to return 0 or 1, hence the check for n > 1
52118589Stjr	 * which detects error return values as well as "impossible" byte
53118589Stjr	 * counts.
54118589Stjr	 */
55127944Stjr	cc = (char)c;
56227753Stheraven	if (XLOCALE_CTYPE(l)->__mbrtowc(&wc, &cc, 1, &mbs) > 1)
57101267Stjr		return (WEOF);
58118589Stjr	return (wc);
59101267Stjr}
60227753Stheravenwint_t
61227753Stheravenbtowc(int c)
62227753Stheraven{
63227753Stheraven	return btowc_l(c, __get_locale());
64227753Stheraven}
65