Home | History | Annotate | Line # | Download | only in compat
fgetln.c revision 1.7
      1  1.7  christos /*	$NetBSD: fgetln.c,v 1.7 2006/08/26 16:24:29 christos Exp $	*/
      2  1.1     lukem 
      3  1.6  christos /*-
      4  1.6  christos  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1     lukem  * All rights reserved.
      6  1.1     lukem  *
      7  1.6  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.6  christos  * by Christos Zoulas.
      9  1.6  christos  *
     10  1.1     lukem  * Redistribution and use in source and binary forms, with or without
     11  1.1     lukem  * modification, are permitted provided that the following conditions
     12  1.1     lukem  * are met:
     13  1.1     lukem  * 1. Redistributions of source code must retain the above copyright
     14  1.1     lukem  *    notice, this list of conditions and the following disclaimer.
     15  1.1     lukem  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     lukem  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     lukem  *    documentation and/or other materials provided with the distribution.
     18  1.7  christos  * 3. Neither the name of The NetBSD Foundation nor the names of its
     19  1.6  christos  *    contributors may be used to endorse or promote products derived
     20  1.6  christos  *    from this software without specific prior written permission.
     21  1.1     lukem  *
     22  1.6  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  1.6  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  1.6  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  1.6  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  1.6  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  1.6  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  1.6  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  1.6  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  1.6  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  1.6  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  1.6  christos  * POSSIBILITY OF SUCH DAMAGE.
     33  1.1     lukem  */
     34  1.1     lukem 
     35  1.5     lukem #include "nbtool_config.h"
     36  1.1     lukem 
     37  1.3        tv #if !HAVE_FGETLN
     38  1.2        tv #include <stdlib.h>
     39  1.6  christos #ifdef notdef
     40  1.6  christos /* These headers are required, but included from nbtool_config.h */
     41  1.6  christos #include <stdio.h>
     42  1.6  christos #include <unistd.h>
     43  1.6  christos #include <errno.h>
     44  1.6  christos #include <string.h>
     45  1.6  christos #endif
     46  1.1     lukem 
     47  1.1     lukem char *
     48  1.1     lukem fgetln(FILE *fp, size_t *len)
     49  1.1     lukem {
     50  1.6  christos 	static char *buf = NULL;
     51  1.6  christos 	static size_t bufsiz = 0;
     52  1.6  christos 	char *ptr;
     53  1.6  christos 
     54  1.1     lukem 
     55  1.1     lukem 	if (buf == NULL) {
     56  1.6  christos 		bufsiz = BUFSIZ;
     57  1.6  christos 		if ((buf = malloc(bufsiz)) == NULL)
     58  1.6  christos 			return NULL;
     59  1.1     lukem 	}
     60  1.1     lukem 
     61  1.6  christos 	if (fgets(buf, bufsiz, fp) == NULL)
     62  1.6  christos 		return NULL;
     63  1.6  christos 
     64  1.6  christos 	*len = 0;
     65  1.6  christos 	while ((ptr = strchr(&buf[*len], '\n')) == NULL) {
     66  1.6  christos 		size_t nbufsiz = bufsiz + BUFSIZ;
     67  1.6  christos 		char *nbuf = realloc(buf, nbufsiz);
     68  1.6  christos 
     69  1.6  christos 		if (nbuf == NULL) {
     70  1.6  christos 			int oerrno = errno;
     71  1.6  christos 			free(buf);
     72  1.6  christos 			errno = oerrno;
     73  1.6  christos 			buf = NULL;
     74  1.6  christos 			return NULL;
     75  1.6  christos 		} else
     76  1.6  christos 			buf = nbuf;
     77  1.6  christos 
     78  1.6  christos 		*len = bufsiz;
     79  1.6  christos 		if (fgets(&buf[bufsiz], BUFSIZ, fp) == NULL)
     80  1.6  christos 			return buf;
     81  1.6  christos 
     82  1.6  christos 		bufsiz = nbufsiz;
     83  1.1     lukem 	}
     84  1.6  christos 
     85  1.6  christos 	*len = (ptr - buf) + 1;
     86  1.6  christos 	return buf;
     87  1.1     lukem }
     88  1.6  christos 
     89  1.3        tv #endif
     90