fgetwln.c revision 132242
1132242Stjr/*-
2132242Stjr * Copyright (c) 2002-2004 Tim J. Robbins.
3132242Stjr * All rights reserved.
4132242Stjr *
5132242Stjr * Redistribution and use in source and binary forms, with or without
6132242Stjr * modification, are permitted provided that the following conditions
7132242Stjr * are met:
8132242Stjr * 1. Redistributions of source code must retain the above copyright
9132242Stjr *    notice, this list of conditions and the following disclaimer.
10132242Stjr * 2. Redistributions in binary form must reproduce the above copyright
11132242Stjr *    notice, this list of conditions and the following disclaimer in the
12132242Stjr *    documentation and/or other materials provided with the distribution.
13132242Stjr *
14132242Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15132242Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16132242Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17132242Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18132242Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19132242Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20132242Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21132242Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22132242Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23132242Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24132242Stjr * SUCH DAMAGE.
25132242Stjr */
26132242Stjr
27132242Stjr#include <sys/cdefs.h>
28132242Stjr__FBSDID("$FreeBSD: head/lib/libc/stdio/fgetwln.c 132242 2004-07-16 06:06:09Z tjr $");
29132242Stjr
30132242Stjr#include "namespace.h"
31132242Stjr#include <stdio.h>
32132242Stjr#include <wchar.h>
33132242Stjr#include "un-namespace.h"
34132242Stjr#include "libc_private.h"
35132242Stjr#include "local.h"
36132242Stjr
37132242Stjrwchar_t *
38132242Stjrfgetwln(FILE * __restrict fp, size_t *lenp)
39132242Stjr{
40132242Stjr	wint_t wc;
41132242Stjr	size_t len;
42132242Stjr
43132242Stjr	FLOCKFILE(fp);
44132242Stjr	ORIENT(fp, 1);
45132242Stjr
46132242Stjr	len = 0;
47132242Stjr	while ((wc = __fgetwc(fp)) != WEOF) {
48132242Stjr#define	GROW	512
49132242Stjr		len++;
50132242Stjr		if (len * sizeof(wchar_t) >= fp->_lb._size &&
51132242Stjr		    __slbexpand(fp, (len + GROW) * sizeof(wchar_t)))
52132242Stjr			goto error;
53132242Stjr		*((wchar_t *)fp->_lb._base + len) = wc;
54132242Stjr		if (wc == L'\n')
55132242Stjr			break;
56132242Stjr	}
57132242Stjr	if (len == 0)
58132242Stjr		goto error;
59132242Stjr
60132242Stjr	FUNLOCKFILE(fp);
61132242Stjr	*lenp = len;
62132242Stjr	return ((wchar_t *)fp->_lb._base);
63132242Stjr
64132242Stjrerror:
65132242Stjr	FUNLOCKFILE(fp);
66132242Stjr	*lenp = 0;
67132242Stjr	return (NULL);
68132242Stjr}
69