xref: /freebsd/lib/libc/locale/mbtowc.c (revision 3c87aa1d3dc1d8dad3efad322852a8e1e76dee55)
17438fc3aSTim J. Robbins /*-
274f90defSTim J. Robbins  * Copyright (c) 2002-2004 Tim J. Robbins.
3998e1248STim J. Robbins  * All rights reserved.
47438fc3aSTim J. Robbins  *
5*3c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
6*3c87aa1dSDavid Chisnall  * All rights reserved.
7*3c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
8*3c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
9*3c87aa1dSDavid Chisnall  *
107438fc3aSTim J. Robbins  * Redistribution and use in source and binary forms, with or without
117438fc3aSTim J. Robbins  * modification, are permitted provided that the following conditions
127438fc3aSTim J. Robbins  * are met:
137438fc3aSTim J. Robbins  * 1. Redistributions of source code must retain the above copyright
147438fc3aSTim J. Robbins  *    notice, this list of conditions and the following disclaimer.
157438fc3aSTim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
167438fc3aSTim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
177438fc3aSTim J. Robbins  *    documentation and/or other materials provided with the distribution.
187438fc3aSTim J. Robbins  *
19998e1248STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
207438fc3aSTim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
217438fc3aSTim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22998e1248STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
237438fc3aSTim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
247438fc3aSTim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
257438fc3aSTim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
267438fc3aSTim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
277438fc3aSTim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
287438fc3aSTim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
297438fc3aSTim J. Robbins  * SUCH DAMAGE.
307438fc3aSTim J. Robbins  */
317438fc3aSTim J. Robbins 
327438fc3aSTim J. Robbins #include <sys/cdefs.h>
337438fc3aSTim J. Robbins __FBSDID("$FreeBSD$");
347438fc3aSTim J. Robbins 
357438fc3aSTim J. Robbins #include <stdlib.h>
36998e1248STim J. Robbins #include <wchar.h>
376155c34aSTim J. Robbins #include "mblocal.h"
387438fc3aSTim J. Robbins 
397438fc3aSTim J. Robbins int
40*3c87aa1dSDavid Chisnall mbtowc_l(wchar_t * __restrict pwc, const char * __restrict s, size_t n, locale_t locale)
417438fc3aSTim J. Robbins {
4274f90defSTim J. Robbins 	static const mbstate_t initial;
43998e1248STim J. Robbins 	size_t rval;
44*3c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
457438fc3aSTim J. Robbins 
4674f90defSTim J. Robbins 	if (s == NULL) {
47b6f33850STim J. Robbins 		/* No support for state dependent encodings. */
48*3c87aa1dSDavid Chisnall 		locale->mbtowc = initial;
49b6f33850STim J. Robbins 		return (0);
5074f90defSTim J. Robbins 	}
51*3c87aa1dSDavid Chisnall 	rval = XLOCALE_CTYPE(locale)->__mbrtowc(pwc, s, n, &locale->mbtowc);
52998e1248STim J. Robbins 	if (rval == (size_t)-1 || rval == (size_t)-2)
53998e1248STim J. Robbins 		return (-1);
54998e1248STim J. Robbins 	return ((int)rval);
557438fc3aSTim J. Robbins }
56*3c87aa1dSDavid Chisnall int
57*3c87aa1dSDavid Chisnall mbtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n)
58*3c87aa1dSDavid Chisnall {
59*3c87aa1dSDavid Chisnall 	return mbtowc_l(pwc, s, n, __get_locale());
60*3c87aa1dSDavid Chisnall }
61