]>
git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/getdelim.c
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: src/lib/libc/stdio/getdelim.c,v 1.3 2009/10/04 19:43:36 das Exp $");
30 #include "namespace.h"
31 #include <os/overflow.h>
32 #include <sys/param.h>
38 #include "un-namespace.h"
40 #include "libc_private.h"
54 #if SIZE_T_MAX > 0xffffffffU
63 * Expand *linep to hold len bytes (up to SSIZE_MAX + 1).
66 expandtofit(char ** __restrict linep
, size_t len
, size_t * __restrict capp
)
71 if (len
> (size_t)SSIZE_MAX
+ 1) {
76 if (len
== (size_t)SSIZE_MAX
+ 1) /* avoid overflow */
77 newcap
= (size_t)SSIZE_MAX
+ 1;
79 newcap
= p2roundup(len
);
80 newline
= realloc(*linep
, newcap
);
90 * Append the src buffer to the *dstp buffer. The buffers are of
91 * length srclen and *dstlenp, respectively, and dst has space for
92 * *dstlenp bytes. After the call, *dstlenp and *dstcapp are updated
93 * appropriately, and *dstp is reallocated if needed. Returns 0 on
94 * success, -1 on allocation failure.
97 sappend(char ** __restrict dstp
, size_t * __restrict dstlenp
,
98 size_t * __restrict dstcapp
, char * __restrict src
, size_t srclen
)
102 /* avoid overflowing the result length */
103 if (os_add3_overflow(srclen
, *dstlenp
, 1, &tmp
)) {
108 /* ensure room for srclen + dstlen + terminating NUL */
109 if (expandtofit(dstp
, tmp
, dstcapp
))
111 memcpy(*dstp
+ *dstlenp
, src
, srclen
);
117 getdelim(char ** __restrict linep
, size_t * __restrict linecapp
, int delim
,
118 FILE * __restrict fp
)
126 if (linep
== NULL
|| linecapp
== NULL
) {
134 if (fp
->_r
<= 0 && __srefill(fp
)) {
135 /* If fp is at EOF already, we just need space for the NUL. */
136 if (__sferror(fp
) || expandtofit(linep
, 1, linecapp
))
144 while ((endp
= memchr(fp
->_p
, delim
, fp
->_r
)) == NULL
) {
145 if (sappend(linep
, &linelen
, linecapp
, (char*)fp
->_p
, fp
->_r
))
150 goto done
; /* hit EOF */
153 endp
++; /* snarf the delimiter, too */
154 if (sappend(linep
, &linelen
, linecapp
, (char*)fp
->_p
, endp
- fp
->_p
))
156 fp
->_r
-= endp
- fp
->_p
;
159 /* Invariant: *linep has space for at least linelen+1 bytes. */
160 (*linep
)[linelen
] = '\0';
165 fp
->_flags
|= __SERR
;