xref: /dragonfly/lib/libc/stdio/getdelim.c (revision 36a3d1d6)
1 /*-
2  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/lib/libc/stdio/getdelim.c,v 1.2 2009/04/06 13:50:04 das Exp $
27  */
28 
29 #include "namespace.h"
30 #include <sys/param.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include "un-namespace.h"
37 
38 #include "libc_private.h"
39 #include "local.h"
40 #include "priv_stdio.h"
41 
42 static inline size_t
43 p2roundup(size_t n)
44 {
45 
46 	if (!powerof2(n)) {
47 		n--;
48 		n |= n >> 1;
49 		n |= n >> 2;
50 		n |= n >> 4;
51 		n |= n >> 8;
52 		n |= n >> 16;
53 #if SIZE_T_MAX > 0xffffffffU
54 		n |= n >> 32;
55 #endif
56 		n++;
57 	}
58 	return (n);
59 }
60 
61 /*
62  * Expand *linep to hold len bytes (up to SSIZE_MAX + 1).
63  */
64 static inline int
65 expandtofit(char ** __restrict linep, size_t len, size_t * __restrict capp)
66 {
67 	char *newline;
68 	size_t newcap;
69 
70 	if (len > (size_t)SSIZE_MAX + 1) {
71 		errno = EOVERFLOW;
72 		return (-1);
73 	}
74 	if (len > *capp) {
75 		if (len == (size_t)SSIZE_MAX + 1)	/* avoid overflow */
76 			newcap = (size_t)SSIZE_MAX + 1;
77 		else
78 			newcap = p2roundup(len);
79 		newline = realloc(*linep, newcap);
80 		if (newline == NULL)
81 			return (-1);
82 		*capp = newcap;
83 		*linep = newline;
84 	}
85 	return (0);
86 }
87 
88 /*
89  * Append the src buffer to the *dstp buffer. The buffers are of
90  * length srclen and *dstlenp, respectively, and dst has space for
91  * *dstlenp bytes. After the call, *dstlenp and *dstcapp are updated
92  * appropriately, and *dstp is reallocated if needed. Returns 0 on
93  * success, -1 on allocation failure.
94  */
95 static int
96 sappend(char ** __restrict dstp, size_t * __restrict dstlenp,
97 	size_t * __restrict dstcapp, char * __restrict src, size_t srclen)
98 {
99 
100 	/* ensure room for srclen + dstlen + terminating NUL */
101 	if (expandtofit(dstp, srclen + *dstlenp + 1, dstcapp))
102 		return (-1);
103 	memcpy(*dstp + *dstlenp, src, srclen);
104 	*dstlenp += srclen;
105 	return (0);
106 }
107 
108 ssize_t
109 getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim,
110 	 FILE * __restrict fp)
111 {
112 	u_char *endp;
113 	size_t linelen;
114 
115 	FLOCKFILE(fp);
116 	ORIENT(fp, -1);
117 
118 	if (linep == NULL || linecapp == NULL) {
119 		errno = EINVAL;
120 		goto error;
121 	}
122 
123 	if (*linecapp == 0)
124 		*linep = NULL;
125 
126 	if (fp->pub._r <= 0 && __srefill(fp)) {
127 		/* If fp is at EOF already, we just need space for the NUL. */
128 		if (__sferror(fp) || expandtofit(linep, 1, linecapp))
129 			goto error;
130 		FUNLOCKFILE(fp);
131 		(*linep)[0] = '\0';
132 		return (-1);
133 	}
134 
135 	linelen = 0;
136 	while ((endp = memchr(fp->pub._p, delim, fp->pub._r)) == NULL) {
137 		if (sappend(linep, &linelen, linecapp, fp->pub._p, fp->pub._r))
138 			goto error;
139 		if (__srefill(fp)) {
140 			if (__sferror(fp))
141 				goto error;
142 			goto done;	/* hit EOF */
143 		}
144 	}
145 	endp++;	/* snarf the delimiter, too */
146 	if (sappend(linep, &linelen, linecapp, fp->pub._p, endp - fp->pub._p))
147 		goto error;
148 	fp->pub._r -= endp - fp->pub._p;
149 	fp->pub._p = endp;
150 done:
151 	/* Invariant: *linep has space for at least linelen+1 bytes. */
152 	(*linep)[linelen] = '\0';
153 	FUNLOCKFILE(fp);
154 	return (linelen);
155 
156 error:
157 	fp->pub._flags |= __SERR;
158 	FUNLOCKFILE(fp);
159 	return (-1);
160 }
161