/*-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 <stdio.h>35#include <wchar.h>36#include "un-namespace.h"37#include "libc_private.h"38#include "local.h"39#include "xlocale_private.h"4041wchar_t *fgetwln_l(FILE * __restrict, size_t *, locale_t);4243wchar_t *44fgetwln_l(FILE * __restrict fp, size_t *lenp, locale_t locale)45{46wchar_t *ret;47wint_t wc;48size_t len;49int savserr;5051FIX_LOCALE(locale);5253FLOCKFILE_CANCELSAFE(fp);54ORIENT(fp, 1);5556savserr = fp->_flags & __SERR;57fp->_flags &= ~__SERR;5859len = 0;60while ((wc = __fgetwc(fp, locale)) != WEOF) {61#define GROW 51262if (len * sizeof(wchar_t) >= fp->_lb._size &&63__slbexpand(fp, (len + GROW) * sizeof(wchar_t))) {64fp->_flags |= __SERR;65goto error;66}67*((wchar_t *)fp->_lb._base + len++) = wc;68if (wc == L'\n')69break;70}71/* fgetwc(3) may set both __SEOF and __SERR at once. */72if (__sferror(fp))73goto error;7475fp->_flags |= savserr;76if (len == 0)77goto error;7879*lenp = len;80ret = (wchar_t *)fp->_lb._base;81end:82FUNLOCKFILE_CANCELSAFE();83return (ret);8485error:86*lenp = 0;87ret = NULL;88goto end;89}9091wchar_t *92fgetwln(FILE * __restrict fp, size_t *lenp)93{94return fgetwln_l(fp, lenp, __get_locale());95}969798