xref: /freebsd/lib/libc/locale/mbtowc.c (revision 7438fc3aa8a196e1af28f4b09418a6733af0d69a)
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 
407438fc3aSTim J. Robbins #include <stdlib.h>
417438fc3aSTim J. Robbins #include <stddef.h>
427438fc3aSTim J. Robbins #include <rune.h>
437438fc3aSTim J. Robbins 
447438fc3aSTim J. Robbins int
457438fc3aSTim J. Robbins mbtowc(pwc, s, n)
467438fc3aSTim J. Robbins 	wchar_t *pwc;
477438fc3aSTim J. Robbins 	const char *s;
487438fc3aSTim J. Robbins 	size_t n;
497438fc3aSTim J. Robbins {
507438fc3aSTim J. Robbins 	char const *e;
517438fc3aSTim J. Robbins 	rune_t r;
527438fc3aSTim J. Robbins 
537438fc3aSTim J. Robbins 	if (s == 0 || *s == 0)
547438fc3aSTim J. Robbins 		return (0);	/* No support for state dependent encodings. */
557438fc3aSTim J. Robbins 
567438fc3aSTim J. Robbins 	if ((r = sgetrune(s, n, &e)) == _INVALID_RUNE)
577438fc3aSTim J. Robbins 		return (s - e);
587438fc3aSTim J. Robbins 	if (pwc)
597438fc3aSTim J. Robbins 		*pwc = r;
607438fc3aSTim J. Robbins 	return (e - s);
617438fc3aSTim J. Robbins }
62