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