xref: /netbsd-src/lib/libc/stdio/fgetln.c (revision da5f4674a3fc214be3572d358b66af40ab9401e7)
1 /*	$NetBSD: fgetln.c,v 1.12 2003/08/07 16:43:23 agc Exp $	*/
2 
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 <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)fgetline.c	8.1 (Berkeley) 6/4/93";
39 #else
40 __RCSID("$NetBSD: fgetln.c,v 1.12 2003/08/07 16:43:23 agc Exp $");
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43 
44 #include "namespace.h"
45 
46 #include <assert.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include "reentrant.h"
51 #include "local.h"
52 
53 #ifdef __weak_alias
54 __weak_alias(fgetln,_fgetln)
55 #endif
56 
57 int __slbexpand __P((FILE *, size_t));
58 
59 /*
60  * Expand the line buffer.  Return -1 on error.
61 #ifdef notdef
62  * The `new size' does not account for a terminating '\0',
63  * so we add 1 here.
64 #endif
65  */
66 int
67 __slbexpand(fp, newsize)
68 	FILE *fp;
69 	size_t newsize;
70 {
71 	void *p;
72 
73 #ifdef notdef
74 	++newsize;
75 #endif
76 	_DIAGASSERT(fp != NULL);
77 
78 	if (fp->_lb._size >= newsize)
79 		return (0);
80 	if ((p = realloc(fp->_lb._base, newsize)) == NULL)
81 		return (-1);
82 	fp->_lb._base = p;
83 	fp->_lb._size = newsize;
84 	return (0);
85 }
86 
87 /*
88  * Get an input line.  The returned pointer often (but not always)
89  * points into a stdio buffer.  Fgetline does not alter the text of
90  * the returned line (which is thus not a C string because it will
91  * not necessarily end with '\0'), but does allow callers to modify
92  * it if they wish.  Thus, we set __SMOD in case the caller does.
93  */
94 char *
95 fgetln(fp, lenp)
96 	FILE *fp;
97 	size_t *lenp;
98 {
99 	unsigned char *p;
100 	size_t len;
101 	size_t off;
102 
103 	_DIAGASSERT(fp != NULL);
104 	_DIAGASSERT(lenp != NULL);
105 
106 	FLOCKFILE(fp);
107 
108 	/* make sure there is input */
109 	if (fp->_r <= 0 && __srefill(fp)) {
110 		*lenp = 0;
111 		FUNLOCKFILE(fp);
112 		return (NULL);
113 	}
114 
115 	/* look for a newline in the input */
116 	if ((p = memchr((void *)fp->_p, '\n', (size_t)fp->_r)) != NULL) {
117 		char *ret;
118 
119 		/*
120 		 * Found one.  Flag buffer as modified to keep fseek from
121 		 * `optimising' a backward seek, in case the user stomps on
122 		 * the text.
123 		 */
124 		p++;		/* advance over it */
125 		ret = (char *)fp->_p;
126 		*lenp = len = p - fp->_p;
127 		fp->_flags |= __SMOD;
128 		fp->_r -= len;
129 		fp->_p = p;
130 		FUNLOCKFILE(fp);
131 		return (ret);
132 	}
133 
134 	/*
135 	 * We have to copy the current buffered data to the line buffer.
136 	 * As a bonus, though, we can leave off the __SMOD.
137 	 *
138 	 * OPTIMISTIC is length that we (optimistically) expect will
139 	 * accomodate the `rest' of the string, on each trip through the
140 	 * loop below.
141 	 */
142 #define OPTIMISTIC 80
143 
144 	for (len = fp->_r, off = 0;; len += fp->_r) {
145 		size_t diff;
146 
147 		/*
148 		 * Make sure there is room for more bytes.  Copy data from
149 		 * file buffer to line buffer, refill file and look for
150 		 * newline.  The loop stops only when we find a newline.
151 		 */
152 		if (__slbexpand(fp, len + OPTIMISTIC))
153 			goto error;
154 		(void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,
155 		    len - off);
156 		off = len;
157 		if (__srefill(fp))
158 			break;	/* EOF or error: return partial line */
159 		if ((p = memchr((void *)fp->_p, '\n', (size_t)fp->_r)) == NULL)
160 			continue;
161 
162 		/* got it: finish up the line (like code above) */
163 		p++;
164 		diff = p - fp->_p;
165 		len += diff;
166 		if (__slbexpand(fp, len))
167 			goto error;
168 		(void)memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,
169 		    diff);
170 		fp->_r -= diff;
171 		fp->_p = p;
172 		break;
173 	}
174 	*lenp = len;
175 #ifdef notdef
176 	fp->_lb._base[len] = 0;
177 #endif
178 	FUNLOCKFILE(fp);
179 	return ((char *)fp->_lb._base);
180 
181 error:
182 	*lenp = 0;		/* ??? */
183 	FUNLOCKFILE(fp);
184 	return (NULL);		/* ??? */
185 }
186