diff options
author | Gilles Chehade <gilles@poolp.org> | 2015-10-01 23:22:07 +0200 |
---|---|---|
committer | Gilles Chehade <gilles@poolp.org> | 2015-10-01 23:22:07 +0200 |
commit | 8abeae51c29328e1e828f3644988af892647d0c1 (patch) | |
tree | 4e2200e8f39a85f59640cefab1c3e6c1efee937a | |
parent | Merge branch 'branch_opensmtpd-5.7.2' into branch_opensmtpd-5.7.2p1 (diff) | |
download | OpenSMTPD-8abeae51c29328e1e828f3644988af892647d0c1.tar.xz OpenSMTPD-8abeae51c29328e1e828f3644988af892647d0c1.zip |
merge Joerg Jung's fgetln()
-rw-r--r-- | openbsd-compat/fgetln.c | 99 |
1 files changed, 50 insertions, 49 deletions
diff --git a/openbsd-compat/fgetln.c b/openbsd-compat/fgetln.c index 4a7d6461..175dca56 100644 --- a/openbsd-compat/fgetln.c +++ b/openbsd-compat/fgetln.c @@ -1,62 +1,63 @@ -/* $OpenPackages$ */ -/* $OpenBSD: util.c,v 1.23 2007/09/17 09:28:36 espie Exp $ */ -/* $NetBSD: util.c,v 1.10 1996/12/31 17:56:04 christos Exp $ */ - /* - * Copyright (c) 2001 Marc Espie. + * Copyright (c) 2015 Joerg Jung <jung@openbsd.org> * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. * - * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD - * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/* OPENBSD ORIGINAL: usr.bin/make/util.c */ - -#include "includes.h" +/* + * portable fgetln() version, NOT reentrant + */ #ifndef HAVE_FGETLN - -#include "xmalloc.h" +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> char * -fgetln(stream, len) - FILE *stream; - size_t *len; +fgetln(FILE *fp, size_t *len) { - static char *buffer = NULL; - static size_t buflen = 0; + static char *buf = NULL; + static size_t bufsz = 0; + size_t r = 0; + char *p, c; + int e; + + if (fp == NULL || len == NULL) { + errno = EINVAL; + return NULL; + } + + if (buf == NULL) { + if ((buf = calloc(1, BUFSIZ)) == NULL) + return NULL; + bufsz = BUFSIZ; + } - if (buflen == 0) { - buflen = 512; - buffer = __xmalloc(buflen+1); - } - if (fgets(buffer, buflen+1, stream) == NULL) - return NULL; - *len = strlen(buffer); - while (*len == buflen && buffer[*len-1] != '\n') { - buffer = __xrealloc(buffer, 1, 2*buflen + 1); - if (fgets(buffer + buflen, buflen + 1, stream) == NULL) - return NULL; - *len += strlen(buffer + buflen); - buflen *= 2; - } - return buffer; + while ((c = getc(fp)) != EOF) { + buf[r++] = c; + if (r == bufsz) { + if (!(p = reallocarray(buf, 2, bufsz))) { + e = errno; + free(buf); + errno = e; + buf = NULL, bufsz = 0; + return NULL; + } + buf = p, bufsz = 2 * bufsz; + } + if (c == '\n') + break; + } + return (*len = r) ? buf : NULL; } #endif |