16301e6c7SDarren Tucker /*	$NetBSD: getline.c,v 1.1.1.6 2015/01/02 20:34:27 christos Exp $	*/
26301e6c7SDarren Tucker 
36301e6c7SDarren Tucker /*	NetBSD: getline.c,v 1.2 2014/09/16 17:23:50 christos Exp 	*/
46301e6c7SDarren Tucker 
56301e6c7SDarren Tucker /*-
66301e6c7SDarren Tucker  * Copyright (c) 2011 The NetBSD Foundation, Inc.
76301e6c7SDarren Tucker  * All rights reserved.
86301e6c7SDarren Tucker  *
96301e6c7SDarren Tucker  * This code is derived from software contributed to The NetBSD Foundation
106301e6c7SDarren Tucker  * by Christos Zoulas.
116301e6c7SDarren Tucker  *
126301e6c7SDarren Tucker  * Redistribution and use in source and binary forms, with or without
136301e6c7SDarren Tucker  * modification, are permitted provided that the following conditions
146301e6c7SDarren Tucker  * are met:
156301e6c7SDarren Tucker  * 1. Redistributions of source code must retain the above copyright
166301e6c7SDarren Tucker  *    notice, this list of conditions and the following disclaimer.
176301e6c7SDarren Tucker  * 2. Redistributions in binary form must reproduce the above copyright
186301e6c7SDarren Tucker  *    notice, this list of conditions and the following disclaimer in the
196301e6c7SDarren Tucker  *    documentation and/or other materials provided with the distribution.
206301e6c7SDarren Tucker  *
216301e6c7SDarren Tucker  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
226301e6c7SDarren Tucker  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
236301e6c7SDarren Tucker  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
246301e6c7SDarren Tucker  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
256301e6c7SDarren Tucker  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
266301e6c7SDarren Tucker  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
276301e6c7SDarren Tucker  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
286301e6c7SDarren Tucker  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
296301e6c7SDarren Tucker  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
306301e6c7SDarren Tucker  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
316301e6c7SDarren Tucker  * POSSIBILITY OF SUCH DAMAGE.
326301e6c7SDarren Tucker  */
336301e6c7SDarren Tucker 
346301e6c7SDarren Tucker /* NETBSD ORIGINAL: external/bsd/file/dist/src/getline.c */
356301e6c7SDarren Tucker 
366301e6c7SDarren Tucker #include "includes.h"
376301e6c7SDarren Tucker 
386301e6c7SDarren Tucker #if 0
396301e6c7SDarren Tucker #include "file.h"
406301e6c7SDarren Tucker #endif
416301e6c7SDarren Tucker 
426301e6c7SDarren Tucker #if !HAVE_GETLINE
436301e6c7SDarren Tucker #include <stdlib.h>
446301e6c7SDarren Tucker #include <stdio.h>
456301e6c7SDarren Tucker #include <unistd.h>
466301e6c7SDarren Tucker #include <errno.h>
476301e6c7SDarren Tucker #include <string.h>
486301e6c7SDarren Tucker 
496301e6c7SDarren Tucker static ssize_t
getdelim(char ** buf,size_t * bufsiz,int delimiter,FILE * fp)506301e6c7SDarren Tucker getdelim(char **buf, size_t *bufsiz, int delimiter, FILE *fp)
516301e6c7SDarren Tucker {
526301e6c7SDarren Tucker 	char *ptr, *eptr;
536301e6c7SDarren Tucker 
546301e6c7SDarren Tucker 
556301e6c7SDarren Tucker 	if (*buf == NULL || *bufsiz == 0) {
56*872517ddSDarren Tucker 		if ((*buf = malloc(BUFSIZ)) == NULL)
576301e6c7SDarren Tucker 			return -1;
58*872517ddSDarren Tucker 		*bufsiz = BUFSIZ;
596301e6c7SDarren Tucker 	}
606301e6c7SDarren Tucker 
616301e6c7SDarren Tucker 	for (ptr = *buf, eptr = *buf + *bufsiz;;) {
626301e6c7SDarren Tucker 		int c = fgetc(fp);
636301e6c7SDarren Tucker 		if (c == -1) {
646301e6c7SDarren Tucker 			if (feof(fp)) {
656301e6c7SDarren Tucker 				ssize_t diff = (ssize_t)(ptr - *buf);
666301e6c7SDarren Tucker 				if (diff != 0) {
676301e6c7SDarren Tucker 					*ptr = '\0';
686301e6c7SDarren Tucker 					return diff;
696301e6c7SDarren Tucker 				}
706301e6c7SDarren Tucker 			}
716301e6c7SDarren Tucker 			return -1;
726301e6c7SDarren Tucker 		}
736301e6c7SDarren Tucker 		*ptr++ = c;
746301e6c7SDarren Tucker 		if (c == delimiter) {
756301e6c7SDarren Tucker 			*ptr = '\0';
766301e6c7SDarren Tucker 			return ptr - *buf;
776301e6c7SDarren Tucker 		}
786301e6c7SDarren Tucker 		if (ptr + 2 >= eptr) {
796301e6c7SDarren Tucker 			char *nbuf;
806301e6c7SDarren Tucker 			size_t nbufsiz = *bufsiz * 2;
816301e6c7SDarren Tucker 			ssize_t d = ptr - *buf;
826301e6c7SDarren Tucker 			if ((nbuf = realloc(*buf, nbufsiz)) == NULL)
836301e6c7SDarren Tucker 				return -1;
846301e6c7SDarren Tucker 			*buf = nbuf;
856301e6c7SDarren Tucker 			*bufsiz = nbufsiz;
866301e6c7SDarren Tucker 			eptr = nbuf + nbufsiz;
876301e6c7SDarren Tucker 			ptr = nbuf + d;
886301e6c7SDarren Tucker 		}
896301e6c7SDarren Tucker 	}
906301e6c7SDarren Tucker }
916301e6c7SDarren Tucker 
926301e6c7SDarren Tucker ssize_t
getline(char ** buf,size_t * bufsiz,FILE * fp)936301e6c7SDarren Tucker getline(char **buf, size_t *bufsiz, FILE *fp)
946301e6c7SDarren Tucker {
956301e6c7SDarren Tucker 	return getdelim(buf, bufsiz, '\n', fp);
966301e6c7SDarren Tucker }
976301e6c7SDarren Tucker 
986301e6c7SDarren Tucker #endif
996301e6c7SDarren Tucker 
1006301e6c7SDarren Tucker #ifdef TEST
1016301e6c7SDarren Tucker int
main(int argc,char * argv[])1026301e6c7SDarren Tucker main(int argc, char *argv[])
1036301e6c7SDarren Tucker {
1046301e6c7SDarren Tucker 	char *p = NULL;
1056301e6c7SDarren Tucker 	ssize_t len;
1066301e6c7SDarren Tucker 	size_t n = 0;
1076301e6c7SDarren Tucker 
1086301e6c7SDarren Tucker 	while ((len = getline(&p, &n, stdin)) != -1)
1096301e6c7SDarren Tucker 		(void)printf("%" SIZE_T_FORMAT "d %s", len, p);
1106301e6c7SDarren Tucker 	free(p);
1116301e6c7SDarren Tucker 	return 0;
1126301e6c7SDarren Tucker }
1136301e6c7SDarren Tucker #endif
114