diff options
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | Makefile.depend | 1 | ||||
-rw-r--r-- | compat_getline.c | 68 | ||||
-rwxr-xr-x | configure | 9 | ||||
-rw-r--r-- | test-getline.c | 13 |
5 files changed, 93 insertions, 1 deletions
@@ -22,6 +22,7 @@ VERSION = 1.13.3 TESTSRCS = test-dirent-namlen.c \ test-err.c \ test-fts.c \ + test-getline.c \ test-getsubopt.c \ test-isblank.c \ test-mkdtemp.c \ @@ -47,6 +48,7 @@ SRCS = att.c \ chars.c \ compat_err.c \ compat_fts.c \ + compat_getline.c \ compat_getsubopt.c \ compat_isblank.c \ compat_mkdtemp.c \ @@ -206,6 +208,7 @@ LIBMANDOC_OBJS = $(LIBMAN_OBJS) \ COMPAT_OBJS = compat_err.o \ compat_fts.o \ + compat_getline.o \ compat_getsubopt.o \ compat_isblank.o \ compat_mkdtemp.o \ diff --git a/Makefile.depend b/Makefile.depend index 003edeb1..98a1928b 100644 --- a/Makefile.depend +++ b/Makefile.depend @@ -3,6 +3,7 @@ cgi.o: cgi.c config.h mandoc_aux.h mandoc.h roff.h mdoc.h man.h main.h manconf.h chars.o: chars.c config.h mandoc.h mandoc_aux.h mandoc_ohash.h compat_ohash.h libmandoc.h compat_err.o: compat_err.c config.h compat_fts.o: compat_fts.c config.h compat_fts.h +compat_getline.o: compat_getline.c config.h compat_getsubopt.o: compat_getsubopt.c config.h compat_isblank.o: compat_isblank.c config.h compat_mkdtemp.o: compat_mkdtemp.c config.h diff --git a/compat_getline.c b/compat_getline.c new file mode 100644 index 00000000..94078ddb --- /dev/null +++ b/compat_getline.c @@ -0,0 +1,68 @@ +#include "config.h" + +#if HAVE_GETLINE + +int dummy; + +#else + +/* $Id$ */ +/* + * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org> + * + * 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. + * + * 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. + */ + +#include <sys/types.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> + +ssize_t +getline(char **buf, size_t *bufsz, FILE *fp) +{ + char *nbuf; + size_t nbufsz, pos; + int c; + + if (buf == NULL || bufsz == NULL) { + errno = EINVAL; + return -1; + } + + if (*buf == NULL) + *bufsz = 0; + else + **buf = '\0'; + + pos = 0; + for (;;) { + if (pos + 1 >= *bufsz) { + nbufsz = *bufsz ? *bufsz * 2 : BUFSIZ; + if ((nbuf = realloc(*buf, nbufsz)) == NULL) + return -1; + *buf = nbuf; + *bufsz = nbufsz; + } + if ((c = fgetc(fp)) == EOF) { + (*buf)[pos] = '\0'; + return pos > 0 && feof(fp) ? (ssize_t)pos : -1; + } + (*buf)[pos++] = c; + (*buf)[pos] = '\0'; + if (c == '\n') + return pos; + } +} + +#endif @@ -45,6 +45,7 @@ BUILD_CGI=0 HAVE_DIRENT_NAMLEN= HAVE_ERR= HAVE_FTS= +HAVE_GETLINE= HAVE_GETSUBOPT= HAVE_ISBLANK= HAVE_MKDTEMP= @@ -176,6 +177,7 @@ runtest() { runtest dirent-namlen DIRENT_NAMLEN || true runtest err ERR || true runtest fts FTS || true +runtest getline GETLINE || true runtest getsubopt GETSUBOPT || true runtest isblank ISBLANK || true runtest mkdtemp MKDTEMP || true @@ -287,10 +289,11 @@ cat << __HEREDOC__ __HEREDOC__ -[ ${HAVE_REALLOCARRAY} -eq 0 -o \ +[ ${HAVE_GETLINE} -eq 0 -o ${HAVE_REALLOCARRAY} -eq 0 -o \ ${HAVE_STRLCAT} -eq 0 -o ${HAVE_STRLCPY} -eq 0 ] \ && echo "#include <sys/types.h>" [ ${HAVE_VASPRINTF} -eq 0 ] && echo "#include <stdarg.h>" +[ ${HAVE_GETLINE} -eq 0 ] && echo "#include <stdio.h>" echo echo "#define MAN_CONF_FILE \"/etc/${MANM_MANCONF}\"" @@ -302,6 +305,7 @@ cat << __HEREDOC__ #define HAVE_DIRENT_NAMLEN ${HAVE_DIRENT_NAMLEN} #define HAVE_ERR ${HAVE_ERR} #define HAVE_FTS ${HAVE_FTS} +#define HAVE_GETLINE ${HAVE_GETLINE} #define HAVE_GETSUBOPT ${HAVE_GETSUBOPT} #define HAVE_ISBLANK ${HAVE_ISBLANK} #define HAVE_MKDTEMP ${HAVE_MKDTEMP} @@ -339,6 +343,9 @@ if [ ${HAVE_ERR} -eq 0 ]; then echo "extern void warnx(const char *, ...);" fi +[ ${HAVE_GETLINE} -eq 0 ] && \ + echo "extern ssize_t getline(char **, size_t *, FILE *);" + [ ${HAVE_GETSUBOPT} -eq 0 ] && \ echo "extern int getsubopt(char **, char * const *, char **);" diff --git a/test-getline.c b/test-getline.c new file mode 100644 index 00000000..d05df217 --- /dev/null +++ b/test-getline.c @@ -0,0 +1,13 @@ +#include <sys/types.h> +#include <stdio.h> +#include <unistd.h> + +int +main(void) +{ + char *line = NULL; + size_t linesz = 0; + + fclose(stdin); + return getline(&line, &linesz, stdin) != -1; +} |