diff options
author | 2015-11-07 17:58:55 +0000 | |
---|---|---|
committer | 2015-11-07 17:58:55 +0000 | |
commit | 52461cacec49af96b6b0643f4322e9fc0b36d403 (patch) | |
tree | aaf6acf44516521f4f6604b822aecfff87e96d54 /main.c | |
parent | b897019f5696c540f454bcb428d44ad31ef65559 (diff) | |
download | mandoc-52461cacec49af96b6b0643f4322e9fc0b36d403.tar.gz mandoc-52461cacec49af96b6b0643f4322e9fc0b36d403.tar.zst mandoc-52461cacec49af96b6b0643f4322e9fc0b36d403.zip |
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.
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 44 |
1 files changed, 20 insertions, 24 deletions
@@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.256 2015/11/07 14:22:29 schwarze Exp $ */ +/* $Id: main.c,v 1.257 2015/11/07 17:58:55 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv> * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org> @@ -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; |