1102697Stjr/*-
2127944Stjr * Copyright (c) 2002-2004 Tim J. Robbins.
3118591Stjr * All rights reserved.
4102697Stjr *
5227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
6227753Stheraven * All rights reserved.
7227753Stheraven * Portions of this software were developed by David Chisnall
8227753Stheraven * under sponsorship from the FreeBSD Foundation.
9227753Stheraven *
10102697Stjr * Redistribution and use in source and binary forms, with or without
11102697Stjr * modification, are permitted provided that the following conditions
12102697Stjr * are met:
13102697Stjr * 1. Redistributions of source code must retain the above copyright
14102697Stjr *    notice, this list of conditions and the following disclaimer.
15102697Stjr * 2. Redistributions in binary form must reproduce the above copyright
16102697Stjr *    notice, this list of conditions and the following disclaimer in the
17102697Stjr *    documentation and/or other materials provided with the distribution.
18102697Stjr *
19118591Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20102697Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21102697Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22118591Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23102697Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24102697Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25102697Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26102697Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27102697Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28102697Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29102697Stjr * SUCH DAMAGE.
30102697Stjr */
31102697Stjr
32102697Stjr#include <sys/cdefs.h>
33102697Stjr__FBSDID("$FreeBSD$");
34102697Stjr
35102697Stjr#include <stdlib.h>
36118591Stjr#include <wchar.h>
37129154Stjr#include "mblocal.h"
38102697Stjr
39102697Stjrint
40227753Stheravenwctomb_l(char *s, wchar_t wchar, locale_t locale)
41102697Stjr{
42127944Stjr	static const mbstate_t initial;
43118591Stjr	size_t rval;
44227753Stheraven	FIX_LOCALE(locale);
45102697Stjr
46127944Stjr	if (s == NULL) {
47106032Stjr		/* No support for state dependent encodings. */
48227753Stheraven		locale->wctomb = initial;
49118591Stjr		return (0);
50127944Stjr	}
51227753Stheraven	if ((rval = XLOCALE_CTYPE(locale)->__wcrtomb(s, wchar, &locale->wctomb)) == (size_t)-1)
52102879Stjr		return (-1);
53118591Stjr	return ((int)rval);
54102697Stjr}
55227753Stheravenint
56227753Stheravenwctomb(char *s, wchar_t wchar)
57227753Stheraven{
58227753Stheraven	return wctomb_l(s, wchar, __get_locale());
59227753Stheraven}
60