1241675Suqs#ifdef HAVE_CONFIG_H 2241675Suqs#include "config.h" 3241675Suqs#endif 4241675Suqs 5241675Suqs#ifdef HAVE_FGETLN 6241675Suqs 7241675Suqsint dummy; 8241675Suqs 9241675Suqs#else 10241675Suqs 11241675Suqs/* $NetBSD: fgetln.c,v 1.3 2006/09/25 07:18:17 lukem Exp $ */ 12241675Suqs 13241675Suqs/*- 14241675Suqs * Copyright (c) 1998 The NetBSD Foundation, Inc. 15241675Suqs * All rights reserved. 16241675Suqs * 17241675Suqs * This code is derived from software contributed to The NetBSD Foundation 18241675Suqs * by Christos Zoulas. 19241675Suqs * 20241675Suqs * Redistribution and use in source and binary forms, with or without 21241675Suqs * modification, are permitted provided that the following conditions 22241675Suqs * are met: 23241675Suqs * 1. Redistributions of source code must retain the above copyright 24241675Suqs * notice, this list of conditions and the following disclaimer. 25241675Suqs * 2. Redistributions in binary form must reproduce the above copyright 26241675Suqs * notice, this list of conditions and the following disclaimer in the 27241675Suqs * documentation and/or other materials provided with the distribution. 28241675Suqs * 3. Neither the name of The NetBSD Foundation nor the names of its 29241675Suqs * contributors may be used to endorse or promote products derived 30241675Suqs * from this software without specific prior written permission. 31241675Suqs * 32241675Suqs * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 33241675Suqs * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 34241675Suqs * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 35241675Suqs * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 36241675Suqs * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 37241675Suqs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 38241675Suqs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 39241675Suqs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 40241675Suqs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 41241675Suqs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 42241675Suqs * POSSIBILITY OF SUCH DAMAGE. 43241675Suqs */ 44241675Suqs#include <errno.h> 45241675Suqs#include <stdio.h> 46241675Suqs#include <stdlib.h> 47241675Suqs#include <string.h> 48241675Suqs 49241675Suqschar * 50241675Suqsfgetln(fp, len) 51241675Suqs FILE *fp; 52241675Suqs size_t *len; 53241675Suqs{ 54241675Suqs static char *buf = NULL; 55241675Suqs static size_t bufsiz = 0; 56241675Suqs char *ptr; 57241675Suqs 58241675Suqs 59241675Suqs if (buf == NULL) { 60241675Suqs bufsiz = BUFSIZ; 61241675Suqs if ((buf = malloc(bufsiz)) == NULL) 62241675Suqs return NULL; 63241675Suqs } 64241675Suqs 65241675Suqs if (fgets(buf, bufsiz, fp) == NULL) 66241675Suqs return NULL; 67241675Suqs 68241675Suqs *len = 0; 69241675Suqs while ((ptr = strchr(&buf[*len], '\n')) == NULL) { 70241675Suqs size_t nbufsiz = bufsiz + BUFSIZ; 71241675Suqs char *nbuf = realloc(buf, nbufsiz); 72241675Suqs 73241675Suqs if (nbuf == NULL) { 74241675Suqs int oerrno = errno; 75241675Suqs free(buf); 76241675Suqs errno = oerrno; 77241675Suqs buf = NULL; 78241675Suqs return NULL; 79241675Suqs } else 80241675Suqs buf = nbuf; 81241675Suqs 82241675Suqs *len = bufsiz; 83241675Suqs if (fgets(&buf[bufsiz], BUFSIZ, fp) == NULL) 84241675Suqs return buf; 85241675Suqs 86241675Suqs bufsiz = nbufsiz; 87241675Suqs } 88241675Suqs 89241675Suqs *len = (ptr - buf) + 1; 90241675Suqs return buf; 91241675Suqs} 92241675Suqs 93241675Suqs#endif 94