2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "namespace.h"
33 #include <sys/param.h>
39 #include "un-namespace.h"
41 #include "libc_private.h"
55 #if SIZE_T_MAX > 0xffffffffU
64 * Expand *linep to hold len bytes (up to SSIZE_MAX + 1).
67 expandtofit(char ** __restrict linep, size_t len, size_t * __restrict capp)
72 if (len > (size_t)SSIZE_MAX + 1) {
77 if (len == (size_t)SSIZE_MAX + 1) /* avoid overflow */
78 newcap = (size_t)SSIZE_MAX + 1;
80 newcap = p2roundup(len);
81 newline = realloc(*linep, newcap);
91 * Append the src buffer to the *dstp buffer. The buffers are of
92 * length srclen and *dstlenp, respectively, and dst has space for
93 * *dstlenp bytes. After the call, *dstlenp and *dstcapp are updated
94 * appropriately, and *dstp is reallocated if needed. Returns 0 on
95 * success, -1 on allocation failure.
98 sappend(char ** __restrict dstp, size_t * __restrict dstlenp,
99 size_t * __restrict dstcapp, char * __restrict src, size_t srclen)
102 /* ensure room for srclen + dstlen + terminating NUL */
103 if (expandtofit(dstp, srclen + *dstlenp + 1, dstcapp))
105 memcpy(*dstp + *dstlenp, src, srclen);
111 getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim,
112 FILE * __restrict fp)
117 FLOCKFILE_CANCELSAFE(fp);
120 if (linep == NULL || linecapp == NULL) {
128 if (fp->_r <= 0 && __srefill(fp)) {
129 /* If fp is at EOF already, we just need space for the NUL. */
130 if (!__sfeof(fp) || expandtofit(linep, 1, linecapp))
138 while ((endp = memchr(fp->_p, delim, fp->_r)) == NULL) {
139 if (sappend(linep, &linelen, linecapp, fp->_p, fp->_r))
144 goto done; /* hit EOF */
147 endp++; /* snarf the delimiter, too */
148 if (sappend(linep, &linelen, linecapp, fp->_p, endp - fp->_p))
150 fp->_r -= endp - fp->_p;
153 /* Invariant: *linep has space for at least linelen+1 bytes. */
154 (*linep)[linelen] = '\0';
156 FUNLOCKFILE_CANCELSAFE();
160 fp->_flags |= __SERR;