/*-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 <string.h>37#include <wchar.h>38#include "un-namespace.h"39#include "libc_private.h"40#include "local.h"41#include "mblocal.h"4243wchar_t *44fgetws_l(wchar_t * __restrict ws, int n, FILE * __restrict fp, locale_t locale)45{46int sret;47wchar_t *wsp, *ret;48size_t nconv;49const char *src;50unsigned char *nl;51FIX_LOCALE(locale);52struct xlocale_ctype *l = XLOCALE_CTYPE(locale);5354FLOCKFILE_CANCELSAFE(fp);55ORIENT(fp, 1);5657if (n <= 0) {58fp->_flags |= __SERR;59errno = EINVAL;60goto error;61}6263wsp = ws;64if (n == 1)65goto ok;6667if (fp->_r <= 0 && __srefill(fp))68/* EOF or ferror */69goto error;7071sret = 0;72do {73src = fp->_p;74nl = memchr(fp->_p, '\n', fp->_r);75nconv = l->__mbsnrtowcs(wsp, &src,76nl != NULL ? (nl - fp->_p + 1) : fp->_r,77n - 1, &fp->_mbstate);78if (nconv == (size_t)-1) {79/* Conversion error */80fp->_flags |= __SERR;81goto error;82}83if (src == NULL) {84/*85* We hit a null byte. Increment the character count,86* since mbsnrtowcs()'s return value doesn't include87* the terminating null, then resume conversion88* after the null.89*/90nconv++;91src = memchr(fp->_p, '\0', fp->_r);92src++;93}94fp->_r -= (unsigned char *)src - fp->_p;95fp->_p = (unsigned char *)src;96n -= nconv;97wsp += nconv;98} while ((wsp == ws || wsp[-1] != L'\n') && n > 1 && (fp->_r > 0 ||99(sret = __srefill(fp)) == 0));100if (sret && !__sfeof(fp))101/* ferror */102goto error;103if (!l->__mbsinit(&fp->_mbstate)) {104/* Incomplete character */105fp->_flags |= __SERR;106errno = EILSEQ;107goto error;108}109if (wsp == ws)110/* EOF */111goto error;112ok:113*wsp = L'\0';114ret = ws;115end:116FUNLOCKFILE_CANCELSAFE();117return (ret);118119error:120ret = NULL;121goto end;122}123124wchar_t *125fgetws(wchar_t * __restrict ws, int n, FILE * __restrict fp)126{127return fgetws_l(ws, n, fp, __get_locale());128}129130131