fgetln.c revision 1.5 1 1.5 lukem /* $NetBSD: fgetln.c,v 1.5 2003/10/27 00:12:43 lukem Exp $ */
2 1.1 lukem
3 1.1 lukem /*
4 1.4 salo * Copyright 1999 Luke Mewburn <lukem (at) NetBSD.org>.
5 1.1 lukem * All rights reserved.
6 1.1 lukem *
7 1.1 lukem * Redistribution and use in source and binary forms, with or without
8 1.1 lukem * modification, are permitted provided that the following conditions
9 1.1 lukem * are met:
10 1.1 lukem * 1. Redistributions of source code must retain the above copyright
11 1.1 lukem * notice, this list of conditions and the following disclaimer.
12 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 lukem * notice, this list of conditions and the following disclaimer in the
14 1.1 lukem * documentation and/or other materials provided with the distribution.
15 1.1 lukem * 3. The name of the author may not be used to endorse or promote products
16 1.1 lukem * derived from this software without specific prior written permission.
17 1.1 lukem *
18 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 lukem * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 lukem * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 lukem * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 lukem * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.1 lukem * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24 1.1 lukem * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 1.1 lukem * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
26 1.1 lukem * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
27 1.1 lukem * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 lukem */
29 1.1 lukem
30 1.5 lukem #include "nbtool_config.h"
31 1.1 lukem
32 1.3 tv #if !HAVE_FGETLN
33 1.2 tv #include <stdlib.h>
34 1.1 lukem
35 1.1 lukem #define BUFCHUNKS BUFSIZ
36 1.1 lukem
37 1.1 lukem char *
38 1.1 lukem fgetln(FILE *fp, size_t *len)
39 1.1 lukem {
40 1.1 lukem static char *buf;
41 1.1 lukem static size_t bufsize;
42 1.1 lukem size_t buflen;
43 1.1 lukem char curbuf[BUFCHUNKS];
44 1.1 lukem char *p;
45 1.1 lukem
46 1.1 lukem if (buf == NULL) {
47 1.1 lukem bufsize = BUFCHUNKS;
48 1.1 lukem buf = (char *)malloc(bufsize);
49 1.1 lukem if (buf == NULL)
50 1.1 lukem err(1, "Unable to allocate buffer for fgetln()");
51 1.1 lukem }
52 1.1 lukem
53 1.1 lukem *buf = '\0';
54 1.1 lukem buflen = 0;
55 1.1 lukem while ((p = fgets(curbuf, sizeof(curbuf), fp)) != NULL) {
56 1.1 lukem size_t l;
57 1.1 lukem
58 1.1 lukem l = strlen(p);
59 1.1 lukem if (bufsize < buflen + l) {
60 1.1 lukem bufsize += BUFCHUNKS;
61 1.1 lukem if ((buf = (char *)realloc(buf, bufsize)) == NULL)
62 1.1 lukem err(1, "Unable to allocate %ld bytes of memory",
63 1.1 lukem (long)bufsize);
64 1.1 lukem }
65 1.1 lukem strcpy(buf + buflen, p);
66 1.1 lukem buflen += l;
67 1.1 lukem if (p[l - 1] == '\n')
68 1.1 lukem break;
69 1.1 lukem }
70 1.1 lukem if (p == NULL && *buf == '\0')
71 1.1 lukem return (NULL);
72 1.1 lukem *len = strlen(buf);
73 1.1 lukem return (buf);
74 1.1 lukem }
75 1.3 tv #endif
76