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"
52 #if SIZE_T_MAX > 0xffffffffU
61 * Expand *linep to hold len bytes (up to SSIZE_MAX + 1).
64 expandtofit(char ** __restrict linep, size_t len, size_t * __restrict capp)
69 if (len > (size_t)SSIZE_MAX + 1) {
74 if (len == (size_t)SSIZE_MAX + 1) /* avoid overflow */
75 newcap = (size_t)SSIZE_MAX + 1;
77 newcap = p2roundup(len);
78 newline = realloc(*linep, newcap);
88 * Append the src buffer to the *dstp buffer. The buffers are of
89 * length srclen and *dstlenp, respectively, and dst has space for
90 * *dstlenp bytes. After the call, *dstlenp and *dstcapp are updated
91 * appropriately, and *dstp is reallocated if needed. Returns 0 on
92 * success, -1 on allocation failure.
95 sappend(char ** __restrict dstp, size_t * __restrict dstlenp,
96 size_t * __restrict dstcapp, char * __restrict src, size_t srclen)
99 /* ensure room for srclen + dstlen + terminating NUL */
100 if (expandtofit(dstp, srclen + *dstlenp + 1, dstcapp))
102 memcpy(*dstp + *dstlenp, src, srclen);
108 getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim,
109 FILE * __restrict fp)
117 if (linep == NULL || linecapp == NULL) {
125 if (fp->pub._r <= 0 && __srefill(fp)) {
126 /* If fp is at EOF already, we just need space for the NUL. */
127 if (__sferror(fp) || expandtofit(linep, 1, linecapp))
135 while ((endp = memchr(fp->pub._p, delim, fp->pub._r)) == NULL) {
136 if (sappend(linep, &linelen, linecapp, fp->pub._p, fp->pub._r))
141 goto done; /* hit EOF */
144 endp++; /* snarf the delimiter, too */
145 if (sappend(linep, &linelen, linecapp, fp->pub._p, endp - fp->pub._p))
147 fp->pub._r -= endp - fp->pub._p;
150 /* Invariant: *linep has space for at least linelen+1 bytes. */
151 (*linep)[linelen] = '\0';
156 fp->pub._flags |= __SERR;