mbtowc.c revision 102697
1214571Sdim/*-
2214571Sdim * Copyright (c) 1993
3214571Sdim *	The Regents of the University of California.  All rights reserved.
4214571Sdim *
5214571Sdim * This code is derived from software contributed to Berkeley by
6214571Sdim * Paul Borman at Krystal Technologies.
7214571Sdim *
8214571Sdim * Redistribution and use in source and binary forms, with or without
9214571Sdim * modification, are permitted provided that the following conditions
10214571Sdim * are met:
11214571Sdim * 1. Redistributions of source code must retain the above copyright
12214571Sdim *    notice, this list of conditions and the following disclaimer.
13214571Sdim * 2. Redistributions in binary form must reproduce the above copyright
14214571Sdim *    notice, this list of conditions and the following disclaimer in the
15214571Sdim *    documentation and/or other materials provided with the distribution.
16214571Sdim * 3. All advertising materials mentioning features or use of this software
17214571Sdim *    must display the following acknowledgement:
18214571Sdim *	This product includes software developed by the University of
19214571Sdim *	California, Berkeley and its contributors.
20214571Sdim * 4. Neither the name of the University nor the names of its contributors
21214571Sdim *    may be used to endorse or promote products derived from this software
22214571Sdim *    without specific prior written permission.
23214571Sdim *
24214571Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25214571Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26214571Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27214571Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28214571Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29214571Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30214571Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31214571Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32214571Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33214571Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34214571Sdim * SUCH DAMAGE.
35214571Sdim */
36214571Sdim
37214571Sdim#include <sys/cdefs.h>
38214571Sdim__FBSDID("$FreeBSD: head/lib/libc/locale/mbtowc.c 102697 2002-08-31 11:26:55Z tjr $");
39214571Sdim
40214571Sdim#include <stdlib.h>
41214571Sdim#include <stddef.h>
42214571Sdim#include <rune.h>
43214571Sdim
44214571Sdimint
45214571Sdimmbtowc(pwc, s, n)
46214571Sdim	wchar_t *pwc;
47214571Sdim	const char *s;
48214571Sdim	size_t n;
49214571Sdim{
50214571Sdim	char const *e;
51214571Sdim	rune_t r;
52214571Sdim
53214571Sdim	if (s == 0 || *s == 0)
54214571Sdim		return (0);	/* No support for state dependent encodings. */
55214571Sdim
56214571Sdim	if ((r = sgetrune(s, n, &e)) == _INVALID_RUNE)
57214571Sdim		return (s - e);
58214571Sdim	if (pwc)
59214571Sdim		*pwc = r;
60214571Sdim	return (e - s);
61214571Sdim}
62214571Sdim