source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: provide a simple stand-alone implementation of getline(3)  for
@ 2015-11-07 20:53 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2015-11-07 20:53 UTC (permalink / raw)
  To: source

Log Message:
-----------
provide a simple stand-alone implementation of getline(3) 
for systems lacking it

Modified Files:
--------------
    mdocml:
        Makefile
        Makefile.depend
        configure

Added Files:
-----------
    mdocml:
        compat_getline.c
        test-getline.c

Revision Data
-------------
Index: configure
===================================================================
RCS file: /home/cvs/mdocml/mdocml/configure,v
retrieving revision 1.34
retrieving revision 1.35
diff -Lconfigure -Lconfigure -u -p -r1.34 -r1.35
--- configure
+++ 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}
@@ -338,6 +342,9 @@ if [ ${HAVE_ERR} -eq 0 ]; then
 	echo "extern	void	  warn(const char *, ...);"
 	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 **);"
--- /dev/null
+++ 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;
+}
--- /dev/null
+++ 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
Index: Makefile
===================================================================
RCS file: /home/cvs/mdocml/mdocml/Makefile,v
retrieving revision 1.477
retrieving revision 1.478
diff -LMakefile -LMakefile -u -p -r1.477 -r1.478
--- Makefile
+++ Makefile
@@ -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 \
Index: Makefile.depend
===================================================================
RCS file: /home/cvs/mdocml/mdocml/Makefile.depend,v
retrieving revision 1.20
retrieving revision 1.21
diff -LMakefile.depend -LMakefile.depend -u -p -r1.20 -r1.21
--- Makefile.depend
+++ Makefile.depend
@@ -3,6 +3,7 @@ cgi.o: cgi.c config.h mandoc_aux.h mando
 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
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2015-11-07 20:53 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-07 20:53 mdocml: provide a simple stand-alone implementation of getline(3) for schwarze

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).