Home | History | Annotate | Line # | Download | only in man
man.c revision 1.11
      1 /*	$NetBSD: man.c,v 1.11 1998/06/19 23:02:44 kleink 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.11 1998/06/19 23:02:44 kleink 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, NULL, pg)) {
    389 			warn("globbing");
    390 			(void)cleanup();
    391 			exit(1);
    392 		}
    393 		if (pg->gl_matchc == 0)
    394 			continue;
    395 
    396 		/* Find out if it's really a man page. */
    397 		for (cnt = pg->gl_pathc - pg->gl_matchc;
    398 		    cnt < pg->gl_pathc; ++cnt) {
    399 
    400 			/*
    401 			 * Try the _suffix key words first.
    402 			 *
    403 			 * XXX
    404 			 * Older versions of man.conf didn't have the suffix
    405 			 * key words, it was assumed that everything was a .0.
    406 			 * We just test for .0 first, it's fast and probably
    407 			 * going to hit.
    408 			 */
    409 			(void)snprintf(buf, sizeof(buf), "*/%s.0", page);
    410 			if (!fnmatch(buf, pg->gl_pathv[cnt], 0))
    411 				goto next;
    412 
    413 			e_sufp = (sufp = getlist("_suffix")) == NULL ?
    414 			    NULL : sufp->list.tqh_first;
    415 			for (found = 0;
    416 			    e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
    417 				(void)snprintf(buf,
    418 				     sizeof(buf), "*/%s%s", page, e_sufp->s);
    419 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
    420 					found = 1;
    421 					break;
    422 				}
    423 			}
    424 			if (found)
    425 				goto next;
    426 
    427 			/* Try the _build key words next. */
    428 			e_sufp = (sufp = getlist("_build")) == NULL ?
    429 			    NULL : sufp->list.tqh_first;
    430 			for (found = 0;
    431 			    e_sufp != NULL; e_sufp = e_sufp->q.tqe_next) {
    432 				for (p = e_sufp->s;
    433 				    *p != '\0' && !isspace(*p); ++p);
    434 				if (*p == '\0')
    435 					continue;
    436 				*p = '\0';
    437 				(void)snprintf(buf,
    438 				     sizeof(buf), "*/%s%s", page, e_sufp->s);
    439 				if (!fnmatch(buf, pg->gl_pathv[cnt], 0)) {
    440 					if (!f_where)
    441 						build_page(p + 1,
    442 						    &pg->gl_pathv[cnt]);
    443 					*p = ' ';
    444 					found = 1;
    445 					break;
    446 				}
    447 				*p = ' ';
    448 			}
    449 			if (found) {
    450 next:				anyfound = 1;
    451 				if (!f_all) {
    452 					/* Delete any other matches. */
    453 					while (++cnt< pg->gl_pathc)
    454 						pg->gl_pathv[cnt] = "";
    455 					break;
    456 				}
    457 				continue;
    458 			}
    459 
    460 			/* It's not a man page, forget about it. */
    461 			pg->gl_pathv[cnt] = "";
    462 		}
    463 
    464 		if (anyfound && !f_all)
    465 			break;
    466 	}
    467 
    468 	/* If not found, enter onto the missing list. */
    469 	if (!anyfound) {
    470 		if ((missp = getlist("_missing")) == NULL)
    471 			missp = addlist("_missing");
    472 		if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    473 		    (ep->s = strdup(page)) == NULL) {
    474 			warn("malloc");
    475 			(void)cleanup();
    476 			exit(1);
    477 		}
    478 		TAILQ_INSERT_TAIL(&missp->list, ep, q);
    479 	}
    480 	return (anyfound);
    481 }
    482 
    483 /*
    484  * build_page --
    485  *	Build a man page for display.
    486  */
    487 static void
    488 build_page(fmt, pathp)
    489 	char *fmt, **pathp;
    490 {
    491 	static int warned;
    492 	ENTRY *ep;
    493 	TAG *intmpp;
    494 	int fd, n;
    495 	char *p, *b;
    496 	char buf[MAXPATHLEN], cmd[MAXPATHLEN], tpath[sizeof(_PATH_TMP)];
    497 
    498 	/* Let the user know this may take awhile. */
    499 	if (!warned) {
    500 		warned = 1;
    501 		warnx("Formatting manual page...");
    502 	}
    503 
    504        /*
    505         * Historically man chdir'd to the root of the man tree.
    506         * This was used in man pages that contained relative ".so"
    507         * directives (including other man pages for command aliases etc.)
    508         * It even went one step farther, by examining the first line
    509         * of the man page and parsing the .so filename so it would
    510         * make hard(?) links to the cat'ted man pages for space savings.
    511         * (We don't do that here, but we could).
    512         */
    513 
    514        /* copy and find the end */
    515        for (b = buf, p = *pathp; (*b++ = *p++) != '\0';)
    516                continue;
    517 
    518 	/* skip the last two path components, page name and man[n] */
    519 	for (--b, --p, n = 2; b != buf; b--, p--)
    520 		if (*b == '/')
    521 			if (--n == 0) {
    522 				*b = '\0';
    523 				(void) chdir(buf);
    524 				p++;
    525 				break;
    526 			}
    527 
    528 
    529 	/* Add a remove-when-done list. */
    530 	if ((intmpp = getlist("_intmp")) == NULL)
    531 		intmpp = addlist("_intmp");
    532 
    533 	/* Move to the printf(3) format string. */
    534 	for (; *fmt && isspace(*fmt); ++fmt);
    535 
    536 	/*
    537 	 * Get a temporary file and build a version of the file
    538 	 * to display.  Replace the old file name with the new one.
    539 	 */
    540 	(void)strcpy(tpath, _PATH_TMP);
    541 	if ((fd = mkstemp(tpath)) == -1) {
    542 		warn("%s", tpath);
    543 		(void)cleanup();
    544 		exit(1);
    545 	}
    546 	(void)snprintf(buf, sizeof(buf), "%s > %s", fmt, tpath);
    547 	(void)snprintf(cmd, sizeof(cmd), buf, p);
    548 	(void)system(cmd);
    549 	(void)close(fd);
    550 	if ((*pathp = strdup(tpath)) == NULL) {
    551 		warn("malloc");
    552 		(void)cleanup();
    553 		exit(1);
    554 	}
    555 
    556 	/* Link the built file into the remove-when-done list. */
    557 	if ((ep = malloc(sizeof(ENTRY))) == NULL) {
    558 		warn("malloc");
    559 		(void)cleanup();
    560 		exit(1);
    561 	}
    562 	ep->s = *pathp;
    563 	TAILQ_INSERT_TAIL(&intmpp->list, ep, q);
    564 }
    565 
    566 /*
    567  * how --
    568  *	display how information
    569  */
    570 static void
    571 how(fname)
    572 	char *fname;
    573 {
    574 	FILE *fp;
    575 
    576 	int lcnt, print;
    577 	char *p, buf[256];
    578 
    579 	if (!(fp = fopen(fname, "r"))) {
    580 		warn("%s", fname);
    581 		(void)cleanup();
    582 		exit (1);
    583 	}
    584 #define	S1	"SYNOPSIS"
    585 #define	S2	"S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS"
    586 #define	D1	"DESCRIPTION"
    587 #define	D2	"D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN"
    588 	for (lcnt = print = 0; fgets(buf, sizeof(buf), fp);) {
    589 		if (!strncmp(buf, S1, sizeof(S1) - 1) ||
    590 		    !strncmp(buf, S2, sizeof(S2) - 1)) {
    591 			print = 1;
    592 			continue;
    593 		} else if (!strncmp(buf, D1, sizeof(D1) - 1) ||
    594 		    !strncmp(buf, D2, sizeof(D2) - 1))
    595 			return;
    596 		if (!print)
    597 			continue;
    598 		if (*buf == '\n')
    599 			++lcnt;
    600 		else {
    601 			for(; lcnt; --lcnt)
    602 				(void)putchar('\n');
    603 			for (p = buf; isspace(*p); ++p);
    604 			(void)fputs(p, stdout);
    605 		}
    606 	}
    607 	(void)fclose(fp);
    608 }
    609 
    610 /*
    611  * cat --
    612  *	cat out the file
    613  */
    614 static void
    615 cat(fname)
    616 	char *fname;
    617 {
    618 	int fd, n;
    619 	char buf[2048];
    620 
    621 	if ((fd = open(fname, O_RDONLY, 0)) < 0) {
    622 		warn("%s", fname);
    623 		(void)cleanup();
    624 		exit(1);
    625 	}
    626 	while ((n = read(fd, buf, sizeof(buf))) > 0)
    627 		if (write(STDOUT_FILENO, buf, n) != n) {
    628 			warn("write");
    629 			(void)cleanup();
    630 			exit (1);
    631 		}
    632 	if (n == -1) {
    633 		warn("read");
    634 		(void)cleanup();
    635 		exit(1);
    636 	}
    637 	(void)close(fd);
    638 }
    639 
    640 /*
    641  * check_pager --
    642  *	check the user supplied page information
    643  */
    644 static char *
    645 check_pager(name)
    646 	char *name;
    647 {
    648 	char *p, *save;
    649 
    650 	/*
    651 	 * if the user uses "more", we make it "more -s"; watch out for
    652 	 * PAGER = "mypager /usr/ucb/more"
    653 	 */
    654 	for (p = name; *p && !isspace(*p); ++p);
    655 	for (; p > name && *p != '/'; --p);
    656 	if (p != name)
    657 		++p;
    658 
    659 	/* make sure it's "more", not "morex" */
    660 	if (!strncmp(p, "more", 4) && (!p[4] || isspace(p[4]))){
    661 		save = name;
    662 		/* allocate space to add the "-s" */
    663 		if (!(name =
    664 		    malloc((u_int)(strlen(save) + sizeof("-s") + 1))))
    665 			err(1, "malloc");
    666 		(void)sprintf(name, "%s %s", save, "-s");
    667 	}
    668 	return(name);
    669 }
    670 
    671 /*
    672  * jump --
    673  *	strip out flag argument and jump
    674  */
    675 static void
    676 jump(argv, flag, name)
    677 	char **argv, *flag, *name;
    678 {
    679 	char **arg;
    680 
    681 	argv[0] = name;
    682 	for (arg = argv + 1; *arg; ++arg)
    683 		if (!strcmp(*arg, flag))
    684 			break;
    685 	for (; *arg; ++arg)
    686 		arg[0] = arg[1];
    687 	execvp(name, argv);
    688 	(void)fprintf(stderr, "%s: Command not found.\n", name);
    689 	exit(1);
    690 }
    691 
    692 /*
    693  * onsig --
    694  *	If signaled, delete the temporary files.
    695  */
    696 static void
    697 onsig(signo)
    698 	int signo;
    699 {
    700 	(void)cleanup();
    701 
    702 	(void)signal(signo, SIG_DFL);
    703 	(void)kill(getpid(), signo);
    704 
    705 	/* NOTREACHED */
    706 	exit (1);
    707 }
    708 
    709 /*
    710  * cleanup --
    711  *	Clean up temporary files, show any error messages.
    712  */
    713 static int
    714 cleanup()
    715 {
    716 	TAG *intmpp, *missp;
    717 	ENTRY *ep;
    718 	int rval;
    719 
    720 	rval = 0;
    721 	ep = (missp = getlist("_missing")) == NULL ?
    722 	    NULL : missp->list.tqh_first;
    723 	if (ep != NULL)
    724 		for (; ep != NULL; ep = ep->q.tqe_next) {
    725 			warnx("no entry for %s in the manual.", ep->s);
    726 			rval = 1;
    727 		}
    728 
    729 	ep = (intmpp = getlist("_intmp")) == NULL ?
    730 	    NULL : intmpp->list.tqh_first;
    731 	for (; ep != NULL; ep = ep->q.tqe_next)
    732 		(void)unlink(ep->s);
    733 	return (rval);
    734 }
    735 
    736 /*
    737  * usage --
    738  *	print usage message and die
    739  */
    740 static void
    741 usage()
    742 {
    743 	(void)fprintf(stderr,
    744     "usage: man [-achw] [-C file] [-M path] [-m path] [section] title ...\n");
    745 	exit(1);
    746 }
    747