xref: /freebsd/lib/libc/locale/mbtowc.c (revision b6f33850e0b8ee9cc071000b1ed108a9144a8609)
17438fc3aSTim J. Robbins /*-
27438fc3aSTim J. Robbins  * Copyright (c) 1993
37438fc3aSTim J. Robbins  *	The Regents of the University of California.  All rights reserved.
47438fc3aSTim J. Robbins  *
57438fc3aSTim J. Robbins  * This code is derived from software contributed to Berkeley by
67438fc3aSTim J. Robbins  * Paul Borman at Krystal Technologies.
77438fc3aSTim J. Robbins  *
87438fc3aSTim J. Robbins  * Redistribution and use in source and binary forms, with or without
97438fc3aSTim J. Robbins  * modification, are permitted provided that the following conditions
107438fc3aSTim J. Robbins  * are met:
117438fc3aSTim J. Robbins  * 1. Redistributions of source code must retain the above copyright
127438fc3aSTim J. Robbins  *    notice, this list of conditions and the following disclaimer.
137438fc3aSTim J. Robbins  * 2. Redistributions in binary form must reproduce the above copyright
147438fc3aSTim J. Robbins  *    notice, this list of conditions and the following disclaimer in the
157438fc3aSTim J. Robbins  *    documentation and/or other materials provided with the distribution.
167438fc3aSTim J. Robbins  * 3. All advertising materials mentioning features or use of this software
177438fc3aSTim J. Robbins  *    must display the following acknowledgement:
187438fc3aSTim J. Robbins  *	This product includes software developed by the University of
197438fc3aSTim J. Robbins  *	California, Berkeley and its contributors.
207438fc3aSTim J. Robbins  * 4. Neither the name of the University nor the names of its contributors
217438fc3aSTim J. Robbins  *    may be used to endorse or promote products derived from this software
227438fc3aSTim J. Robbins  *    without specific prior written permission.
237438fc3aSTim J. Robbins  *
247438fc3aSTim J. Robbins  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
257438fc3aSTim J. Robbins  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
267438fc3aSTim J. Robbins  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
277438fc3aSTim J. Robbins  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
287438fc3aSTim J. Robbins  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
297438fc3aSTim J. Robbins  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
307438fc3aSTim J. Robbins  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
317438fc3aSTim J. Robbins  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
327438fc3aSTim J. Robbins  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
337438fc3aSTim J. Robbins  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
347438fc3aSTim J. Robbins  * SUCH DAMAGE.
357438fc3aSTim J. Robbins  */
367438fc3aSTim J. Robbins 
377438fc3aSTim J. Robbins #include <sys/cdefs.h>
387438fc3aSTim J. Robbins __FBSDID("$FreeBSD$");
397438fc3aSTim J. Robbins 
40f0c6c306STim J. Robbins #include <errno.h>
417438fc3aSTim J. Robbins #include <stdlib.h>
427438fc3aSTim J. Robbins #include <stddef.h>
437438fc3aSTim J. Robbins #include <rune.h>
447438fc3aSTim J. Robbins 
457438fc3aSTim J. Robbins int
46b6f33850STim J. Robbins mbtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n)
477438fc3aSTim J. Robbins {
48b6f33850STim J. Robbins 	const char *e;
497438fc3aSTim J. Robbins 	rune_t r;
507438fc3aSTim J. Robbins 
51b6f33850STim J. Robbins 	if (s == NULL || *s == '\0')
52b6f33850STim J. Robbins 		/* No support for state dependent encodings. */
53b6f33850STim J. Robbins 		return (0);
547438fc3aSTim J. Robbins 
55f0c6c306STim J. Robbins 	if ((r = sgetrune(s, n, &e)) == _INVALID_RUNE) {
56f0c6c306STim J. Robbins 		errno = EILSEQ;
577438fc3aSTim J. Robbins 		return (s - e);
58f0c6c306STim J. Robbins 	}
59b6f33850STim J. Robbins 	if (pwc != NULL)
607438fc3aSTim J. Robbins 		*pwc = r;
617438fc3aSTim J. Robbins 	return (e - s);
627438fc3aSTim J. Robbins }
63