/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2002-2004 Tim J. Robbins.4* All rights reserved.5*6* Copyright (c) 2011 The FreeBSD Foundation7*8* Portions of this software were developed by David Chisnall9* under sponsorship from the FreeBSD Foundation.10*11* Redistribution and use in source and binary forms, with or without12* modification, are permitted provided that the following conditions13* are met:14* 1. Redistributions of source code must retain the above copyright15* notice, this list of conditions and the following disclaimer.16* 2. Redistributions in binary form must reproduce the above copyright17* notice, this list of conditions and the following disclaimer in the18* documentation and/or other materials provided with the distribution.19*20* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "namespace.h"34#include <errno.h>35#include <limits.h>36#include <stdio.h>37#include <stdlib.h>38#include <wchar.h>39#include "un-namespace.h"40#include "libc_private.h"41#include "local.h"42#include "mblocal.h"4344/*45* Non-MT-safe version.46*/47wint_t48__fputwc(wchar_t wc, FILE *fp, locale_t locale)49{50char buf[MB_LEN_MAX];51size_t i, len;52struct xlocale_ctype *l = XLOCALE_CTYPE(locale);5354if ((len = l->__wcrtomb(buf, wc, &fp->_mbstate)) == (size_t)-1) {55fp->_flags |= __SERR;56return (WEOF);57}5859for (i = 0; i < len; i++)60if (__sputc((unsigned char)buf[i], fp) == EOF)61return (WEOF);6263return ((wint_t)wc);64}6566/*67* MT-safe version.68*/69wint_t70fputwc_l(wchar_t wc, FILE *fp, locale_t locale)71{72wint_t r;73FIX_LOCALE(locale);7475FLOCKFILE_CANCELSAFE(fp);76ORIENT(fp, 1);77r = __fputwc(wc, fp, locale);78FUNLOCKFILE_CANCELSAFE();7980return (r);81}82wint_t83fputwc(wchar_t wc, FILE *fp)84{85return fputwc_l(wc, fp, __get_locale());86}878889