Home | History | Annotate | Line # | Download | only in man
man.c revision 1.9.2.1
      1 /*	$NetBSD: man.c,v 1.9.2.1 1997/11/26 04:22:35 mellon Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1987, 1993, 1994, 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 
     38 #ifndef lint
     39 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994, 1995\n\
     40 	The Regents of the University of California.  All rights reserved.\n");
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 #if 0
     45 static char sccsid[] = "@(#)man.c	8.17 (Berkeley) 1/31/95";
     46 #else
     47 __RCSID("$NetBSD: man.c,v 1.9.2.1 1997/11/26 04:22:35 mellon Exp $");
     48 #endif
     49 #endif /* not lint */
     50 
     51 #include <sys/param.h>
     52 #include <sys/queue.h>
     53 
     54 #include <ctype.h>
     55 #include <err.h>
     56 #include <errno.h>
     57 #include <fcntl.h>
     58 #include <fnmatch.h>
     59 #include <glob.h>
     60 #include <signal.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <unistd.h>
     65 
     66 #include "config.h"
     67 #include "pathnames.h"
     68 
     69 int f_all, f_where;
     70 
     71 int		 main __P((int, char **));
     72 static void	 build_page __P((char *, char **));
     73 static void	 cat __P((char *));
     74 static char	*check_pager __P((char *));
     75 static int	 cleanup __P((void));
     76 static void	 how __P((char *));
     77 static void	 jump __P((char **, char *, char *));
     78 static int	 manual __P((char *, TAG *, glob_t *));
     79 static void	 onsig __P((int));
     80 static void	 usage __P((void));
     81 
     82 int
     83 main(argc, argv)
     84 	int argc;
     85 	char *argv[];
     86 {
     87 	TAG *defp, *defnewp, *section, *sectnewp, *subp;
     88 	ENTRY *e_defp, *e_sectp, *e_subp, *ep;
     89 	glob_t pg;
     90 	size_t len;
     91 	int ch, f_cat, f_how, found;
     92 	char **ap, *cmd, *machine, *p, *p_add, *p_path, *pager, *slashp;
     93 	char *conffile, buf[MAXPATHLEN * 2];
     94 
     95 #ifdef __GNUC__
     96 	pager = NULL;		/* XXX gcc -Wuninitialized */
     97 #endif
     98 
     99 	f_cat = f_how = 0;
    100 	conffile = p_add = p_path = NULL;
    101 	while ((ch = getopt(argc, argv, "-aC:cfhkM:m:P:w")) != -1)
    102 		switch (ch) {
    103 		case 'a':
    104 			f_all = 1;
    105 			break;
    106 		case 'C':
    107 			conffile = optarg;
    108 			break;
    109 		case 'c':
    110 		case '-':		/* Deprecated. */
    111 			f_cat = 1;
    112 			break;
    113 		case 'h':
    114 			f_how = 1;
    115 			break;
    116 		case 'm':
    117 			p_add = optarg;
    118 			break;
    119 		case 'M':
    120 		case 'P':		/* Backward compatibility. */
    121 			p_path = optarg;
    122 			break;
    123 		/*
    124 		 * The -f and -k options are backward compatible,
    125 		 * undocumented ways of calling whatis(1) and apropos(1).
    126 		 */
    127 		case 'f':
    128 			jump(argv, "-f", "whatis");
    129 			/* NOTREACHED */
    130 		case 'k':
    131 			jump(argv, "-k", "apropos");
    132 			/* NOTREACHED */
    133 		case 'w':
    134 			f_all = f_where = 1;
    135 			break;
    136 		case '?':
    137 		default:
    138 			usage();
    139 		}
    140 	argc -= optind;
    141 	argv += optind;
    142 
    143 	if (!*argv)
    144 		usage();
    145 
    146 	if (!f_cat && !f_how && !f_where)
    147 		if (!isatty(1))
    148 			f_cat = 1;
    149 		else if ((pager = getenv("PAGER")) != NULL)
    150 			pager = check_pager(pager);
    151 		else
    152 			pager = _PATH_PAGER;
    153 
    154 	/* Read the configuration file. */
    155 	config(conffile);
    156 
    157 	/* Get the machine type. */
    158 	if ((machine = getenv("MACHINE")) == NULL)
    159 		machine = MACHINE;
    160 
    161 	/* If there's no _default list, create an empty one. */
    162 	if ((defp = getlist("_default")) == NULL)
    163 		defp = addlist("_default");
    164 
    165 	/*
    166 	 * 1: If the user specified a MANPATH variable, or set the -M
    167 	 *    option, we replace the _default list with the user's list,
    168 	 *    appending the entries in the _subdir list and the machine.
    169 	 */
    170 	if (p_path == NULL)
    171 		p_path = getenv("MANPATH");
    172 	if (p_path != NULL) {
    173 		while ((e_defp = defp->list.tqh_first) != NULL) {
    174 			free(e_defp->s);
    175 			TAILQ_REMOVE(&defp->list, e_defp, q);
    176 		}
    177 		for (p = strtok(p_path, ":");
    178 		    p != NULL; p = strtok(NULL, ":")) {
    179 			slashp = p[strlen(p) - 1] == '/' ? "" : "/";
    180 			e_subp = (subp = getlist("_subdir")) == NULL ?
    181 			    NULL : subp->list.tqh_first;
    182 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
    183 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
    184 				    p, slashp, e_subp->s, machine);
    185 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    186 				    (ep->s = strdup(buf)) == NULL)
    187 					err(1, "malloc");
    188 				TAILQ_INSERT_TAIL(&defp->list, ep, q);
    189 			}
    190 		}
    191 	}
    192 
    193 	/*
    194 	 * 2: If the user did not specify MANPATH, -M or a section, rewrite
    195 	 *    the _default list to include the _subdir list and the machine.
    196 	 */
    197 	if (argv[1] == NULL)
    198 		section = NULL;
    199 	else if ((section = getlist(*argv)) != NULL)
    200 		++argv;
    201 	if (p_path == NULL && section == NULL) {
    202 		defnewp = addlist("_default_new");
    203 		e_defp =
    204 		    defp->list.tqh_first == NULL ? NULL : defp->list.tqh_first;
    205 		for (; e_defp != NULL; e_defp = e_defp->q.tqe_next) {
    206 			slashp =
    207 			    e_defp->s[strlen(e_defp->s) - 1] == '/' ? "" : "/";
    208 			e_subp = (subp = getlist("_subdir")) == NULL ?
    209 			    NULL : subp->list.tqh_first;
    210 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
    211 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
    212 				e_defp->s, slashp, e_subp->s, machine);
    213 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    214 				    (ep->s = strdup(buf)) == NULL)
    215 					err(1, "malloc");
    216 				TAILQ_INSERT_TAIL(&defnewp->list, ep, q);
    217 			}
    218 		}
    219 		defp = getlist("_default");
    220 		while ((e_defp = defp->list.tqh_first) != NULL) {
    221 			free(e_defp->s);
    222 			TAILQ_REMOVE(&defp->list, e_defp, q);
    223 		}
    224 		free(defp->s);
    225 		TAILQ_REMOVE(&head, defp, q);
    226 		defnewp = getlist("_default_new");
    227 		free(defnewp->s);
    228 		defnewp->s = "_default";
    229 		defp = defnewp;
    230 	}
    231 
    232 	/*
    233 	 * 3: If the user set the -m option, insert the user's list before
    234 	 *    whatever list we have, again appending the _subdir list and
    235 	 *    the machine.
    236 	 */
    237 	if (p_add != NULL)
    238 		for (p = strtok(p_add, ":"); p != NULL; p = strtok(NULL, ":")) {
    239 			slashp = p[strlen(p) - 1] == '/' ? "" : "/";
    240 			e_subp = (subp = getlist("_subdir")) == NULL ?
    241 			    NULL : subp->list.tqh_first;
    242 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
    243 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
    244 				    p, slashp, e_subp->s, machine);
    245 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    246 				    (ep->s = strdup(buf)) == NULL)
    247 					err(1, "malloc");
    248 				TAILQ_INSERT_HEAD(&defp->list, ep, q);
    249 			}
    250 		}
    251 
    252 	/*
    253 	 * 4: If no -m was specified, and a section was, rewrite the section's
    254 	 *    paths (if they have a trailing slash) to append the _subdir list
    255 	 *    and the machine.  This then becomes the _default list.
    256 	 */
    257 	if (p_add == NULL && section != NULL) {
    258 		sectnewp = addlist("_section_new");
    259 		for (e_sectp = section->list.tqh_first;
    260 		    e_sectp != NULL; e_sectp = e_sectp->q.tqe_next) {
    261 			if (e_sectp->s[strlen(e_sectp->s) - 1] != '/') {
    262 				(void)snprintf(buf, sizeof(buf),
    263 				    "%s{/%s,}", e_sectp->s, machine);
    264 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    265 				    (ep->s = strdup(buf)) == NULL)
    266 					err(1, "malloc");
    267 				TAILQ_INSERT_TAIL(&sectnewp->list, ep, q);
    268 				continue;
    269 			}
    270 			e_subp = (subp = getlist("_subdir")) == NULL ?
    271 			    NULL : subp->list.tqh_first;
    272 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
    273 				(void)snprintf(buf, sizeof(buf), "%s%s{/%s,}",
    274 				    e_sectp->s, e_subp->s, machine);
    275 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    276 				    (ep->s = strdup(buf)) == NULL)
    277 					err(1, "malloc");
    278 				TAILQ_INSERT_TAIL(&sectnewp->list, ep, q);
    279 			}
    280 		}
    281 		sectnewp->s = section->s;
    282 		defp = sectnewp;
    283 		TAILQ_REMOVE(&head, section, q);
    284 	}
    285 
    286 	/*
    287 	 * 5: Search for the files.  Set up an interrupt handler, so the
    288 	 *    temporary files go away.
    289 	 */
    290 	(void)signal(SIGINT, onsig);
    291 	(void)signal(SIGHUP, onsig);
    292 
    293 	memset(&pg, 0, sizeof(pg));
    294 	for (found = 0; *argv; ++argv)
    295 		if (manual(*argv, defp, &pg))
    296 			found = 1;
    297 
    298 	/* 6: If nothing found, we're done. */
    299 	if (!found) {
    300 		(void)cleanup();
    301 		exit (1);
    302 	}
    303 
    304 	/* 7: If it's simple, display it fast. */
    305 	if (f_cat) {
    306 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    307 			if (**ap == '\0')
    308 				continue;
    309 			cat(*ap);
    310 		}
    311 		exit (cleanup());
    312 	}
    313 	if (f_how) {
    314 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    315 			if (**ap == '\0')
    316 				continue;
    317 			how(*ap);
    318 		}
    319 		exit(cleanup());
    320 	}
    321 	if (f_where) {
    322 		for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    323 			if (**ap == '\0')
    324 				continue;
    325 			(void)printf("%s\n", *ap);
    326 		}
    327 		exit(cleanup());
    328 	}
    329 
    330 	/*
    331 	 * 8: We display things in a single command; build a list of things
    332 	 *    to display.
    333 	 */
    334 	for (ap = pg.gl_pathv, len = strlen(pager) + 1; *ap != NULL; ++ap) {
    335 		if (**ap == '\0')
    336 			continue;
    337 		len += strlen(*ap) + 1;
    338 	}
    339 	if ((cmd = malloc(len)) == NULL) {
    340 		warn("malloc");
    341 		(void)cleanup();
    342 		exit(1);
    343 	}
    344 	p = cmd;
    345 	len = strlen(pager);
    346 	memmove(p, pager, len);
    347 	p += len;
    348 	*p++ = ' ';
    349 	for (ap = pg.gl_pathv; *ap != NULL; ++ap) {
    350 		if (**ap == '\0')
    351 			continue;
    352 		len = strlen(*ap);
    353 		memmove(p, *ap, len);
    354 		p += len;
    355 		*p++ = ' ';
    356 	}
    357 	*p = '\0';
    358 
    359 	/* Use system(3) in case someone's pager is "pager arg1 arg2". */
    360 	(void)system(cmd);
    361 
    362 	exit(cleanup());
    363 }
    364 
    365 /*
    366  * manual --
    367  *	Search the manuals for the pages.
    368  */
    369 static int
    370 manual(page, tag, pg)
    371 	char *page;
    372 	TAG *tag;
    373 	glob_t *pg;
    374 {
    375 	ENTRY *ep, *e_sufp, *e_tag;
    376 	TAG *missp, *sufp;
    377 	int anyfound, cnt, found;
    378 	char *p, buf[MAXPATHLEN];
    379 
    380 	anyfound = 0;
    381 	buf[0] = '*';
    382 
    383 	/* For each element in the list... */
    384 	e_tag = tag == NULL ? NULL : tag->list.tqh_first;
    385 	for (; e_tag != NULL; e_tag = e_tag->q.tqe_next) {
    386 		(void)snprintf(buf, sizeof(buf), "%s/%s.*", e_tag->s, page);
    387 		if (glob(buf,
    388 		    GLOB_APPEND | GLOB_BRACE | GLOB_NOSORT | GLOB_QUOTE,
    389 		    NULL, pg)) {
    390 			warn("globbing");
    391 			(void)cleanup();
    392 			exit(1);
    393 		}
    394 		if (pg->gl_matchc == 0)
    395 			continue;
    396 
    397 		/* Find out if it's really a man page. */
    398 		for (cnt = pg->gl_pathc - pg->gl_matchc;
    399 		    cnt < pg->gl_pathc; ++cnt) {
    400 
    401 			/*
    402 			 * Try the _suffix key words first.
    403 			 *
    404 			 * XXX
    405 			 * Older versions of man.conf didn't have the suffix
    406 			 * key words, it was assumed that everything was a .0.
    407 			 * We just test for .0 first, it's fast and probably
    408 			 * going to hit.
    409 			 */
    410 			(void)snprintf(buf, sizeof(buf), "*/%s.0", page);
    411 			if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
    412 				goto next;
    413 
    414 			e_sufp = (sufp = getlist("_suffix")) == NULL ?
    415 			    NULL : sufp->list.tqh_first;
    416 			for (found = 0;
    417 			    e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
    418 				(void)snprintf(buf,
    419 				     sizeof(buf), "*/%s%s", page, e_sufp->s);
    420 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
    421 					found = 1;
    422 					break;
    423 				}
    424 			}
    425 			if (found)
    426 				goto next;
    427 
    428 			/* Try the _build key words next. */
    429 			e_sufp = (sufp = getlist("_build")) == NULL ?
    430 			    NULL : sufp->list.tqh_first;
    431 			for (found = 0;
    432 			    e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
    433 				for (p = e_sufp->s;
    434 				    *p != '\0' && !isspace(*p); ++p);
    435 				if (*p == '\0')
    436 					continue;
    437 				*p = '\0';
    438 				(void)snprintf(buf,
    439 				     sizeof(buf), "*/%s%s", page, e_sufp->s);
    440 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
    441 					if (!f_where)
    442 						build_page(p + 1,
    443 						    &pg->gl_pathv[cnt]);
    444 					*p = ' ';
    445 					found = 1;
    446 					break;
    447 				}
    448 				*p = ' ';
    449 			}
    450 			if (found) {
    451 next:				anyfound = 1;
    452 				if (!f_all) {
    453 					/* Delete any other matches. */
    454 					while (++cnt< pg->gl_pathc)
    455 						pg->gl_pathv[cnt] = "";
    456 					break;
    457 				}
    458 				continue;
    459 			}
    460 
    461 			/* It's not a man page, forget about it. */
    462 			pg->gl_pathv[cnt] = "";
    463 		}
    464 
    465 		if (anyfound && !f_all)
    466 			break;
    467 	}
    468 
    469 	/* If not found, enter onto the missing list. */
    470 	if (!anyfound) {
    471 		if ((missp = getlist("_missing")) == NULL)
    472 			missp = addlist("_missing");
    473 		if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    474 		    (ep->s = strdup(page)) == NULL) {
    475 			warn("malloc");
    476 			(void)cleanup();
    477 			exit(1);
    478 		}
    479 		TAILQ_INSERT_TAIL(&missp->list, ep, q);
    480 	}
    481 	return (anyfound);
    482 }
    483 
    484 /*
    485  * build_page --
    486  *	Build a man page for display.
    487  */
    488 static void
    489 build_page(fmt, pathp)
    490 	char *fmt, **pathp;
    491 {
    492 	static int warned;
    493 	ENTRY *ep;
    494 	TAG *intmpp;
    495 	int fd, n;
    496 	char *p, *b;
    497 	char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[sizeof(_PATH_TMP)];
    498 
    499 	/* Let the user know this may take awhile. */
    500 	if (!warned) {
    501 		warned = 1;
    502 		warnx("Formatting manual page...");
    503 	}
    504 
    505        /*
    506         * Historically man chdir'd to the root of the man tree.
    507         * This was used in man pages that contained relative ".so"
    508         * directives (including other man pages for command aliases etc.)
    509         * It even went one step farther, by examining the first line
    510         * of the man page and parsing the .so filename so it would
    511         * make hard(?) links to the cat'ted man pages for space savings.
    512         * (We don't do that here, but we could).
    513         */
    514 
    515        /* copy and find the end */
    516        for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
    517                continue;
    518 
    519 	/* skip the last two path components, page name and man[n] */
    520 	for (--b, --p, n = 2; b != buf; b--, p--)
    521 		if (*b == '/')
    522 			if (--n == 0) {
    523 				*b = '\0';
    524 				(void) chdir(buf);
    525 				p++;
    526 				break;
    527 			}
    528 
    529 
    530 	/* Add a remove-when-done list. */
    531 	if ((intmpp = getlist("_intmp")) == NULL)
    532 		intmpp = addlist("_intmp");
    533 
    534 	/* Move to the printf(3) format string. */
    535 	for (; *fmt && isspace(*fmt); ++fmt);
    536 
    537 	/*
    538 	 * Get a temporary file and build a version of the file
    539 	 * to display.  Replace the old file name with the new one.
    540 	 */
    541 	(void)strcpy(tpath, _PATH_TMP);
    542 	if ((fd = mkstemp(tpath)) == -1) {
    543 		warn("%s", tpath);
    544 		(void)cleanup();
    545 		exit(1);
    546 	}
    547 	(void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
    548 	(void)snprintf(cmd, sizeof(cmd), buf, p);
    549 	(void)system(cmd);
    550 	(void)close(fd);
    551 	if ((*pathp = strdup(tpath)) == NULL) {
    552 		warn("malloc");
    553 		(void)cleanup();
    554 		exit(1);
    555 	}
    556 
    557 	/* Link the built file into the remove-when-done list. */
    558 	if ((ep = malloc(sizeof(ENTRY))) == NULL) {
    559 		warn("malloc");
    560 		(void)cleanup();
    561 		exit(1);
    562 	}
    563 	ep->s = *pathp;
    564 	TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
    565 }
    566 
    567 /*
    568  * how --
    569  *	display how information
    570  */
    571 static void
    572 how(fname)
    573 	char *fname;
    574 {
    575 	FILE *fp;
    576 
    577 	int lcnt, print;
    578 	char *p, buf[256];
    579 
    580 	if (!(fp = fopen(fname, "r"))) {
    581 		warn("%s", fname);
    582 		(void)cleanup();
    583 		exit (1);
    584 	}
    585 #define	S1	"SYNOPSIS"
    586 #define	S2	"S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
    587 #define	D1	"DESCRIPTION"
    588 #define	D2	"D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
    589 	for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
    590 		if (!strncmp(buf, S1, sizeof(S1) - 1) ||
    591 		    !strncmp(buf, S2, sizeof(S2) - 1)) {
    592 			print = 1;
    593 			continue;
    594 		} else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
    595 		    !strncmp(buf, D2, sizeof(D2) - 1))
    596 			return;
    597 		if (!print)
    598 			continue;
    599 		if (*buf == '\n')
    600 			++lcnt;
    601 		else {
    602 			for(; lcnt; --lcnt)
    603 				(void)putchar('\n');
    604 			for (p = buf; isspace(*p); ++p);
    605 			(void)fputs(p, stdout);
    606 		}
    607 	}
    608 	(void)fclose(fp);
    609 }
    610 
    611 /*
    612  * cat --
    613  *	cat out the file
    614  */
    615 static void
    616 cat(fname)
    617 	char *fname;
    618 {
    619 	int fd, n;
    620 	char buf[2048];
    621 
    622 	if ((fd = open(fname, O_RDONLY, 0)) < 0) {
    623 		warn("%s", fname);
    624 		(void)cleanup();
    625 		exit(1);
    626 	}
    627 	while ((n = read(fd, buf, sizeof(buf))) > 0)
    628 		if (write(STDOUT_FILENO, buf, n) != n) {
    629 			warn("write");
    630 			(void)cleanup();
    631 			exit (1);
    632 		}
    633 	if (n == -1) {
    634 		warn("read");
    635 		(void)cleanup();
    636 		exit(1);
    637 	}
    638 	(void)close(fd);
    639 }
    640 
    641 /*
    642  * check_pager --
    643  *	check the user supplied page information
    644  */
    645 static char *
    646 check_pager(name)
    647 	char *name;
    648 {
    649 	char *p, *save;
    650 
    651 	/*
    652 	 * if the user uses "more", we make it "more -s"; watch out for
    653 	 * PAGER = "mypager /usr/ucb/more"
    654 	 */
    655 	for (p = name; *p && !isspace(*p); ++p);
    656 	for (; p > name && *p != '/'; --p);
    657 	if (p != name)
    658 		++p;
    659 
    660 	/* make sure it's "more", not "morex" */
    661 	if (!strncmp(p, "more", 4) && (!p[4] || isspace(p[4]))){
    662 		save = name;
    663 		/* allocate space to add the "-s" */
    664 		if (!(name =
    665 		    malloc((u_int)(strlen(save) + sizeof("-s") + 1))))
    666 			err(1, "malloc");
    667 		(void)sprintf(name, "%s %s", save, "-s");
    668 	}
    669 	return(name);
    670 }
    671 
    672 /*
    673  * jump --
    674  *	strip out flag argument and jump
    675  */
    676 static void
    677 jump(argv, flag, name)
    678 	char **argv, *flag, *name;
    679 {
    680 	char **arg;
    681 
    682 	argv[0] = name;
    683 	for (arg = argv + 1; *arg; ++arg)
    684 		if (!strcmp(*arg, flag))
    685 			break;
    686 	for (; *arg; ++arg)
    687 		arg[0] = arg[1];
    688 	execvp(name, argv);
    689 	(void)fprintf(stderr, "%s: Command not found.\n", name);
    690 	exit(1);
    691 }
    692 
    693 /*
    694  * onsig --
    695  *	If signaled, delete the temporary files.
    696  */
    697 static void
    698 onsig(signo)
    699 	int signo;
    700 {
    701 	(void)cleanup();
    702 
    703 	(void)signal(signo, SIG_DFL);
    704 	(void)kill(getpid(), signo);
    705 
    706 	/* NOTREACHED */
    707 	exit (1);
    708 }
    709 
    710 /*
    711  * cleanup --
    712  *	Clean up temporary files, show any error messages.
    713  */
    714 static int
    715 cleanup()
    716 {
    717 	TAG *intmpp, *missp;
    718 	ENTRY *ep;
    719 	int rval;
    720 
    721 	rval = 0;
    722 	ep = (missp = getlist("_missing")) == NULL ?
    723 	    NULL : missp->list.tqh_first;
    724 	if (ep != NULL)
    725 		for (; ep != NULL; ep = ep->q.tqe_next) {
    726 			warnx("no entry for %s in the manual.", ep->s);
    727 			rval = 1;
    728 		}
    729 
    730 	ep = (intmpp = getlist("_intmp")) == NULL ?
    731 	    NULL : intmpp->list.tqh_first;
    732 	for (; ep != NULL; ep = ep->q.tqe_next)
    733 		(void)unlink(ep->s);
    734 	return (rval);
    735 }
    736 
    737 /*
    738  * usage --
    739  *	print usage message and die
    740  */
    741 static void
    742 usage()
    743 {
    744 	(void)fprintf(stderr,
    745     "usage: man [-achw] [-C file] [-M path] [-m path] [section] title ...\n");
    746 	exit(1);
    747 }
    748