diff options
author | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2015-11-07 17:58:53 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2015-11-07 17:58:53 +0000 |
commit | c82079ed7ba1f7d607c69beb97ddb4e0dd4ca945 (patch) | |
tree | 46a687a5b80fa7615e42bc22720407588fe21a44 /usr.bin | |
parent | 1b2bcd306c25281d25fb7657ed70bccf481cbe47 (diff) |
Modernization, no functional change intended:
Use the POSIX function getline(3) rather than the slightly
dangerous BSD function fgetln(3).
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/mandoc/cgi.c | 35 | ||||
-rw-r--r-- | usr.bin/mandoc/main.c | 44 | ||||
-rw-r--r-- | usr.bin/mandoc/mandocdb.c | 40 | ||||
-rw-r--r-- | usr.bin/mandoc/manpath.c | 26 |
4 files changed, 81 insertions, 64 deletions
diff --git a/usr.bin/mandoc/cgi.c b/usr.bin/mandoc/cgi.c index 4b9d2b20bb2..d1e49214644 100644 --- a/usr.bin/mandoc/cgi.c +++ b/usr.bin/mandoc/cgi.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cgi.c,v 1.53 2015/11/05 20:55:46 schwarze Exp $ */ +/* $OpenBSD: cgi.c,v 1.54 2015/11/07 17:58:52 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> * Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@usta.de> @@ -702,12 +702,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; } @@ -715,9 +716,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 @@ -742,7 +746,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. */ @@ -818,11 +822,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>"); @@ -1132,6 +1137,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", @@ -1140,12 +1146,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); @@ -1159,7 +1167,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/usr.bin/mandoc/main.c b/usr.bin/mandoc/main.c index d18c61f6af3..8268fb26118 100644 --- a/usr.bin/mandoc/main.c +++ b/usr.bin/mandoc/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.163 2015/11/06 17:23:50 schwarze Exp $ */ +/* $OpenBSD: main.c,v 1.164 2015/11/07 17:58:52 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv> * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org> @@ -729,12 +729,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); @@ -743,45 +743,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; diff --git a/usr.bin/mandoc/mandocdb.c b/usr.bin/mandoc/mandocdb.c index 066cf9fe7c9..d811864373c 100644 --- a/usr.bin/mandoc/mandocdb.c +++ b/usr.bin/mandoc/mandocdb.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mandocdb.c,v 1.161 2015/11/06 16:27:13 schwarze Exp $ */ +/* $OpenBSD: mandocdb.c,v 1.162 2015/11/07 17:58:52 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv> * Copyright (c) 2011-2015 Ingo Schwarze <schwarze@openbsd.org> @@ -1269,7 +1269,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") : @@ -1282,10 +1284,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; /* @@ -1293,8 +1298,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; /* @@ -1307,20 +1312,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 @@ -1338,8 +1343,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. diff --git a/usr.bin/mandoc/manpath.c b/usr.bin/mandoc/manpath.c index 9788e60388a..c04727c94b9 100644 --- a/usr.bin/mandoc/manpath.c +++ b/usr.bin/mandoc/manpath.c @@ -1,4 +1,4 @@ -/* $OpenBSD: manpath.c,v 1.16 2015/10/11 21:06:59 schwarze Exp $ */ +/* $OpenBSD: manpath.c,v 1.17 2015/11/07 17:58:52 schwarze Exp $ */ /* * Copyright (c) 2011, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org> * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv> @@ -161,14 +161,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'; @@ -178,11 +183,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; @@ -208,6 +213,7 @@ manconf_file(struct manconf *conf, const char *file) break; } } + free(line); fclose(stream); out: |