Home | History | Annotate | Line # | Download | only in compat
fgetln.c revision 1.5
      1 /*	$NetBSD: fgetln.c,v 1.5 2003/10/27 00:12:43 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright 1999 Luke Mewburn <lukem (at) NetBSD.org>.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     24  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     26  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
     27  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include "nbtool_config.h"
     31 
     32 #if !HAVE_FGETLN
     33 #include <stdlib.h>
     34 
     35 #define BUFCHUNKS	BUFSIZ
     36 
     37 char *
     38 fgetln(FILE *fp, size_t *len)
     39 {
     40 	static char *buf;
     41 	static size_t bufsize;
     42 	size_t buflen;
     43 	char curbuf[BUFCHUNKS];
     44 	char *p;
     45 
     46 	if (buf == NULL) {
     47 		bufsize = BUFCHUNKS;
     48 		buf = (char *)malloc(bufsize);
     49 		if (buf == NULL)
     50 			err(1, "Unable to allocate buffer for fgetln()");
     51 	}
     52 
     53 	*buf = '\0';
     54 	buflen = 0;
     55 	while ((p = fgets(curbuf, sizeof(curbuf), fp)) != NULL) {
     56 		size_t l;
     57 
     58 		l = strlen(p);
     59 		if (bufsize < buflen + l) {
     60 			bufsize += BUFCHUNKS;
     61 			if ((buf = (char *)realloc(buf, bufsize)) == NULL)
     62 				err(1, "Unable to allocate %ld bytes of memory",
     63 				    (long)bufsize);
     64 		}
     65 		strcpy(buf + buflen, p);
     66 		buflen += l;
     67 		if (p[l - 1] == '\n')
     68 			break;
     69 	}
     70 	if (p == NULL && *buf == '\0')
     71 		return (NULL);
     72 	*len = strlen(buf);
     73 	return (buf);
     74 }
     75 #endif
     76