1101776Stjr/*- 2129583Stjr * Copyright (c) 2002-2004 Tim J. Robbins. 3101776Stjr * All rights reserved. 4101776Stjr * 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 * 10101776Stjr * Redistribution and use in source and binary forms, with or without 11101776Stjr * modification, are permitted provided that the following conditions 12101776Stjr * are met: 13101776Stjr * 1. Redistributions of source code must retain the above copyright 14101776Stjr * notice, this list of conditions and the following disclaimer. 15101776Stjr * 2. Redistributions in binary form must reproduce the above copyright 16101776Stjr * notice, this list of conditions and the following disclaimer in the 17101776Stjr * documentation and/or other materials provided with the distribution. 18101776Stjr * 19101776Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20101776Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21101776Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22101776Stjr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23101776Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24101776Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25101776Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26101776Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27101776Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28101776Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29101776Stjr * SUCH DAMAGE. 30101776Stjr */ 31101776Stjr 32101776Stjr#include <sys/cdefs.h> 33101776Stjr__FBSDID("$FreeBSD$"); 34101776Stjr 35101776Stjr#include "namespace.h" 36101776Stjr#include <errno.h> 37103539Stjr#include <limits.h> 38101776Stjr#include <stdio.h> 39103523Stjr#include <stdlib.h> 40101776Stjr#include <wchar.h> 41101776Stjr#include "un-namespace.h" 42101776Stjr#include "libc_private.h" 43101776Stjr#include "local.h" 44132442Stjr#include "mblocal.h" 45101776Stjr 46103676Stjr/* 47103676Stjr * Non-MT-safe version. 48103676Stjr */ 49101776Stjrwint_t 50227753Stheraven__fputwc(wchar_t wc, FILE *fp, locale_t locale) 51101776Stjr{ 52103523Stjr char buf[MB_LEN_MAX]; 53103523Stjr size_t i, len; 54227753Stheraven struct xlocale_ctype *l = XLOCALE_CTYPE(locale); 55101776Stjr 56121851Stjr if (MB_CUR_MAX == 1 && wc > 0 && wc <= UCHAR_MAX) { 57103539Stjr /* 58103539Stjr * Assume single-byte locale with no special encoding. 59103539Stjr * A more careful test would be to check 60103539Stjr * _CurrentRuneLocale->encoding. 61103539Stjr */ 62103539Stjr *buf = (unsigned char)wc; 63103539Stjr len = 1; 64103539Stjr } else { 65227753Stheraven if ((len = l->__wcrtomb(buf, wc, &fp->_mbstate)) == (size_t)-1) { 66105234Stjr fp->_flags |= __SERR; 67103539Stjr return (WEOF); 68105234Stjr } 69103539Stjr } 70103523Stjr 71103523Stjr for (i = 0; i < len; i++) 72103676Stjr if (__sputc((unsigned char)buf[i], fp) == EOF) 73103523Stjr return (WEOF); 74103523Stjr 75103676Stjr return ((wint_t)wc); 76103676Stjr} 77103676Stjr 78103676Stjr/* 79103676Stjr * MT-safe version. 80103676Stjr */ 81103676Stjrwint_t 82227753Stheravenfputwc_l(wchar_t wc, FILE *fp, locale_t locale) 83103676Stjr{ 84103676Stjr wint_t r; 85227753Stheraven FIX_LOCALE(locale); 86103676Stjr 87103676Stjr FLOCKFILE(fp); 88103676Stjr ORIENT(fp, 1); 89227753Stheraven r = __fputwc(wc, fp, locale); 90103539Stjr FUNLOCKFILE(fp); 91103539Stjr 92103676Stjr return (r); 93101776Stjr} 94227753Stheravenwint_t 95227753Stheravenfputwc(wchar_t wc, FILE *fp) 96227753Stheraven{ 97227753Stheraven return fputwc_l(wc, fp, __get_locale()); 98227753Stheraven} 99