=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/mandoc/main.c,v retrieving revision 1.128 retrieving revision 1.129 diff -u -r1.128 -r1.129 --- src/usr.bin/mandoc/main.c 2015/03/09 21:30:27 1.128 +++ src/usr.bin/mandoc/main.c 2015/03/10 03:00:48 1.129 @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.128 2015/03/09 21:30:27 schwarze Exp $ */ +/* $OpenBSD: main.c,v 1.129 2015/03/10 03:00:48 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze @@ -19,6 +19,7 @@ #include #include /* MACHINE */ +#include #include #include @@ -458,6 +459,21 @@ free(defos); + /* + * Flush the output and signal end of file. + * If a pager is attached, it allows browsing to the end. + * Otherwise, it does no harm, we are about to exit anyway. + */ + + fclose(stdout); + + /* + * If we spawned a pager, wait for the user to close it. + * Otherwise, this call fails with no adverse effect. + */ + + wait(NULL); + return((int)rc); } @@ -926,18 +942,19 @@ progname, strerror(errno)); exit((int)MANDOCLEVEL_SYSERR); case 0: + break; + default: close(fildes[0]); if (dup2(fildes[1], STDOUT_FILENO) == -1) { fprintf(stderr, "%s: dup output: %s\n", progname, strerror(errno)); exit((int)MANDOCLEVEL_SYSERR); } + close(fildes[1]); return; - default: - break; } - /* The original process becomes the pager. */ + /* The child process becomes the pager. */ close(fildes[1]); if (dup2(fildes[0], STDIN_FILENO) == -1) { @@ -945,6 +962,7 @@ progname, strerror(errno)); exit((int)MANDOCLEVEL_SYSERR); } + close(fildes[0]); pager = getenv("MANPAGER"); if (pager == NULL || *pager == '\0')