xref: /openbsd-src/lib/libc/locale/wctomb.c (revision 4a39ccd02c887d988c1a5398dd2142879056da5f)
1*4a39ccd0Sderaadt /*	$OpenBSD: wctomb.c,v 1.2 2012/12/05 23:20:00 deraadt Exp $ */
2c9b8e388Sstsp 
3c9b8e388Sstsp /*-
4c9b8e388Sstsp  * Copyright (c) 2002-2004 Tim J. Robbins.
5c9b8e388Sstsp  * All rights reserved.
6c9b8e388Sstsp  *
7c9b8e388Sstsp  * Redistribution and use in source and binary forms, with or without
8c9b8e388Sstsp  * modification, are permitted provided that the following conditions
9c9b8e388Sstsp  * are met:
10c9b8e388Sstsp  * 1. Redistributions of source code must retain the above copyright
11c9b8e388Sstsp  *    notice, this list of conditions and the following disclaimer.
12c9b8e388Sstsp  * 2. Redistributions in binary form must reproduce the above copyright
13c9b8e388Sstsp  *    notice, this list of conditions and the following disclaimer in the
14c9b8e388Sstsp  *    documentation and/or other materials provided with the distribution.
15c9b8e388Sstsp  *
16c9b8e388Sstsp  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17c9b8e388Sstsp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c9b8e388Sstsp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c9b8e388Sstsp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20c9b8e388Sstsp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c9b8e388Sstsp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c9b8e388Sstsp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c9b8e388Sstsp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c9b8e388Sstsp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c9b8e388Sstsp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c9b8e388Sstsp  * SUCH DAMAGE.
27c9b8e388Sstsp  */
28c9b8e388Sstsp 
29c9b8e388Sstsp #include <stdlib.h>
30c9b8e388Sstsp #include <string.h>
31c9b8e388Sstsp #include <wchar.h>
32c9b8e388Sstsp 
33c9b8e388Sstsp int
wctomb(char * s,wchar_t wchar)34c9b8e388Sstsp wctomb(char *s, wchar_t wchar)
35c9b8e388Sstsp {
36c9b8e388Sstsp 	static mbstate_t mbs;
37c9b8e388Sstsp 	size_t rval;
38c9b8e388Sstsp 
39c9b8e388Sstsp 	if (s == NULL) {
40c9b8e388Sstsp 		/* No support for state dependent encodings. */
41c9b8e388Sstsp 		memset(&mbs, 0, sizeof(mbs));
42c9b8e388Sstsp 		return (0);
43c9b8e388Sstsp 	}
44c9b8e388Sstsp 	if ((rval = wcrtomb(s, wchar, &mbs)) == (size_t)-1)
45c9b8e388Sstsp 		return (-1);
46c9b8e388Sstsp 	return ((int)rval);
47c9b8e388Sstsp }
48