1/*	$OpenBSD: fgetln.c,v 1.17 2017/03/17 14:53:08 deraadt Exp $ */
2/*-
3 * Copyright (c) 1990, 1993
4 *	The Regents of the University of California.  All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Chris Torek.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include "local.h"
38
39/*
40 * Expand the line buffer.  Return -1 on error.
41 */
42static int
43__slbexpand(FILE *fp, size_t newsize)
44{
45	void *p;
46
47	if (fp->_lb._size >= newsize)
48		return (0);
49	if ((p = recallocarray(fp->_lb._base, fp->_lb._size, newsize, 1)) == NULL)
50		return (-1);
51	fp->_lb._base = p;
52	fp->_lb._size = newsize;
53	return (0);
54}
55
56/*
57 * Get an input line.  The returned pointer often (but not always)
58 * points into a stdio buffer.  Fgetline does not alter the text of
59 * the returned line (which is thus not a C string because it will
60 * not necessarily end with '\0'), but does allow callers to modify
61 * it if they wish.  Thus, we set __SMOD in case the caller does.
62 */
63char *
64fgetln(FILE *fp, size_t *lenp)
65{
66	unsigned char *p;
67	char *ret;
68	size_t len;
69	size_t off;
70
71	FLOCKFILE(fp);
72	_SET_ORIENTATION(fp, -1);
73
74	/* make sure there is input */
75	if (fp->_r <= 0 && __srefill(fp))
76		goto error;
77
78	/* look for a newline in the input */
79	if ((p = memchr(fp->_p, '\n', fp->_r)) != NULL) {
80		/*
81		 * Found one.  Flag buffer as modified to keep fseek from
82		 * `optimising' a backward seek, in case the user stomps on
83		 * the text.
84		 */
85		p++;		/* advance over it */
86		ret = (char *)fp->_p;
87		*lenp = len = p - fp->_p;
88		fp->_flags |= __SMOD;
89		fp->_r -= len;
90		fp->_p = p;
91		FUNLOCKFILE(fp);
92		return (ret);
93	}
94
95	/*
96	 * We have to copy the current buffered data to the line buffer.
97	 * As a bonus, though, we can leave off the __SMOD.
98	 *
99	 * OPTIMISTIC is length that we (optimistically) expect will
100	 * accommodate the `rest' of the string, on each trip through the
101	 * loop below.
102	 */
103#define OPTIMISTIC 80
104
105	for (len = fp->_r, off = 0;; len += fp->_r) {
106		size_t diff;
107
108		/*
109		 * Make sure there is room for more bytes.  Copy data from
110		 * file buffer to line buffer, refill file and look for
111		 * newline.  The loop stops only when we find a newline.
112		 */
113		if (__slbexpand(fp, len + OPTIMISTIC))
114			goto error;
115		(void)memcpy(fp->_lb._base + off, fp->_p, len - off);
116		off = len;
117		if (__srefill(fp)) {
118			if (fp->_flags & __SEOF)
119				break;
120			goto error;
121		}
122		if ((p = memchr(fp->_p, '\n', fp->_r)) == NULL)
123			continue;
124
125		/* got it: finish up the line (like code above) */
126		p++;
127		diff = p - fp->_p;
128		len += diff;
129		if (__slbexpand(fp, len))
130			goto error;
131		(void)memcpy(fp->_lb._base + off, fp->_p, diff);
132		fp->_r -= diff;
133		fp->_p = p;
134		break;
135	}
136	*lenp = len;
137	ret = (char *)fp->_lb._base;
138	FUNLOCKFILE(fp);
139	return (ret);
140
141error:
142	FUNLOCKFILE(fp);
143	*lenp = 0;
144	return (NULL);
145}
146DEF_WEAK(fgetln);
147