]> diplodocus.org Git - mdfrm/blob - fgetln.c
098216f6efc60ea65595fa78620aaf1b723667af
[mdfrm] / fgetln.c
1 /* fgetln.c -- Portable fgetln(3) implementation from OpenBSD.
2 *
3 * $Id$
4 */
5
6 #include <sys/types.h>
7
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 /* $OpenBSD: memory.c,v 1.1 2001/05/23 12:34:47 espie Exp $ */
14
15 /*
16 * Copyright (c) 1988, 1989, 1990, 1993
17 * The Regents of the University of California. All rights reserved.
18 * Copyright (c) 1989 by Berkeley Softworks
19 * All rights reserved.
20 *
21 * This code is derived from software contributed to Berkeley by
22 * Adam de Boor.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in the
31 * documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 * must display the following acknowledgement:
34 * This product includes software developed by the University of
35 * California, Berkeley and its contributors.
36 * 4. Neither the name of the University nor the names of its contributors
37 * may be used to endorse or promote products derived from this software
38 * without specific prior written permission.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 */
52
53 /*
54 * enomem --
55 * die when out of memory.
56 */
57 void
58 enomem(size)
59 size_t size;
60 {
61 fprintf(stderr, "make: %s (%lu)\n", strerror(errno), (u_long)size);
62 exit(2);
63 }
64
65 /*
66 * erealloc --
67 * realloc, but die on error.
68 */
69 static void *
70 erealloc(ptr, size)
71 void *ptr;
72 size_t size;
73 {
74 if ((ptr = realloc(ptr, size)) == NULL)
75 enomem(size);
76 return ptr;
77 }
78
79 /*
80 * emalloc --
81 * malloc, but die on error.
82 */
83 static void *
84 emalloc(len)
85 size_t len;
86 {
87 void *p;
88
89 if ((p = malloc(len)) == NULL)
90 enomem(len);
91 return p;
92 }
93
94 /* $OpenBSD: util.c,v 1.18 2002/05/29 09:23:25 deraadt Exp $ */
95
96 /*
97 * Copyright (c) 2001 Marc Espie.
98 *
99 * Redistribution and use in source and binary forms, with or without
100 * modification, are permitted provided that the following conditions
101 * are met:
102 * 1. Redistributions of source code must retain the above copyright
103 * notice, this list of conditions and the following disclaimer.
104 * 2. Redistributions in binary form must reproduce the above copyright
105 * notice, this list of conditions and the following disclaimer in the
106 * documentation and/or other materials provided with the distribution.
107 *
108 * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
109 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
110 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
111 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
112 * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
113 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
114 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
115 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
116 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
117 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
118 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
119 */
120
121 char *
122 fgetln(stream, len)
123 FILE *stream;
124 size_t *len;
125 {
126 static char *buffer = NULL;
127 static size_t buflen = 0;
128
129 if (buflen == 0) {
130 buflen = 512;
131 buffer = emalloc(buflen+1);
132 }
133 if (fgets(buffer, buflen+1, stream) == NULL)
134 return NULL;
135 *len = strlen(buffer);
136 while (*len == buflen && buffer[*len-1] != '\n') {
137 buffer = erealloc(buffer, 2*buflen + 1);
138 if (fgets(buffer + buflen, buflen + 1, stream) == NULL)
139 return NULL;
140 *len += strlen(buffer + buflen);
141 buflen *= 2;
142 }
143 return buffer;
144 }