1 /* -*- mode: c; c-file-style: "openbsd" -*- */
2 /*
3  * Copyright (c) 2009 The NetBSD Foundation, Inc.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Roy Marples.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* Several modifications to make the code more portable (and less robust and far less efficient) */
30 
31 #include <stdio.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #define MINBUF 128
40 
41 static ssize_t
___getdelim(char ** buf,size_t * buflen,int sep,FILE * fp)42 ___getdelim(char **buf, size_t *buflen,
43     int sep, FILE *fp)
44 {
45 	int p;
46 	size_t len = 0, newlen;
47 	char *newb;
48 
49 	if (buf == NULL || buflen == NULL) {
50 		errno = EINVAL;
51 		return -1;
52 	}
53 
54 	/* If buf is NULL, we have to assume a size of zero */
55 	if (*buf == NULL)
56 		*buflen = 0;
57 
58 	do {
59 		p = fgetc(fp);
60 		if (ferror(fp))
61 			return -1;
62 		if (p == EOF)
63 			break;
64 
65 		/* Ensure we can handle it */
66 		if (len > SSIZE_MAX) {
67 			errno = EOVERFLOW;
68 			return -1;
69 		}
70 		newlen = len + 2; /* reserve space for NUL terminator */
71 		if (newlen > *buflen) {
72 			if (newlen < MINBUF)
73 				newlen = MINBUF;
74 #define powerof2(x) ((((x)-1)&(x))==0)
75 			if (!powerof2(newlen)) {
76 				/* Grow the buffer to the next power of 2 */
77 				newlen--;
78 				newlen |= newlen >> 1;
79 				newlen |= newlen >> 2;
80 				newlen |= newlen >> 4;
81 				newlen |= newlen >> 8;
82 				newlen |= newlen >> 16;
83 #if SIZE_MAX > 0xffffffffU
84 				newlen |= newlen >> 32;
85 #endif
86 				newlen++;
87 			}
88 
89 			newb = realloc(*buf, newlen);
90 			if (newb == NULL)
91 				return -1;
92 			*buf = newb;
93 			*buflen = newlen;
94 		}
95 
96 		(*buf)[len++] = p;
97 	} while (p != sep);
98 
99 	/* POSIX demands we return -1 on EOF. */
100 	if (len == 0)
101 		return -1;
102 
103 	if (*buf != NULL)
104 		(*buf)[len] = '\0';
105 	return (ssize_t)len;
106 }
107 
108 ssize_t
getline(char ** buf,size_t * buflen,FILE * fp)109 getline(char **buf, size_t *buflen, FILE *fp)
110 {
111 	return ___getdelim(buf, buflen, '\n', fp);
112 }
113