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