wctob.c revision 129154
1101267Stjr/*-
2127944Stjr * Copyright (c) 2002-2004 Tim J. Robbins.
3101267Stjr * All rights reserved.
4101267Stjr *
5101267Stjr * Redistribution and use in source and binary forms, with or without
6101267Stjr * modification, are permitted provided that the following conditions
7101267Stjr * are met:
8101267Stjr * 1. Redistributions of source code must retain the above copyright
9101267Stjr *    notice, this list of conditions and the following disclaimer.
10101267Stjr * 2. Redistributions in binary form must reproduce the above copyright
11101267Stjr *    notice, this list of conditions and the following disclaimer in the
12101267Stjr *    documentation and/or other materials provided with the distribution.
13101267Stjr *
14101267Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15101267Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16101267Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17101267Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18101267Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19101267Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20101267Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21101267Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22101267Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23101267Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24101267Stjr * SUCH DAMAGE.
25101267Stjr */
26101267Stjr
27101267Stjr#include <sys/cdefs.h>
28101267Stjr__FBSDID("$FreeBSD: head/lib/libc/locale/wctob.c 129154 2004-05-12 14:26:54Z tjr $");
29101267Stjr
30118589Stjr#include <limits.h>
31118589Stjr#include <stdio.h>
32101267Stjr#include <wchar.h>
33129154Stjr#include "mblocal.h"
34101267Stjr
35101267Stjrint
36101267Stjrwctob(wint_t c)
37101267Stjr{
38127944Stjr	static const mbstate_t initial;
39127944Stjr	mbstate_t mbs = initial;
40118589Stjr	char buf[MB_LEN_MAX];
41101267Stjr
42129154Stjr	if (c == WEOF || __wcrtomb(buf, c, &mbs) != 1)
43101267Stjr		return (EOF);
44118589Stjr	return ((unsigned char)*buf);
45101267Stjr}
46