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$");
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
43132242Stjrwchar_t *
44227753Stheravenfgetwln_l(FILE * __restrict fp, size_t *lenp, locale_t locale)
45132242Stjr{
46132242Stjr	wint_t wc;
47132242Stjr	size_t len;
48227753Stheraven	FIX_LOCALE(locale);
49132242Stjr
50132242Stjr	FLOCKFILE(fp);
51132242Stjr	ORIENT(fp, 1);
52132242Stjr
53132242Stjr	len = 0;
54227753Stheraven	while ((wc = __fgetwc(fp, locale)) != WEOF) {
55132242Stjr#define	GROW	512
56132242Stjr		if (len * sizeof(wchar_t) >= fp->_lb._size &&
57132242Stjr		    __slbexpand(fp, (len + GROW) * sizeof(wchar_t)))
58132242Stjr			goto error;
59133223Stjr		*((wchar_t *)fp->_lb._base + len++) = wc;
60132242Stjr		if (wc == L'\n')
61132242Stjr			break;
62132242Stjr	}
63132242Stjr	if (len == 0)
64132242Stjr		goto error;
65132242Stjr
66132242Stjr	FUNLOCKFILE(fp);
67132242Stjr	*lenp = len;
68132242Stjr	return ((wchar_t *)fp->_lb._base);
69132242Stjr
70132242Stjrerror:
71132242Stjr	FUNLOCKFILE(fp);
72132242Stjr	*lenp = 0;
73132242Stjr	return (NULL);
74132242Stjr}
75227753Stheravenwchar_t *
76227753Stheravenfgetwln(FILE * __restrict fp, size_t *lenp)
77227753Stheraven{
78227753Stheraven	return fgetwln_l(fp, lenp, __get_locale());
79227753Stheraven}
80