1 /*	$OpenBSD: fgetln.c,v 1.13 2015/01/05 21:58:52 millert 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  */
42 static int
__slbexpand(FILE * fp,size_t newsize)43 __slbexpand(FILE *fp, size_t newsize)
44 {
45 	void *p;
46 
47 	if (fp->_lb._size >= newsize)
48 		return (0);
49 	if ((p = realloc(fp->_lb._base, newsize)) == 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  */
63 char *
fgetln(FILE * fp,size_t * lenp)64 fgetln(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((void *)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((void *)(fp->_lb._base + off), (void *)fp->_p,
116 		    len - off);
117 		off = len;
118 		if (__srefill(fp))
119 			break;	/* EOF or error: return partial line */
120 		if ((p = memchr((void *)fp->_p, '\n', fp->_r)) == NULL)
121 			continue;
122 
123 		/* got it: finish up the line (like code above) */
124 		p++;
125 		diff = p - fp->_p;
126 		len += diff;
127 		if (__slbexpand(fp, len))
128 			goto error;
129 		(void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,
130 		    diff);
131 		fp->_r -= diff;
132 		fp->_p = p;
133 		break;
134 	}
135 	*lenp = len;
136 	ret = (char *)fp->_lb._base;
137 	FUNLOCKFILE(fp);
138 	return (ret);
139 
140 error:
141 	FUNLOCKFILE(fp);
142 	*lenp = 0;
143 	return (NULL);
144 }
145