xref: /freebsd/lib/libc/stdio/getdelim.c (revision 6685ac34d91d361a03918777934cf037bedd8841)
169099ba2SDavid Schultz /*-
269099ba2SDavid Schultz  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
369099ba2SDavid Schultz  * All rights reserved.
469099ba2SDavid Schultz  *
569099ba2SDavid Schultz  * Redistribution and use in source and binary forms, with or without
669099ba2SDavid Schultz  * modification, are permitted provided that the following conditions
769099ba2SDavid Schultz  * are met:
869099ba2SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
969099ba2SDavid Schultz  *    notice, this list of conditions and the following disclaimer.
1069099ba2SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
1169099ba2SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
1269099ba2SDavid Schultz  *    documentation and/or other materials provided with the distribution.
1369099ba2SDavid Schultz  *
1469099ba2SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1569099ba2SDavid Schultz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1669099ba2SDavid Schultz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1769099ba2SDavid Schultz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1869099ba2SDavid Schultz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1969099ba2SDavid Schultz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2069099ba2SDavid Schultz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2169099ba2SDavid Schultz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2269099ba2SDavid Schultz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2369099ba2SDavid Schultz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2469099ba2SDavid Schultz  * SUCH DAMAGE.
2569099ba2SDavid Schultz  */
2669099ba2SDavid Schultz 
2769099ba2SDavid Schultz #include <sys/cdefs.h>
2869099ba2SDavid Schultz __FBSDID("$FreeBSD$");
2969099ba2SDavid Schultz 
3069099ba2SDavid Schultz #include "namespace.h"
3169099ba2SDavid Schultz #include <sys/param.h>
3269099ba2SDavid Schultz #include <errno.h>
3369099ba2SDavid Schultz #include <limits.h>
3469099ba2SDavid Schultz #include <stdio.h>
3569099ba2SDavid Schultz #include <stdlib.h>
3669099ba2SDavid Schultz #include <string.h>
3769099ba2SDavid Schultz #include "un-namespace.h"
3869099ba2SDavid Schultz 
3969099ba2SDavid Schultz #include "libc_private.h"
4069099ba2SDavid Schultz #include "local.h"
4169099ba2SDavid Schultz 
4269099ba2SDavid Schultz static inline size_t
4369099ba2SDavid Schultz p2roundup(size_t n)
4469099ba2SDavid Schultz {
4569099ba2SDavid Schultz 
4669099ba2SDavid Schultz 	if (!powerof2(n)) {
4769099ba2SDavid Schultz 		n--;
4869099ba2SDavid Schultz 		n |= n >> 1;
4969099ba2SDavid Schultz 		n |= n >> 2;
5069099ba2SDavid Schultz 		n |= n >> 4;
5169099ba2SDavid Schultz 		n |= n >> 8;
5269099ba2SDavid Schultz 		n |= n >> 16;
5369099ba2SDavid Schultz #if SIZE_T_MAX > 0xffffffffU
5469099ba2SDavid Schultz 		n |= n >> 32;
5569099ba2SDavid Schultz #endif
5669099ba2SDavid Schultz 		n++;
5769099ba2SDavid Schultz 	}
5869099ba2SDavid Schultz 	return (n);
5969099ba2SDavid Schultz }
6069099ba2SDavid Schultz 
6169099ba2SDavid Schultz /*
6269099ba2SDavid Schultz  * Expand *linep to hold len bytes (up to SSIZE_MAX + 1).
6369099ba2SDavid Schultz  */
6469099ba2SDavid Schultz static inline int
6569099ba2SDavid Schultz expandtofit(char ** __restrict linep, size_t len, size_t * __restrict capp)
6669099ba2SDavid Schultz {
6769099ba2SDavid Schultz 	char *newline;
6869099ba2SDavid Schultz 	size_t newcap;
6969099ba2SDavid Schultz 
7069099ba2SDavid Schultz 	if (len > (size_t)SSIZE_MAX + 1) {
7169099ba2SDavid Schultz 		errno = EOVERFLOW;
7269099ba2SDavid Schultz 		return (-1);
7369099ba2SDavid Schultz 	}
7469099ba2SDavid Schultz 	if (len > *capp) {
7569099ba2SDavid Schultz 		if (len == (size_t)SSIZE_MAX + 1)	/* avoid overflow */
7669099ba2SDavid Schultz 			newcap = (size_t)SSIZE_MAX + 1;
7769099ba2SDavid Schultz 		else
7869099ba2SDavid Schultz 			newcap = p2roundup(len);
7969099ba2SDavid Schultz 		newline = realloc(*linep, newcap);
8069099ba2SDavid Schultz 		if (newline == NULL)
8169099ba2SDavid Schultz 			return (-1);
8269099ba2SDavid Schultz 		*capp = newcap;
8369099ba2SDavid Schultz 		*linep = newline;
8469099ba2SDavid Schultz 	}
8569099ba2SDavid Schultz 	return (0);
8669099ba2SDavid Schultz }
8769099ba2SDavid Schultz 
8869099ba2SDavid Schultz /*
8969099ba2SDavid Schultz  * Append the src buffer to the *dstp buffer. The buffers are of
9069099ba2SDavid Schultz  * length srclen and *dstlenp, respectively, and dst has space for
9169099ba2SDavid Schultz  * *dstlenp bytes. After the call, *dstlenp and *dstcapp are updated
9269099ba2SDavid Schultz  * appropriately, and *dstp is reallocated if needed. Returns 0 on
9369099ba2SDavid Schultz  * success, -1 on allocation failure.
9469099ba2SDavid Schultz  */
9569099ba2SDavid Schultz static int
9669099ba2SDavid Schultz sappend(char ** __restrict dstp, size_t * __restrict dstlenp,
9769099ba2SDavid Schultz 	size_t * __restrict dstcapp, char * __restrict src, size_t srclen)
9869099ba2SDavid Schultz {
9969099ba2SDavid Schultz 
10069099ba2SDavid Schultz 	/* ensure room for srclen + dstlen + terminating NUL */
10169099ba2SDavid Schultz 	if (expandtofit(dstp, srclen + *dstlenp + 1, dstcapp))
10269099ba2SDavid Schultz 		return (-1);
10369099ba2SDavid Schultz 	memcpy(*dstp + *dstlenp, src, srclen);
10469099ba2SDavid Schultz 	*dstlenp += srclen;
10569099ba2SDavid Schultz 	return (0);
10669099ba2SDavid Schultz }
10769099ba2SDavid Schultz 
10869099ba2SDavid Schultz ssize_t
10969099ba2SDavid Schultz getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim,
11069099ba2SDavid Schultz 	 FILE * __restrict fp)
11169099ba2SDavid Schultz {
11269099ba2SDavid Schultz 	u_char *endp;
11369099ba2SDavid Schultz 	size_t linelen;
11469099ba2SDavid Schultz 
11569099ba2SDavid Schultz 	FLOCKFILE(fp);
11669099ba2SDavid Schultz 	ORIENT(fp, -1);
11769099ba2SDavid Schultz 
11869099ba2SDavid Schultz 	if (linep == NULL || linecapp == NULL) {
11969099ba2SDavid Schultz 		errno = EINVAL;
12069099ba2SDavid Schultz 		goto error;
12169099ba2SDavid Schultz 	}
12269099ba2SDavid Schultz 
12369099ba2SDavid Schultz 	if (*linecapp == 0)
12469099ba2SDavid Schultz 		*linep = NULL;
12569099ba2SDavid Schultz 
12669099ba2SDavid Schultz 	if (fp->_r <= 0 && __srefill(fp)) {
12769099ba2SDavid Schultz 		/* If fp is at EOF already, we just need space for the NUL. */
12869099ba2SDavid Schultz 		if (__sferror(fp) || expandtofit(linep, 1, linecapp))
12969099ba2SDavid Schultz 			goto error;
1306685ac34SDavid Schultz 		FUNLOCKFILE(fp);
1316685ac34SDavid Schultz 		(*linep)[0] = '\0';
1326685ac34SDavid Schultz 		return (-1);
13369099ba2SDavid Schultz 	}
13469099ba2SDavid Schultz 
1356685ac34SDavid Schultz 	linelen = 0;
13669099ba2SDavid Schultz 	while ((endp = memchr(fp->_p, delim, fp->_r)) == NULL) {
13769099ba2SDavid Schultz 		if (sappend(linep, &linelen, linecapp, fp->_p, fp->_r))
13869099ba2SDavid Schultz 			goto error;
13969099ba2SDavid Schultz 		if (__srefill(fp)) {
14069099ba2SDavid Schultz 			if (__sferror(fp))
14169099ba2SDavid Schultz 				goto error;
14269099ba2SDavid Schultz 			goto done;	/* hit EOF */
14369099ba2SDavid Schultz 		}
14469099ba2SDavid Schultz 	}
14569099ba2SDavid Schultz 	endp++;	/* snarf the delimiter, too */
14669099ba2SDavid Schultz 	if (sappend(linep, &linelen, linecapp, fp->_p, endp - fp->_p))
14769099ba2SDavid Schultz 		goto error;
14869099ba2SDavid Schultz 	fp->_r -= endp - fp->_p;
14969099ba2SDavid Schultz 	fp->_p = endp;
15069099ba2SDavid Schultz done:
15169099ba2SDavid Schultz 	/* Invariant: *linep has space for at least linelen+1 bytes. */
15269099ba2SDavid Schultz 	(*linep)[linelen] = '\0';
15369099ba2SDavid Schultz 	FUNLOCKFILE(fp);
15469099ba2SDavid Schultz 	return (linelen);
15569099ba2SDavid Schultz 
15669099ba2SDavid Schultz error:
15769099ba2SDavid Schultz 	fp->_flags |= __SERR;
15869099ba2SDavid Schultz 	FUNLOCKFILE(fp);
15969099ba2SDavid Schultz 	return (-1);
16069099ba2SDavid Schultz }
161