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