1*0afa8e06SEd Maste /* $NetBSD: getline.c,v 1.1.1.6 2015/01/02 20:34:27 christos Exp $ */
2*0afa8e06SEd Maste
3*0afa8e06SEd Maste /* NetBSD: getline.c,v 1.2 2014/09/16 17:23:50 christos Exp */
4*0afa8e06SEd Maste
5*0afa8e06SEd Maste /*-
6*0afa8e06SEd Maste * Copyright (c) 2011 The NetBSD Foundation, Inc.
7*0afa8e06SEd Maste * All rights reserved.
8*0afa8e06SEd Maste *
9*0afa8e06SEd Maste * This code is derived from software contributed to The NetBSD Foundation
10*0afa8e06SEd Maste * by Christos Zoulas.
11*0afa8e06SEd Maste *
12*0afa8e06SEd Maste * Redistribution and use in source and binary forms, with or without
13*0afa8e06SEd Maste * modification, are permitted provided that the following conditions
14*0afa8e06SEd Maste * are met:
15*0afa8e06SEd Maste * 1. Redistributions of source code must retain the above copyright
16*0afa8e06SEd Maste * notice, this list of conditions and the following disclaimer.
17*0afa8e06SEd Maste * 2. Redistributions in binary form must reproduce the above copyright
18*0afa8e06SEd Maste * notice, this list of conditions and the following disclaimer in the
19*0afa8e06SEd Maste * documentation and/or other materials provided with the distribution.
20*0afa8e06SEd Maste *
21*0afa8e06SEd Maste * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22*0afa8e06SEd Maste * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23*0afa8e06SEd Maste * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24*0afa8e06SEd Maste * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25*0afa8e06SEd Maste * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26*0afa8e06SEd Maste * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27*0afa8e06SEd Maste * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*0afa8e06SEd Maste * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29*0afa8e06SEd Maste * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30*0afa8e06SEd Maste * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31*0afa8e06SEd Maste * POSSIBILITY OF SUCH DAMAGE.
32*0afa8e06SEd Maste */
33*0afa8e06SEd Maste
34*0afa8e06SEd Maste /* NETBSD ORIGINAL: external/bsd/file/dist/src/getline.c */
35*0afa8e06SEd Maste
36*0afa8e06SEd Maste #include "openbsd-compat.h"
37*0afa8e06SEd Maste
38*0afa8e06SEd Maste #if 0
39*0afa8e06SEd Maste #include "file.h"
40*0afa8e06SEd Maste #endif
41*0afa8e06SEd Maste
42*0afa8e06SEd Maste #if !HAVE_GETLINE
43*0afa8e06SEd Maste #include <stdlib.h>
44*0afa8e06SEd Maste #include <stdio.h>
45*0afa8e06SEd Maste #ifdef HAVE_UNISTD_H
46*0afa8e06SEd Maste #include <unistd.h>
47*0afa8e06SEd Maste #endif
48*0afa8e06SEd Maste #include <errno.h>
49*0afa8e06SEd Maste #include <string.h>
50*0afa8e06SEd Maste
51*0afa8e06SEd Maste static ssize_t
getdelim(char ** buf,size_t * bufsiz,int delimiter,FILE * fp)52*0afa8e06SEd Maste getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
53*0afa8e06SEd Maste {
54*0afa8e06SEd Maste char *ptr, *eptr;
55*0afa8e06SEd Maste
56*0afa8e06SEd Maste
57*0afa8e06SEd Maste if (*buf == NULL || *bufsiz == 0) {
58*0afa8e06SEd Maste if ((*buf = malloc(BUFSIZ)) == NULL)
59*0afa8e06SEd Maste return -1;
60*0afa8e06SEd Maste *bufsiz = BUFSIZ;
61*0afa8e06SEd Maste }
62*0afa8e06SEd Maste
63*0afa8e06SEd Maste for (ptr = *buf, eptr = *buf + *bufsiz;;) {
64*0afa8e06SEd Maste int c = fgetc(fp);
65*0afa8e06SEd Maste if (c == -1) {
66*0afa8e06SEd Maste if (feof(fp)) {
67*0afa8e06SEd Maste ssize_t diff = (ssize_t)(ptr - *buf);
68*0afa8e06SEd Maste if (diff != 0) {
69*0afa8e06SEd Maste *ptr = '\0';
70*0afa8e06SEd Maste return diff;
71*0afa8e06SEd Maste }
72*0afa8e06SEd Maste }
73*0afa8e06SEd Maste return -1;
74*0afa8e06SEd Maste }
75*0afa8e06SEd Maste *ptr++ = (char)c;
76*0afa8e06SEd Maste if (c == delimiter) {
77*0afa8e06SEd Maste *ptr = '\0';
78*0afa8e06SEd Maste return ptr - *buf;
79*0afa8e06SEd Maste }
80*0afa8e06SEd Maste if (ptr + 2 >= eptr) {
81*0afa8e06SEd Maste char *nbuf;
82*0afa8e06SEd Maste size_t nbufsiz = *bufsiz * 2;
83*0afa8e06SEd Maste ssize_t d = ptr - *buf;
84*0afa8e06SEd Maste if ((nbuf = realloc(*buf, nbufsiz)) == NULL)
85*0afa8e06SEd Maste return -1;
86*0afa8e06SEd Maste *buf = nbuf;
87*0afa8e06SEd Maste *bufsiz = nbufsiz;
88*0afa8e06SEd Maste eptr = nbuf + nbufsiz;
89*0afa8e06SEd Maste ptr = nbuf + d;
90*0afa8e06SEd Maste }
91*0afa8e06SEd Maste }
92*0afa8e06SEd Maste }
93*0afa8e06SEd Maste
94*0afa8e06SEd Maste ssize_t
getline(char ** buf,size_t * bufsiz,FILE * fp)95*0afa8e06SEd Maste getline(char **buf, size_t *bufsiz, FILE *fp)
96*0afa8e06SEd Maste {
97*0afa8e06SEd Maste return getdelim(buf, bufsiz, '\n', fp);
98*0afa8e06SEd Maste }
99*0afa8e06SEd Maste
100*0afa8e06SEd Maste #endif
101*0afa8e06SEd Maste
102*0afa8e06SEd Maste #ifdef TEST
103*0afa8e06SEd Maste int
main(int argc,char * argv[])104*0afa8e06SEd Maste main(int argc, char *argv[])
105*0afa8e06SEd Maste {
106*0afa8e06SEd Maste char *p = NULL;
107*0afa8e06SEd Maste ssize_t len;
108*0afa8e06SEd Maste size_t n = 0;
109*0afa8e06SEd Maste
110*0afa8e06SEd Maste while ((len = getline(&p, &n, stdin)) != -1)
111*0afa8e06SEd Maste (void)printf("%" SIZE_T_FORMAT "d %s", len, p);
112*0afa8e06SEd Maste free(p);
113*0afa8e06SEd Maste return 0;
114*0afa8e06SEd Maste }
115*0afa8e06SEd Maste #endif
116