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