xref: /freebsd/lib/libc/locale/mbtowc.c (revision 6155c34adf50d3d025ec0e862bb34746ea8c0a21)
17438fc3aSTim J. Robbins /*-
274f90defSTim J. Robbins  * Copyright (c) 2002-2004 Tim J. Robbins.
3998e1248STim J. Robbins  * All rights reserved.
47438fc3aSTim J. Robbins  *
57438fc3aSTim J. Robbins  * Redistribution and use in source and binary forms, with or without
67438fc3aSTim J. Robbins  * modification, are permitted provided that the following conditions
77438fc3aSTim J. Robbins  * are met:
87438fc3aSTim J. Robbins  * 1. Redistributions of source code must retain the above copyright
97438fc3aSTim J. Robbins  *    notice, this list of conditions and the following disclaimer.
107438fc3aSTim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
117438fc3aSTim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
127438fc3aSTim J. Robbins  *    documentation and/or other materials provided with the distribution.
137438fc3aSTim J. Robbins  *
14998e1248STim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
157438fc3aSTim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167438fc3aSTim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17998e1248STim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
187438fc3aSTim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
197438fc3aSTim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
207438fc3aSTim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
217438fc3aSTim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
227438fc3aSTim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
237438fc3aSTim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
247438fc3aSTim J. Robbins  * SUCH DAMAGE.
257438fc3aSTim J. Robbins  */
267438fc3aSTim J. Robbins 
277438fc3aSTim J. Robbins #include <sys/cdefs.h>
287438fc3aSTim J. Robbins __FBSDID("$FreeBSD$");
297438fc3aSTim J. Robbins 
30f0c6c306STim J. Robbins #include <errno.h>
31998e1248STim J. Robbins #include <limits.h>
327438fc3aSTim J. Robbins #include <stdlib.h>
33998e1248STim J. Robbins #include <string.h>
34998e1248STim J. Robbins #include <wchar.h>
356155c34aSTim J. Robbins #include "mblocal.h"
367438fc3aSTim J. Robbins 
377438fc3aSTim J. Robbins int
38b6f33850STim J. Robbins mbtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n)
397438fc3aSTim J. Robbins {
4074f90defSTim J. Robbins 	static const mbstate_t initial;
4174f90defSTim J. Robbins 	static mbstate_t mbs;
42998e1248STim J. Robbins 	size_t rval;
437438fc3aSTim J. Robbins 
4474f90defSTim J. Robbins 	if (s == NULL) {
45b6f33850STim J. Robbins 		/* No support for state dependent encodings. */
4674f90defSTim J. Robbins 		mbs = initial;
47b6f33850STim J. Robbins 		return (0);
4874f90defSTim J. Robbins 	}
496155c34aSTim J. Robbins 	rval = __mbrtowc(pwc, s, n, &mbs);
50998e1248STim J. Robbins 	if (rval == (size_t)-1 || rval == (size_t)-2)
51998e1248STim J. Robbins 		return (-1);
52998e1248STim J. Robbins 	if (rval > INT_MAX) {
53998e1248STim J. Robbins 		errno = ERANGE;
54c5929b30STim J. Robbins 		return (-1);
55f0c6c306STim J. Robbins 	}
56998e1248STim J. Robbins 	return ((int)rval);
577438fc3aSTim J. Robbins }
58