1 1.12 christos /* $NetBSD: fgetln.c,v 1.12 2015/10/09 14:42:40 christos Exp $ */ 2 1.1 lukem 3 1.10 christos /* 4 1.10 christos * Copyright (c) 2015 Joerg Jung <jung (at) openbsd.org> 5 1.1 lukem * 6 1.10 christos * Permission to use, copy, modify, and distribute this software for any 7 1.10 christos * purpose with or without fee is hereby granted, provided that the above 8 1.10 christos * copyright notice and this permission notice appear in all copies. 9 1.6 christos * 10 1.10 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 1.10 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 1.10 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 1.10 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 1.10 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 1.10 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 1.10 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 1.10 christos */ 18 1.10 christos 19 1.10 christos /* 20 1.11 christos * portable fgetln() version 21 1.1 lukem */ 22 1.1 lukem 23 1.8 christos #ifdef HAVE_NBTOOL_CONFIG_H 24 1.5 lukem #include "nbtool_config.h" 25 1.8 christos #endif 26 1.1 lukem 27 1.3 tv #if !HAVE_FGETLN 28 1.2 tv #include <stdlib.h> 29 1.8 christos #ifndef HAVE_NBTOOL_CONFIG_H 30 1.6 christos /* These headers are required, but included from nbtool_config.h */ 31 1.6 christos #include <stdio.h> 32 1.10 christos #include <stdlib.h> 33 1.6 christos #include <errno.h> 34 1.6 christos #endif 35 1.1 lukem 36 1.1 lukem char * 37 1.1 lukem fgetln(FILE *fp, size_t *len) 38 1.1 lukem { 39 1.6 christos static char *buf = NULL; 40 1.10 christos static size_t bufsz = 0; 41 1.10 christos size_t r = 0; 42 1.10 christos char *p; 43 1.10 christos int c, e; 44 1.6 christos 45 1.10 christos if (!fp || !len) { 46 1.10 christos errno = EINVAL; 47 1.10 christos return NULL; 48 1.10 christos } 49 1.10 christos if (!buf) { 50 1.10 christos if (!(buf = calloc(1, BUFSIZ))) 51 1.6 christos return NULL; 52 1.10 christos bufsz = BUFSIZ; 53 1.1 lukem } 54 1.10 christos while ((c = getc(fp)) != EOF) { 55 1.10 christos buf[r++] = c; 56 1.10 christos if (r == bufsz) { 57 1.12 christos /* 58 1.12 christos * Original uses reallocarray() but we don't have it 59 1.12 christos * in tools. 60 1.12 christos */ 61 1.10 christos if (!(p = realloc(buf, 2 * bufsz))) { 62 1.10 christos e = errno; 63 1.10 christos free(buf); 64 1.10 christos errno = e; 65 1.10 christos buf = NULL, bufsz = 0; 66 1.10 christos return NULL; 67 1.10 christos } 68 1.10 christos buf = p, bufsz = 2 * bufsz; 69 1.8 christos } 70 1.10 christos if (c == '\n') 71 1.10 christos break; 72 1.1 lukem } 73 1.10 christos return (*len = r) ? buf : NULL; 74 1.1 lukem } 75 1.10 christos #endif 76 1.6 christos 77 1.8 christos 78 1.8 christos #ifdef TEST 79 1.8 christos int 80 1.8 christos main(int argc, char *argv[]) 81 1.8 christos { 82 1.8 christos char *p; 83 1.8 christos size_t len; 84 1.8 christos 85 1.8 christos while ((p = fgetln(stdin, &len)) != NULL) { 86 1.8 christos (void)printf("%zu %s", len, p); 87 1.8 christos free(p); 88 1.8 christos } 89 1.8 christos return 0; 90 1.8 christos } 91 1.8 christos #endif 92