aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2015-11-07 20:52:52 +0000
committerIngo Schwarze <schwarze@openbsd.org>2015-11-07 20:52:52 +0000
commit91090b741273a8ad6061114d369045a4b16a580c (patch)
tree094288d07d953ce24e6d9eeccc5d4350f43a6c6e
parent52461cacec49af96b6b0643f4322e9fc0b36d403 (diff)
downloadmandoc-91090b741273a8ad6061114d369045a4b16a580c.tar.gz
mandoc-91090b741273a8ad6061114d369045a4b16a580c.tar.zst
mandoc-91090b741273a8ad6061114d369045a4b16a580c.zip
provide a simple stand-alone implementation of getline(3)
for systems lacking it
-rw-r--r--Makefile5
-rw-r--r--Makefile.depend1
-rw-r--r--compat_getline.c68
-rwxr-xr-xconfigure9
-rw-r--r--test-getline.c13
5 files changed, 94 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index fa3cee7c..07972261 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.477 2015/11/07 17:58:55 schwarze Exp $
+# $Id: Makefile,v 1.478 2015/11/07 20:52:52 schwarze Exp $
#
# Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
# Copyright (c) 2011, 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
@@ -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..aed4754f
--- /dev/null
+++ b/compat_getline.c
@@ -0,0 +1,68 @@
+#include "config.h"
+
+#if HAVE_GETLINE
+
+int dummy;
+
+#else
+
+/* $Id: compat_getline.c,v 1.1 2015/11/07 20:52:52 schwarze Exp $ */
+/*
+ * 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
diff --git a/configure b/configure
index 108a4e58..afc916f5 100755
--- a/configure
+++ b/configure
@@ -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;
+}