13d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes/*	$OpenBSD: wctob.c,v 1.2 2012/12/05 23:20:00 deraadt Exp $ */
23d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes/*-
33d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * Copyright (c) 2002-2004 Tim J. Robbins.
43d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * All rights reserved.
53d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes *
63d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * Redistribution and use in source and binary forms, with or without
73d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * modification, are permitted provided that the following conditions
83d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * are met:
93d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * 1. Redistributions of source code must retain the above copyright
103d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes *    notice, this list of conditions and the following disclaimer.
113d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
123d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes *    notice, this list of conditions and the following disclaimer in the
133d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes *    documentation and/or other materials provided with the distribution.
143d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes *
153d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
163d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
183d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
193d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
203d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
213d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
223d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
233d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
243d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
253d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes * SUCH DAMAGE.
263d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes */
273d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes
283d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes#include <limits.h>
293d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes#include <stdio.h>
303d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes#include <string.h>
313d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes#include <wchar.h>
323d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes
333d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughesint
343d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hugheswctob(wint_t c)
353d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes{
363d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes	mbstate_t mbs;
373d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes	char buf[MB_LEN_MAX];
383d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes
393d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes	memset(&mbs, 0, sizeof(mbs));
403d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes	if (c == WEOF || wcrtomb(buf, c, &mbs) != 1)
413d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes		return (EOF);
423d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes	return ((unsigned char)*buf);
433d7a0d9b08ecab054b2dff8332507644de3690b0Elliott Hughes}
44