xref: /csrg-svn/lib/libc/stdio/fgetln.c (revision 46111)
1*46111Sbostic /*-
2*46111Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*46111Sbostic  * All rights reserved.
4*46111Sbostic  *
5*46111Sbostic  * This code is derived from software contributed to Berkeley by
6*46111Sbostic  * Chris Torek.
7*46111Sbostic  *
8*46111Sbostic  * %sccs.include.redist.c%
9*46111Sbostic  */
10*46111Sbostic 
11*46111Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*46111Sbostic static char sccsid[] = "@(#)fgetln.c	5.1 (Berkeley) 01/20/91";
13*46111Sbostic #endif /* LIBC_SCCS and not lint */
14*46111Sbostic 
15*46111Sbostic #include <stdio.h>
16*46111Sbostic #include <stdlib.h>
17*46111Sbostic #include <string.h>
18*46111Sbostic #include "local.h"
19*46111Sbostic 
20*46111Sbostic /*
21*46111Sbostic  * Expand the line buffer.  Return -1 on error.
22*46111Sbostic  * The `new size' does not account for a terminating '\0',
23*46111Sbostic  * so we add 1 here.
24*46111Sbostic  */
25*46111Sbostic __slbexpand(fp, newsize)
26*46111Sbostic 	FILE *fp;
27*46111Sbostic 	size_t newsize;
28*46111Sbostic {
29*46111Sbostic 	void *p;
30*46111Sbostic 
31*46111Sbostic 	if (fp->_lb._size >= ++newsize)
32*46111Sbostic 		return (0);
33*46111Sbostic 	if ((p = realloc(fp->_lb._base, newsize)) == NULL)
34*46111Sbostic 		return (-1);
35*46111Sbostic 	fp->_lb._base = p;
36*46111Sbostic 	fp->_lb._size = newsize;
37*46111Sbostic 	return (0);
38*46111Sbostic }
39*46111Sbostic 
40*46111Sbostic /*
41*46111Sbostic  * Get an input line.  The returned pointer often (but not always)
42*46111Sbostic  * points into a stdio buffer.  Fgetline smashes the newline (if any)
43*46111Sbostic  * in the stdio buffer; callers must not use it on streams that
44*46111Sbostic  * have `magic' setvbuf() games happening.
45*46111Sbostic  */
46*46111Sbostic char *
47*46111Sbostic fgetline(fp, lenp)
48*46111Sbostic 	register FILE *fp;
49*46111Sbostic 	size_t *lenp;
50*46111Sbostic {
51*46111Sbostic 	register unsigned char *p;
52*46111Sbostic 	register size_t len;
53*46111Sbostic 	size_t off;
54*46111Sbostic 
55*46111Sbostic 	/* make sure there is input */
56*46111Sbostic 	if (fp->_r <= 0 && __srefill(fp)) {
57*46111Sbostic 		if (lenp != NULL)
58*46111Sbostic 			*lenp = 0;
59*46111Sbostic 		return (NULL);
60*46111Sbostic 	}
61*46111Sbostic 
62*46111Sbostic 	/* look for a newline in the input */
63*46111Sbostic 	if ((p = memchr((void *)fp->_p, '\n', fp->_r)) != NULL) {
64*46111Sbostic 		register char *ret;
65*46111Sbostic 
66*46111Sbostic 		/*
67*46111Sbostic 		 * Found one.  Flag buffer as modified to keep
68*46111Sbostic 		 * fseek from `optimising' a backward seek, since
69*46111Sbostic 		 * the newline is about to be trashed.  (We should
70*46111Sbostic 		 * be able to get away with doing this only if
71*46111Sbostic 		 * p is not pointing into an ungetc buffer, since
72*46111Sbostic 		 * fseek discards ungetc data, but this is the
73*46111Sbostic 		 * usual case anyway.)
74*46111Sbostic 		 */
75*46111Sbostic 		ret = (char *)fp->_p;
76*46111Sbostic 		len = p - fp->_p;
77*46111Sbostic 		fp->_flags |= __SMOD;
78*46111Sbostic 		*p = 0;
79*46111Sbostic 		fp->_r -= len + 1;
80*46111Sbostic 		fp->_p = p + 1;
81*46111Sbostic 		if (lenp != NULL)
82*46111Sbostic 			*lenp = len;
83*46111Sbostic 		return (ret);
84*46111Sbostic 	}
85*46111Sbostic 
86*46111Sbostic 	/*
87*46111Sbostic 	 * We have to copy the current buffered data to the line buffer.
88*46111Sbostic 	 *
89*46111Sbostic 	 * OPTIMISTIC is length that we (optimistically)
90*46111Sbostic 	 * expect will accomodate the `rest' of the string,
91*46111Sbostic 	 * on each trip through the loop below.
92*46111Sbostic 	 */
93*46111Sbostic #define OPTIMISTIC 80
94*46111Sbostic 
95*46111Sbostic 	for (len = fp->_r, off = 0;; len += fp->_r) {
96*46111Sbostic 		register size_t diff;
97*46111Sbostic 
98*46111Sbostic 		/*
99*46111Sbostic 		 * Make sure there is room for more bytes.
100*46111Sbostic 		 * Copy data from file buffer to line buffer,
101*46111Sbostic 		 * refill file and look for newline.  The
102*46111Sbostic 		 * loop stops only when we find a newline.
103*46111Sbostic 		 */
104*46111Sbostic 		if (__slbexpand(fp, len + OPTIMISTIC))
105*46111Sbostic 			goto error;
106*46111Sbostic 		(void) memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,
107*46111Sbostic 		    len - off);
108*46111Sbostic 		off = len;
109*46111Sbostic 		if (__srefill(fp))
110*46111Sbostic 			break;	/* EOF or error: return partial line */
111*46111Sbostic 		if ((p = memchr((void *)fp->_p, '\n', fp->_r)) == NULL)
112*46111Sbostic 			continue;
113*46111Sbostic 
114*46111Sbostic 		/* got it: finish up the line (like code above) */
115*46111Sbostic 		fp->_flags |= __SMOD;	/* soon */
116*46111Sbostic 		diff = p - fp->_p;
117*46111Sbostic 		len += diff;
118*46111Sbostic 		if (__slbexpand(fp, len))
119*46111Sbostic 			goto error;
120*46111Sbostic 		(void) memcpy((void *)(fp->_lb._base + off), (void *)fp->_p,
121*46111Sbostic 		    diff);
122*46111Sbostic 		fp->_r -= diff + 1;
123*46111Sbostic 		fp->_p = p + 1;
124*46111Sbostic 		break;
125*46111Sbostic 	}
126*46111Sbostic 	if (lenp != NULL)
127*46111Sbostic 		*lenp = len;
128*46111Sbostic 	fp->_lb._base[len] = 0;
129*46111Sbostic 	return ((char *)fp->_lb._base);
130*46111Sbostic 
131*46111Sbostic error:
132*46111Sbostic 	if (lenp != NULL)
133*46111Sbostic 		*lenp = 0;	/* ??? */
134*46111Sbostic 	return (NULL);		/* ??? */
135*46111Sbostic }
136