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