/*-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 <stdio.h>36#include <stdlib.h>37#include <wchar.h>38#include "un-namespace.h"39#include "libc_private.h"40#include "local.h"41#include "mblocal.h"42#include "xlocale_private.h"4344/*45* MT-safe version.46*/47wint_t48fgetwc_l(FILE *fp, locale_t locale)49{50wint_t r;51FIX_LOCALE(locale);5253FLOCKFILE_CANCELSAFE(fp);54ORIENT(fp, 1);55r = __fgetwc(fp, locale);56FUNLOCKFILE_CANCELSAFE();5758return (r);59}6061wint_t62fgetwc(FILE *fp)63{64return fgetwc_l(fp, __get_locale());65}6667/*68* Internal (non-MPSAFE) version of fgetwc(). This version takes an69* mbstate_t argument specifying the initial conversion state. For70* wide streams, this should always be fp->_mbstate. On return, *nread71* is set to the number of bytes read.72*/73wint_t74__fgetwc_mbs(FILE *fp, mbstate_t *mbs, int *nread, locale_t locale)75{76wchar_t wc;77size_t nconv;78struct xlocale_ctype *l = XLOCALE_CTYPE(locale);7980*nread = 0;81if (fp->_r <= 0 && __srefill(fp))82return (WEOF);83do {84nconv = l->__mbrtowc(&wc, fp->_p, fp->_r, mbs);85if (nconv == (size_t)-1) {86fp->_flags |= __SERR;87return (WEOF);88} else if (nconv == (size_t)-2)89continue;90else if (nconv == 0) {91fp->_p++;92fp->_r--;93(*nread)++;94return (L'\0');95} else {96fp->_p += nconv;97fp->_r -= nconv;98*nread += nconv;99return (wc);100}101} while (__srefill(fp) == 0);102if (__sfeof(fp)) {103fp->_flags |= __SERR;104errno = EILSEQ;105}106return (WEOF);107}108109110