diff options
-rw-r--r-- | LICENSE | 3 | ||||
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | Makefile.depend | 2 | ||||
-rw-r--r-- | cgi.c | 33 | ||||
-rw-r--r-- | compat_fgetln.c | 94 | ||||
-rwxr-xr-x | configure | 9 | ||||
-rw-r--r-- | main.c | 42 | ||||
-rw-r--r-- | mandocdb.c | 38 | ||||
-rw-r--r-- | manpage.c | 17 | ||||
-rw-r--r-- | manpath.c | 24 | ||||
-rw-r--r-- | test-fgetln.c | 11 |
11 files changed, 90 insertions, 186 deletions
@@ -48,8 +48,5 @@ compat_getsubopt.c, compat_strcasestr.c, compat_strsep.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> @@ -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 \ diff --git a/Makefile.depend b/Makefile.depend index ed00dbb9..003edeb1 100644 --- a/Makefile.depend +++ b/Makefile.depend @@ -2,12 +2,12 @@ att.o: att.c config.h roff.h mdoc.h libmdoc.h 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 @@ -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 *file) 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 *file) 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 *file) 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); diff --git a/compat_fgetln.c b/compat_fgetln.c deleted file mode 100644 index 3760ab99..00000000 --- a/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 @@ -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 \"${MANPATH_DEFAULT}\"" 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} @@ -343,9 +339,6 @@ if [ ${HAVE_ERR} -eq 0 ]; then 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 **);" @@ -774,12 +774,12 @@ passthrough(const char *file, int fd, int synopsis_only) 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, int synopsis_only) } 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; @@ -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. @@ -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++) @@ -212,14 +212,19 @@ manconf_file(struct manconf *conf, const char *file) 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 char *file) 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 char *file) break; } } + free(line); fclose(stream); out: diff --git a/test-fgetln.c b/test-fgetln.c deleted file mode 100644 index c2abf06d..00000000 --- a/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; -} |