2 * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/lib/libc/stdio/getdelim.c,v 1.2 2009/04/06 13:50:04 das Exp $
29 #include "namespace.h"
30 #include <sys/param.h>
36 #include "un-namespace.h"
38 #include "libc_private.h"
40 #include "priv_stdio.h"
53 #if SIZE_T_MAX > 0xffffffffU
62 * Expand *linep to hold len bytes (up to SSIZE_MAX + 1).
65 expandtofit(char ** __restrict linep, size_t len, size_t * __restrict capp)
70 if (len > (size_t)SSIZE_MAX + 1) {
75 if (len == (size_t)SSIZE_MAX + 1) /* avoid overflow */
76 newcap = (size_t)SSIZE_MAX + 1;
78 newcap = p2roundup(len);
79 newline = realloc(*linep, newcap);
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.
96 sappend(char ** __restrict dstp, size_t * __restrict dstlenp,
97 size_t * __restrict dstcapp, char * __restrict src, size_t srclen)
100 /* ensure room for srclen + dstlen + terminating NUL */
101 if (expandtofit(dstp, srclen + *dstlenp + 1, dstcapp))
103 memcpy(*dstp + *dstlenp, src, srclen);
109 getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim,
110 FILE * __restrict fp)
118 if (linep == NULL || linecapp == NULL) {
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))
136 while ((endp = memchr(fp->pub._p, delim, fp->pub._r)) == NULL) {
137 if (sappend(linep, &linelen, linecapp, fp->pub._p, fp->pub._r))
142 goto done; /* hit EOF */
145 endp++; /* snarf the delimiter, too */
146 if (sappend(linep, &linelen, linecapp, fp->pub._p, endp - fp->pub._p))
148 fp->pub._r -= endp - fp->pub._p;
151 /* Invariant: *linep has space for at least linelen+1 bytes. */
152 (*linep)[linelen] = '\0';
157 fp->pub._flags |= __SERR;