1132242Stjr/*- 2132242Stjr * Copyright (c) 2002-2004 Tim J. Robbins. 3132242Stjr * All rights reserved. 4132242Stjr * 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 * 10132242Stjr * Redistribution and use in source and binary forms, with or without 11132242Stjr * modification, are permitted provided that the following conditions 12132242Stjr * are met: 13132242Stjr * 1. Redistributions of source code must retain the above copyright 14132242Stjr * notice, this list of conditions and the following disclaimer. 15132242Stjr * 2. Redistributions in binary form must reproduce the above copyright 16132242Stjr * notice, this list of conditions and the following disclaimer in the 17132242Stjr * documentation and/or other materials provided with the distribution. 18132242Stjr * 19132242Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20132242Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21132242Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22132242Stjr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23132242Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24132242Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25132242Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26132242Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27132242Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28132242Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29132242Stjr * SUCH DAMAGE. 30132242Stjr */ 31132242Stjr 32132242Stjr#include <sys/cdefs.h> 33132242Stjr__FBSDID("$FreeBSD: stable/10/lib/libc/stdio/fgetwln.c 321074 2017-07-17 14:09:34Z kib $"); 34132242Stjr 35132242Stjr#include "namespace.h" 36132242Stjr#include <stdio.h> 37132242Stjr#include <wchar.h> 38132242Stjr#include "un-namespace.h" 39132242Stjr#include "libc_private.h" 40132242Stjr#include "local.h" 41227753Stheraven#include "xlocale_private.h" 42132242Stjr 43291336Sngiewchar_t *fgetwln_l(FILE * __restrict, size_t *, locale_t); 44291336Sngie 45132242Stjrwchar_t * 46227753Stheravenfgetwln_l(FILE * __restrict fp, size_t *lenp, locale_t locale) 47132242Stjr{ 48321074Skib wchar_t *ret; 49132242Stjr wint_t wc; 50132242Stjr size_t len; 51305403Sache int savserr; 52305403Sache 53227753Stheraven FIX_LOCALE(locale); 54132242Stjr 55321074Skib FLOCKFILE_CANCELSAFE(fp); 56132242Stjr ORIENT(fp, 1); 57132242Stjr 58305403Sache savserr = fp->_flags & __SERR; 59305403Sache fp->_flags &= ~__SERR; 60305403Sache 61132242Stjr len = 0; 62227753Stheraven while ((wc = __fgetwc(fp, locale)) != WEOF) { 63132242Stjr#define GROW 512 64132242Stjr if (len * sizeof(wchar_t) >= fp->_lb._size && 65304893Sache __slbexpand(fp, (len + GROW) * sizeof(wchar_t))) { 66304893Sache fp->_flags |= __SERR; 67132242Stjr goto error; 68304893Sache } 69133223Stjr *((wchar_t *)fp->_lb._base + len++) = wc; 70132242Stjr if (wc == L'\n') 71132242Stjr break; 72132242Stjr } 73305403Sache /* fgetwc(3) may set both __SEOF and __SERR at once. */ 74305403Sache if (__sferror(fp)) 75132242Stjr goto error; 76132242Stjr 77305403Sache fp->_flags |= savserr; 78305403Sache if (len == 0) 79305403Sache goto error; 80305403Sache 81132242Stjr *lenp = len; 82321074Skib ret = (wchar_t *)fp->_lb._base; 83321074Skibend: 84321074Skib FUNLOCKFILE_CANCELSAFE(); 85321074Skib return (ret); 86132242Stjr 87132242Stjrerror: 88132242Stjr *lenp = 0; 89321074Skib ret = NULL; 90321074Skib goto end; 91132242Stjr} 92304893Sache 93227753Stheravenwchar_t * 94227753Stheravenfgetwln(FILE * __restrict fp, size_t *lenp) 95227753Stheraven{ 96227753Stheraven return fgetwln_l(fp, lenp, __get_locale()); 97227753Stheraven} 98