]>
diplodocus.org Git - mdfrm/blob - fgetln.c
098216f6efc60ea65595fa78620aaf1b723667af
1 /* fgetln.c -- Portable fgetln(3) implementation from OpenBSD.
13 /* $OpenBSD: memory.c,v 1.1 2001/05/23 12:34:47 espie Exp $ */
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.
21 * This code is derived from software contributed to Berkeley by
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
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.
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
55 * die when out of memory.
61 fprintf(stderr
, "make: %s (%lu)\n", strerror(errno
), (u_long
)size
);
67 * realloc, but die on error.
74 if ((ptr
= realloc(ptr
, size
)) == NULL
)
81 * malloc, but die on error.
89 if ((p
= malloc(len
)) == NULL
)
94 /* $OpenBSD: util.c,v 1.18 2002/05/29 09:23:25 deraadt Exp $ */
97 * Copyright (c) 2001 Marc Espie.
99 * Redistribution and use in source and binary forms, with or without
100 * modification, are permitted provided that the following conditions
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.
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.
126 static char *buffer
= NULL
;
127 static size_t buflen
= 0;
131 buffer
= emalloc(buflen
+1);
133 if (fgets(buffer
, buflen
+1, stream
) == 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
)
140 *len
+= strlen(buffer
+ buflen
);