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