source@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mdocml: Modernization, no functional change intended: Use the POSIX
@ 2015-11-07 17:59 schwarze
  0 siblings, 0 replies; only message in thread
From: schwarze @ 2015-11-07 17:59 UTC (permalink / raw)
  To: source

Log Message:
-----------
Modernization, no functional change intended:
Use the POSIX function getline(3) rather than the slightly
dangerous BSD function fgetln(3).
Remove the related compatibility code.

Modified Files:
--------------
    mdocml:
        LICENSE
        Makefile
        Makefile.depend
        cgi.c
        configure
        main.c
        mandocdb.c
        manpage.c
        manpath.c

Removed Files:
-------------
    mdocml:
        compat_fgetln.c
        test-fgetln.c

Revision Data
-------------
Index: Makefile
===================================================================
RCS file: /home/cvs/mdocml/mdocml/Makefile,v
retrieving revision 1.476
retrieving revision 1.477
diff -LMakefile -LMakefile -u -p -r1.476 -r1.477
--- Makefile
+++ Makefile
@@ -21,7 +21,6 @@ VERSION = 1.13.3
 
 TESTSRCS	 = test-dirent-namlen.c \
 		   test-err.c \
-		   test-fgetln.c \
 		   test-fts.c \
 		   test-getsubopt.c \
 		   test-isblank.c \
@@ -47,7 +46,6 @@ SRCS		 = att.c \
 		   cgi.c \
 		   chars.c \
 		   compat_err.c \
-		   compat_fgetln.c \
 		   compat_fts.c \
 		   compat_getsubopt.c \
 		   compat_isblank.c \
@@ -207,7 +205,6 @@ LIBMANDOC_OBJS	 = $(LIBMAN_OBJS) \
 		   read.o
 
 COMPAT_OBJS	 = compat_err.o \
-		   compat_fgetln.o \
 		   compat_fts.o \
 		   compat_getsubopt.o \
 		   compat_isblank.o \
Index: manpage.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/manpage.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -Lmanpage.c -Lmanpage.c -u -p -r1.12 -r1.13
--- manpage.c
+++ manpage.c
@@ -37,10 +37,11 @@ int
 main(int argc, char *argv[])
 {
 	int		 ch, term;
-	size_t		 i, sz, len;
+	size_t		 i, sz, linesz;
+	ssize_t		 len;
 	struct mansearch search;
 	struct manpage	*res;
-	char		*conf_file, *defpaths, *auxpaths, *cp;
+	char		*conf_file, *defpaths, *auxpaths, *line;
 	char		 buf[PATH_MAX];
 	const char	*cmd;
 	struct manconf	 conf;
@@ -124,12 +125,16 @@ main(int argc, char *argv[])
 	printf("Enter a choice [1]: ");
 	fflush(stdout);
 
-	if (NULL != (cp = fgetln(stdin, &len)))
-		if ('\n' == cp[--len] && len > 0) {
-			cp[len] = '\0';
-			if ((i = atoi(cp)) < 1 || i > sz)
+	line = NULL;
+	linesz = 0;
+	if ((len = getline(&line, &linesz, stdin)) != -1) {
+		if ('\n' == line[--len] && len > 0) {
+			line[len] = '\0';
+			if ((i = atoi(line)) < 1 || i > sz)
 				i = 0;
 		}
+	}
+	free(line);
 
 	if (0 == i) {
 		for (i = 0; i < sz; i++)
--- compat_fgetln.c
+++ /dev/null
@@ -1,94 +0,0 @@
-#include "config.h"
-
-#if HAVE_FGETLN
-
-int dummy;
-
-#else
-
-/*	$NetBSD: fgetln.c,v 1.3 2006/09/25 07:18:17 lukem Exp $	*/
-
-/*-
- * Copyright (c) 1998 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * This code is derived from software contributed to The NetBSD Foundation
- * by Christos Zoulas.
- *
- * 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.
- * 3. Neither the name of The NetBSD Foundation nor the names of its
- *    contributors may be used to endorse or promote products derived
- *    from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
- */
-
-#include <sys/types.h>
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-char *
-fgetln(fp, len)
-	FILE *fp;
-	size_t *len;
-{
-	static char *buf = NULL;
-	static size_t bufsiz = 0;
-	char *ptr;
-
-
-	if (buf == NULL) {
-		bufsiz = BUFSIZ;
-		if ((buf = malloc(bufsiz)) == NULL)
-			return NULL;
-	}
-
-	if (fgets(buf, bufsiz, fp) == NULL)
-		return NULL;
-
-	*len = 0;
-	while ((ptr = strchr(&buf[*len], '\n')) == NULL) {
-		size_t nbufsiz = bufsiz + BUFSIZ;
-		char *nbuf = realloc(buf, nbufsiz);
-
-		if (nbuf == NULL) {
-			int oerrno = errno;
-			free(buf);
-			errno = oerrno;
-			buf = NULL;
-			return NULL;
-		} else
-			buf = nbuf;
-
-		*len = bufsiz;
-		if (fgets(&buf[bufsiz], BUFSIZ, fp) == NULL)
-			return buf;
-
-		bufsiz = nbufsiz;
-	}
-
-	*len = (ptr - buf) + 1;
-	return buf;
-}
-
-#endif
Index: mandocdb.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mandocdb.c,v
retrieving revision 1.208
retrieving revision 1.209
diff -Lmandocdb.c -Lmandocdb.c -u -p -r1.208 -r1.209
--- mandocdb.c
+++ mandocdb.c
@@ -1291,7 +1291,9 @@ parse_cat(struct mpage *mpage, int fd)
 {
 	FILE		*stream;
 	char		*line, *p, *title;
-	size_t		 len, plen, titlesz;
+	size_t		 linesz, plen, titlesz;
+	ssize_t		 len;
+	int		 offs;
 
 	stream = (-1 == fd) ?
 	    fopen(mpage->mlinks->file, "r") :
@@ -1304,10 +1306,13 @@ parse_cat(struct mpage *mpage, int fd)
 		return;
 	}
 
+	line = NULL;
+	linesz = 0;
+
 	/* Skip to first blank line. */
 
-	while (NULL != (line = fgetln(stream, &len)))
-		if ('\n' == *line)
+	while (getline(&line, &linesz, stream) != -1)
+		if (*line == '\n')
 			break;
 
 	/*
@@ -1315,8 +1320,8 @@ parse_cat(struct mpage *mpage, int fd)
 	 * is the first section header.  Skip to it.
 	 */
 
-	while (NULL != (line = fgetln(stream, &len)))
-		if ('\n' != *line && ' ' != *line)
+	while (getline(&line, &linesz, stream) != -1)
+		if (*line != '\n' && *line != ' ')
 			break;
 
 	/*
@@ -1329,20 +1334,20 @@ parse_cat(struct mpage *mpage, int fd)
 	titlesz = 0;
 	title = NULL;
 
-	while (NULL != (line = fgetln(stream, &len))) {
-		if (' ' != *line || '\n' != line[len - 1])
+	while ((len = getline(&line, &linesz, stream)) != -1) {
+		if (*line != ' ')
 			break;
-		while (len > 0 && isspace((unsigned char)*line)) {
-			line++;
-			len--;
-		}
-		if (1 == len)
+		offs = 0;
+		while (isspace((unsigned char)line[offs]))
+			offs++;
+		if (line[offs] == '\0')
 			continue;
-		title = mandoc_realloc(title, titlesz + len);
-		memcpy(title + titlesz, line, len);
-		titlesz += len;
+		title = mandoc_realloc(title, titlesz + len - offs);
+		memcpy(title + titlesz, line + offs, len - offs);
+		titlesz += len - offs;
 		title[titlesz - 1] = ' ';
 	}
+	free(line);
 
 	/*
 	 * If no page content can be found, or the input line
@@ -1360,8 +1365,7 @@ parse_cat(struct mpage *mpage, int fd)
 		return;
 	}
 
-	title = mandoc_realloc(title, titlesz + 1);
-	title[titlesz] = '\0';
+	title[titlesz - 1] = '\0';
 
 	/*
 	 * Skip to the first dash.
Index: main.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/main.c,v
retrieving revision 1.256
retrieving revision 1.257
diff -Lmain.c -Lmain.c -u -p -r1.256 -r1.257
--- main.c
+++ main.c
@@ -774,12 +774,12 @@ passthrough(const char *file, int fd, in
 
 	FILE		*stream;
 	const char	*syscall;
-	char		*line;
-	size_t		 len, off;
-	ssize_t		 nw;
+	char		*line, *cp;
+	size_t		 linesz;
 	int		 print;
 
-	fflush(stdout);
+	line = NULL;
+	linesz = 0;
 
 	if ((stream = fdopen(fd, "r")) == NULL) {
 		close(fd);
@@ -788,45 +788,41 @@ passthrough(const char *file, int fd, in
 	}
 
 	print = 0;
-	while ((line = fgetln(stream, &len)) != NULL) {
+	while (getline(&line, &linesz, stream) != -1) {
+		cp = line;
 		if (synopsis_only) {
 			if (print) {
-				if ( ! isspace((unsigned char)*line))
+				if ( ! isspace((unsigned char)*cp))
 					goto done;
-				while (len &&
-				    isspace((unsigned char)*line)) {
-					line++;
-					len--;
-				}
+				while (isspace((unsigned char)*cp))
+					cp++;
 			} else {
-				if ((len == sizeof(synb) &&
-				     ! strncmp(line, synb, len - 1)) ||
-				    (len == sizeof(synr) &&
-				     ! strncmp(line, synr, len - 1)))
+				if (strcmp(cp, synb) == 0 ||
+				    strcmp(cp, synr) == 0)
 					print = 1;
 				continue;
 			}
 		}
-		for (off = 0; off < len; off += nw)
-			if ((nw = write(STDOUT_FILENO, line + off,
-			    len - off)) == -1 || nw == 0) {
-				fclose(stream);
-				syscall = "write";
-				goto fail;
-			}
+		if (fputs(cp, stdout)) {
+			fclose(stream);
+			syscall = "fputs";
+			goto fail;
+		}
 	}
 
 	if (ferror(stream)) {
 		fclose(stream);
-		syscall = "fgetln";
+		syscall = "getline";
 		goto fail;
 	}
 
 done:
+	free(line);
 	fclose(stream);
 	return;
 
 fail:
+	free(line);
 	warn("%s: SYSERR: %s", file, syscall);
 	if (rc < MANDOCLEVEL_SYSERR)
 		rc = MANDOCLEVEL_SYSERR;
Index: Makefile.depend
===================================================================
RCS file: /home/cvs/mdocml/mdocml/Makefile.depend,v
retrieving revision 1.19
retrieving revision 1.20
diff -LMakefile.depend -LMakefile.depend -u -p -r1.19 -r1.20
--- Makefile.depend
+++ Makefile.depend
@@ -2,12 +2,12 @@ att.o: att.c config.h roff.h mdoc.h libm
 cgi.o: cgi.c config.h mandoc_aux.h mandoc.h roff.h mdoc.h man.h main.h manconf.h mansearch.h cgi.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_fgetln.o: compat_fgetln.c config.h
 compat_fts.o: compat_fts.c config.h compat_fts.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
 compat_ohash.o: compat_ohash.c config.h compat_ohash.h
+compat_progname.o: compat_progname.c config.h
 compat_reallocarray.o: compat_reallocarray.c config.h
 compat_sqlite3_errstr.o: compat_sqlite3_errstr.c config.h
 compat_strcasestr.o: compat_strcasestr.c config.h
Index: configure
===================================================================
RCS file: /home/cvs/mdocml/mdocml/configure,v
retrieving revision 1.33
retrieving revision 1.34
diff -Lconfigure -Lconfigure -u -p -r1.33 -r1.34
--- configure
+++ configure
@@ -44,7 +44,6 @@ BUILD_CGI=0
 
 HAVE_DIRENT_NAMLEN=
 HAVE_ERR=
-HAVE_FGETLN=
 HAVE_FTS=
 HAVE_GETSUBOPT=
 HAVE_ISBLANK=
@@ -176,7 +175,6 @@ runtest() {
 # --- library functions ---
 runtest dirent-namlen	DIRENT_NAMLEN	|| true
 runtest err		ERR		|| true
-runtest fgetln		FGETLN		|| true
 runtest fts		FTS		|| true
 runtest getsubopt	GETSUBOPT	|| true
 runtest isblank		ISBLANK		|| true
@@ -289,11 +287,10 @@ cat << __HEREDOC__
 
 __HEREDOC__
 
-[ ${HAVE_FGETLN} -eq 0 -o ${HAVE_REALLOCARRAY} -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_FGETLN} -eq 0 ] && echo "#include <stdio.h>"
 
 echo
 echo "#define MAN_CONF_FILE \"/etc/${MANM_MANCONF}\""
@@ -304,7 +301,6 @@ echo "#define MANPATH_DEFAULT \"${MANPAT
 cat << __HEREDOC__
 #define HAVE_DIRENT_NAMLEN ${HAVE_DIRENT_NAMLEN}
 #define HAVE_ERR ${HAVE_ERR}
-#define HAVE_FGETLN ${HAVE_FGETLN}
 #define HAVE_FTS ${HAVE_FTS}
 #define HAVE_GETSUBOPT ${HAVE_GETSUBOPT}
 #define HAVE_ISBLANK ${HAVE_ISBLANK}
@@ -342,9 +338,6 @@ if [ ${HAVE_ERR} -eq 0 ]; then
 	echo "extern	void	  warn(const char *, ...);"
 	echo "extern	void	  warnx(const char *, ...);"
 fi
-
-[ ${HAVE_FGETLN} -eq 0 ] && \
-	echo "extern	char	 *fgetln(FILE *, size_t *);"
 
 [ ${HAVE_GETSUBOPT} -eq 0 ] && \
 	echo "extern	int	  getsubopt(char **, char * const *, char **);"
Index: manpath.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/manpath.c,v
retrieving revision 1.28
retrieving revision 1.29
diff -Lmanpath.c -Lmanpath.c -u -p -r1.28 -r1.29
--- manpath.c
+++ manpath.c
@@ -212,14 +212,19 @@ manconf_file(struct manconf *conf, const
 	char manpath_default[] = MANPATH_DEFAULT;
 
 	FILE		*stream;
-	char		*cp, *ep;
-	size_t		 len, tok;
+	char		*line, *cp, *ep;
+	size_t		 linesz, tok, toklen;
+	ssize_t		 linelen;
 
 	if ((stream = fopen(file, "r")) == NULL)
 		goto out;
 
-	while ((cp = fgetln(stream, &len)) != NULL) {
-		ep = cp + len;
+	line = NULL;
+	linesz = 0;
+
+	while ((linelen = getline(&line, &linesz, stream)) != -1) {
+		cp = line;
+		ep = cp + linelen;
 		if (ep[-1] != '\n')
 			break;
 		*--ep = '\0';
@@ -229,11 +234,11 @@ manconf_file(struct manconf *conf, const
 			continue;
 
 		for (tok = 0; tok < sizeof(toks)/sizeof(toks[0]); tok++) {
-			len = strlen(toks[tok]);
-			if (cp + len < ep &&
-			    isspace((unsigned char)cp[len]) &&
-			    !strncmp(cp, toks[tok], len)) {
-				cp += len;
+			toklen = strlen(toks[tok]);
+			if (cp + toklen < ep &&
+			    isspace((unsigned char)cp[toklen]) &&
+			    strncmp(cp, toks[tok], toklen) == 0) {
+				cp += toklen;
 				while (isspace((unsigned char)*cp))
 					cp++;
 				break;
@@ -259,6 +264,7 @@ manconf_file(struct manconf *conf, const
 			break;
 		}
 	}
+	free(line);
 	fclose(stream);
 
 out:
Index: cgi.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/cgi.c,v
retrieving revision 1.114
retrieving revision 1.115
diff -Lcgi.c -Lcgi.c -u -p -r1.114 -r1.115
--- cgi.c
+++ cgi.c
@@ -704,12 +704,13 @@ static void
 catman(const struct req *req, const char *file)
 {
 	FILE		*f;
-	size_t		 len;
-	int		 i;
 	char		*p;
+	size_t		 sz;
+	ssize_t		 len;
+	int		 i;
 	int		 italic, bold;
 
-	if (NULL == (f = fopen(file, "r"))) {
+	if ((f = fopen(file, "r")) == NULL) {
 		puts("<P>You specified an invalid manual file.</P>");
 		return;
 	}
@@ -717,9 +718,12 @@ catman(const struct req *req, const char
 	puts("<DIV CLASS=\"catman\">\n"
 	     "<PRE>");
 
-	while (NULL != (p = fgetln(f, &len))) {
+	p = NULL;
+	sz = 0;
+
+	while ((len = getline(&p, &sz, f)) != -1) {
 		bold = italic = 0;
-		for (i = 0; i < (int)len - 1; i++) {
+		for (i = 0; i < len - 1; i++) {
 			/*
 			 * This means that the catpage is out of state.
 			 * Ignore it and keep going (although the
@@ -744,7 +748,7 @@ catman(const struct req *req, const char
 				italic = bold = 0;
 				html_putchar(p[i]);
 				continue;
-			} else if (i + 2 >= (int)len)
+			} else if (i + 2 >= len)
 				continue;
 
 			/* Italic mode. */
@@ -820,11 +824,12 @@ catman(const struct req *req, const char
 		if (bold)
 			printf("</B>");
 
-		if (i == (int)len - 1 && '\n' != p[i])
+		if (i == len - 1 && p[i] != '\n')
 			html_putchar(p[i]);
 
 		putchar('\n');
 	}
+	free(p);
 
 	puts("</PRE>\n"
 	     "</DIV>");
@@ -1134,6 +1139,7 @@ pathgen(struct req *req)
 	FILE	*fp;
 	char	*dp;
 	size_t	 dpsz;
+	ssize_t	 len;
 
 	if (NULL == (fp = fopen("manpath.conf", "r"))) {
 		fprintf(stderr, "%s/manpath.conf: %s\n",
@@ -1142,12 +1148,14 @@ pathgen(struct req *req)
 		exit(EXIT_FAILURE);
 	}
 
-	while (NULL != (dp = fgetln(fp, &dpsz))) {
-		if ('\n' == dp[dpsz - 1])
-			dpsz--;
+	dp = NULL;
+	dpsz = 0;
+
+	while ((len = getline(&dp, &dpsz, fp)) != -1) {
+		if (dp[len - 1] == '\n')
+			dp[--len] = '\0';
 		req->p = mandoc_realloc(req->p,
 		    (req->psz + 1) * sizeof(char *));
-		dp = mandoc_strndup(dp, dpsz);
 		if ( ! validate_urifrag(dp)) {
 			fprintf(stderr, "%s/manpath.conf contains "
 			    "unsafe path \"%s\"\n", MAN_DIR, dp);
@@ -1161,7 +1169,10 @@ pathgen(struct req *req)
 			exit(EXIT_FAILURE);
 		}
 		req->p[req->psz++] = dp;
+		dp = NULL;
+		dpsz = 0;
 	}
+	free(dp);
 
 	if ( req->p == NULL ) {
 		fprintf(stderr, "%s/manpath.conf is empty\n", MAN_DIR);
--- test-fgetln.c
+++ /dev/null
@@ -1,11 +0,0 @@
-#include <sys/types.h>
-#include <stdio.h>
-#include <unistd.h>
-
-int
-main(void)
-{
-	size_t sz;
-	fclose(stdin);
-	return fgetln(stdin, &sz) != NULL;
-}
Index: LICENSE
===================================================================
RCS file: /home/cvs/mdocml/mdocml/LICENSE,v
retrieving revision 1.10
retrieving revision 1.11
diff -LLICENSE -LLICENSE -u -p -r1.10 -r1.11
--- LICENSE
+++ LICENSE
@@ -48,8 +48,5 @@ compat_getsubopt.c, compat_strcasestr.c,
 man.1:
 Copyright (c) 1989,1990,1993,1994 The Regents of the University of California
 
-compat_fgetln.c:
-Copyright (c) 1998 The NetBSD Foundation, Inc.
-
 compat_stringlist.c, compat_stringlist.h:
 Copyright (c) 1994 Christos Zoulas <christos@netbsd.org>
--
 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 17:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-07 17:59 mdocml: Modernization, no functional change intended: Use the POSIX 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).