xref: /freebsd/lib/libc/locale/mbtowc.c (revision 998e12483784c318c36080a7af9ddd9d46b3766c)
17438fc3aSTim J. Robbins /*-
2998e1248STim J. Robbins  * Copyright (c) 2002, 2003 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>
357438fc3aSTim J. Robbins 
367438fc3aSTim J. Robbins int
37b6f33850STim J. Robbins mbtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n)
387438fc3aSTim J. Robbins {
39998e1248STim J. Robbins 	size_t rval;
407438fc3aSTim J. Robbins 
41c5929b30STim J. Robbins 	if (s == NULL)
42b6f33850STim J. Robbins 		/* No support for state dependent encodings. */
43b6f33850STim J. Robbins 		return (0);
44998e1248STim J. Robbins 	/*
45998e1248STim J. Robbins 	 * We pass NULL as the state pointer to mbrtowc() because we don't
46998e1248STim J. Robbins 	 * support state-dependent encodings and don't want to waste time
47998e1248STim J. Robbins 	 * creating a zeroed mbstate_t that will not be used.
48998e1248STim J. Robbins 	 */
49998e1248STim J. Robbins 	rval = mbrtowc(pwc, s, n, NULL);
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