1 /* $NetBSD: fgetln.c,v 1.12 2015/10/09 14:42:40 christos Exp $ */
4 * Copyright (c) 2015 Joerg Jung <jung@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * portable fgetln() version
23 #ifdef HAVE_NBTOOL_CONFIG_H
24 #include "nbtool_config.h"
29 #ifndef HAVE_NBTOOL_CONFIG_H
30 /* These headers are required, but included from nbtool_config.h */
37 fgetln(FILE *fp
, size_t *len
)
39 static char *buf
= NULL
;
40 static size_t bufsz
= 0;
50 if (!(buf
= calloc(1, BUFSIZ
)))
54 while ((c
= getc(fp
)) != EOF
) {
58 * Original uses reallocarray() but we don't have it
61 if (!(p
= realloc(buf
, 2 * bufsz
))) {
65 buf
= NULL
, bufsz
= 0;
68 buf
= p
, bufsz
= 2 * bufsz
;
73 return (*len
= r
) ? buf
: NULL
;
80 main(int argc
, char *argv
[])
85 while ((p
= fgetln(stdin
, &len
)) != NULL
) {
86 (void)printf("%zu %s", len
, p
);