Home | History | Annotate | Line # | Download | only in catman
catman.c revision 1.14
      1  1.14  tsutsui /*      $NetBSD: catman.c,v 1.14 2000/01/09 04:54:54 tsutsui Exp $       */
      2  1.10    dante 
      3   1.1      jtc /*
      4  1.10    dante  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5   1.1      jtc  * All rights reserved.
      6   1.1      jtc  *
      7  1.10    dante  * Author: Baldassare Dante Profeta <dante (at) mclink.it>
      8  1.10    dante  *
      9   1.1      jtc  * Redistribution and use in source and binary forms, with or without
     10   1.1      jtc  * modification, are permitted provided that the following conditions
     11   1.1      jtc  * are met:
     12   1.1      jtc  * 1. Redistributions of source code must retain the above copyright
     13   1.1      jtc  *    notice, this list of conditions and the following disclaimer.
     14   1.1      jtc  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1      jtc  *    notice, this list of conditions and the following disclaimer in the
     16   1.1      jtc  *    documentation and/or other materials provided with the distribution.
     17   1.1      jtc  * 3. All advertising materials mentioning features or use of this software
     18   1.1      jtc  *    must display the following acknowledgement:
     19  1.10    dante  *        This product includes software developed by the NetBSD
     20  1.10    dante  *        Foundation, Inc. and its contributors.
     21  1.10    dante  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  1.10    dante  *    contributors may be used to endorse or promote products derived
     23  1.10    dante  *    from this software without specific prior written permission.
     24   1.1      jtc  *
     25  1.10    dante  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  1.10    dante  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.10    dante  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.10    dante  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  1.10    dante  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.10    dante  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.10    dante  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.10    dante  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.10    dante  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.10    dante  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.10    dante  * POSSIBILITY OF SUCH DAMAGE.
     36   1.1      jtc  */
     37   1.2  mycroft 
     38  1.10    dante #include <sys/types.h>
     39  1.10    dante #include <sys/queue.h>
     40   1.7    lukem #include <sys/cdefs.h>
     41  1.10    dante #include <sys/param.h>
     42   1.4   chopps #include <sys/stat.h>
     43   1.4   chopps #include <sys/wait.h>
     44  1.14  tsutsui #include <sys/utsname.h>
     45  1.10    dante #include <ctype.h>
     46   1.6      cgd #include <dirent.h>
     47   1.6      cgd #include <err.h>
     48   1.6      cgd #include <errno.h>
     49  1.10    dante #include <fnmatch.h>
     50   1.6      cgd #include <limits.h>
     51  1.10    dante #include <libgen.h>
     52   1.1      jtc #include <stdio.h>
     53   1.1      jtc #include <stdlib.h>
     54   1.1      jtc #include <string.h>
     55   1.1      jtc #include <unistd.h>
     56  1.10    dante #include <glob.h>
     57   1.1      jtc 
     58  1.10    dante #include "config.h"
     59   1.6      cgd #include "pathnames.h"
     60   1.4   chopps 
     61  1.10    dante int f_nowhatis = 0;
     62  1.10    dante int f_noaction = 0;
     63  1.10    dante int f_noformat = 0;
     64  1.10    dante int f_ignerr = 0;
     65  1.10    dante int f_noprint = 0;
     66  1.10    dante int dowhatis = 0;
     67  1.10    dante 
     68  1.10    dante int		main __P((int, char **));
     69  1.10    dante static void	setdefentries __P((char *, char *, char *));
     70  1.10    dante static void	uniquepath __P((void));
     71  1.10    dante static void	catman __P((void));
     72  1.10    dante static void	scanmandir __P((const char *, const char *));
     73  1.10    dante static int	splitentry __P((char *, char *, char *));
     74  1.10    dante static void	setcatsuffix __P((char *, const char *, const char *));
     75  1.10    dante static void	makecat __P((const char *, const char *, const char *,
     76  1.10    dante 							const char *));
     77  1.10    dante static void	makewhatis __P((void));
     78  1.10    dante static void	dosystem __P((const char *));
     79  1.10    dante static void	usage __P((void));
     80  1.10    dante 
     81   1.1      jtc 
     82   1.1      jtc int
     83   1.1      jtc main(argc, argv)
     84   1.4   chopps 	int argc;
     85   1.4   chopps 	char **argv;
     86   1.1      jtc {
     87  1.10    dante 	char *m_path = NULL;
     88  1.10    dante 	char *m_add = NULL;
     89   1.4   chopps 	int c;
     90   1.1      jtc 
     91  1.10    dante 	while ((c = getopt(argc, argv, "km:M:npsw")) != -1) {
     92   1.1      jtc 		switch (c) {
     93   1.4   chopps 		case 'k':
     94   1.4   chopps 			f_ignerr = 1;
     95   1.4   chopps 			break;
     96   1.1      jtc 		case 'n':
     97   1.4   chopps 			f_nowhatis = 1;
     98   1.1      jtc 			break;
     99   1.1      jtc 		case 'p':
    100   1.4   chopps 			f_noaction = 1;
    101   1.4   chopps 			break;
    102   1.4   chopps 		case 's':
    103   1.4   chopps 			f_noprint = 1;
    104   1.1      jtc 			break;
    105   1.1      jtc 		case 'w':
    106   1.4   chopps 			f_noformat = 1;
    107   1.1      jtc 			break;
    108  1.10    dante 		case 'm':
    109  1.10    dante 			m_add = optarg;
    110  1.10    dante 			break;
    111   1.1      jtc 		case 'M':
    112  1.10    dante 			m_path = optarg;
    113   1.1      jtc 			break;
    114   1.1      jtc 
    115   1.1      jtc 		case '?':
    116   1.1      jtc 		default:
    117   1.1      jtc 			usage();
    118   1.1      jtc 		}
    119   1.1      jtc 	}
    120   1.1      jtc 
    121   1.1      jtc 	argc -= optind;
    122   1.1      jtc 	argv += optind;
    123   1.1      jtc 
    124   1.4   chopps 	if (f_noprint && f_noaction)
    125   1.4   chopps 		f_noprint = 0;
    126   1.4   chopps 
    127   1.6      cgd 	if (argc > 1)
    128   1.6      cgd 		usage();
    129  1.10    dante 
    130  1.10    dante 	config(_PATH_MANCONF);
    131  1.10    dante 	setdefentries(m_path, m_add, (argc == 0)? NULL : argv[argc-1]);
    132  1.10    dante 	uniquepath();
    133   1.1      jtc 
    134   1.5   chopps 	if (f_noformat == 0 || f_nowhatis == 0)
    135  1.10    dante 		catman();
    136   1.4   chopps 	if (f_nowhatis == 0 && dowhatis)
    137  1.10    dante 		makewhatis();
    138   1.1      jtc 
    139  1.10    dante 	return(0);
    140   1.1      jtc }
    141   1.1      jtc 
    142  1.10    dante static void
    143  1.10    dante setdefentries(m_path, m_add, sections)
    144  1.10    dante 	char *m_path;
    145  1.10    dante 	char *m_add;
    146  1.10    dante 	char *sections;
    147  1.10    dante {
    148  1.10    dante 	TAG *defp, *defnewp, *sectnewp, *subp;
    149  1.10    dante 	ENTRY *e_defp, *e_sectp, *e_subp, *ep;
    150  1.10    dante 	char *p, *slashp, *machine;
    151  1.10    dante 	char section[10];
    152  1.10    dante 	char buf[MAXPATHLEN * 2];
    153  1.10    dante 	int i;
    154  1.10    dante 
    155  1.10    dante 	/* Get the machine type. */
    156  1.14  tsutsui 	if ((machine = getenv("MACHINE")) == NULL) {
    157  1.14  tsutsui 		struct utsname utsname;
    158  1.14  tsutsui 
    159  1.14  tsutsui 		if (uname(&utsname) == -1) {
    160  1.14  tsutsui 			perror("uname");
    161  1.14  tsutsui 			exit(1);
    162  1.14  tsutsui 		}
    163  1.14  tsutsui 		machine = utsname.machine;
    164  1.14  tsutsui 	}
    165  1.10    dante 
    166  1.10    dante 	/* If there's no _default list, create an empty one. */
    167  1.10    dante 	if ((defp = getlist("_default")) == NULL)
    168  1.10    dante 		defp = addlist("_default");
    169  1.10    dante 
    170  1.10    dante 	/*
    171  1.10    dante 	 * 0: If one or more sections was specified, rewrite _subdir list.
    172  1.10    dante 	 */
    173  1.10    dante 	if (sections != NULL) {
    174  1.10    dante 		sectnewp = addlist("_section_new");
    175  1.10    dante 		for(p=sections; *p;) {
    176  1.10    dante 			section[0] = *p++;
    177  1.10    dante 			for(i=1; *p && !isdigit(*p) && i<10; i++)
    178  1.10    dante 				section[i] = *p++;
    179  1.10    dante 			section[i] = '\0';
    180  1.10    dante 			snprintf(buf, sizeof(buf), "man%s", section);
    181  1.10    dante 			if(!(e_sectp = malloc(sizeof(ENTRY))))
    182  1.10    dante 				err(1, "malloc");
    183  1.10    dante 			if(!(e_sectp->s = strdup(buf)))
    184  1.10    dante 				err(1, "malloc");
    185  1.10    dante 			TAILQ_INSERT_TAIL(&sectnewp->list, e_sectp, q);
    186  1.10    dante 		}
    187  1.10    dante 		removelist("_subdir");
    188  1.10    dante 		renamelist("_section_new", "_subdir");
    189  1.10    dante 	}
    190  1.10    dante 
    191  1.10    dante 	/*
    192  1.10    dante 	 * 1: If the user specified a MANPATH variable, or set the -M
    193  1.10    dante 	 *    option, we replace the _default list with the user's list,
    194  1.10    dante 	 *    appending the entries in the _subdir list and the machine.
    195  1.10    dante 	 */
    196  1.10    dante 	if (m_path == NULL)
    197  1.10    dante 		m_path = getenv("MANPATH");
    198  1.10    dante 	if (m_path != NULL) {
    199  1.10    dante 		while ((e_defp = defp->list.tqh_first) != NULL) {
    200  1.10    dante 			free(e_defp->s);
    201  1.10    dante 			TAILQ_REMOVE(&defp->list, e_defp, q);
    202  1.10    dante 		}
    203  1.10    dante 		for (p = strtok(m_path, ":");
    204  1.10    dante 		    p != NULL; p = strtok(NULL, ":")) {
    205  1.10    dante 			slashp = p[strlen(p) - 1] == '/' ? "" : "/";
    206  1.10    dante 			e_subp = (subp = getlist("_subdir")) == NULL ?
    207  1.10    dante 			    NULL : subp->list.tqh_first;
    208  1.10    dante 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
    209  1.10    dante 				if(!strncmp(e_subp->s, "cat", 3))
    210  1.10    dante 					continue;
    211  1.10    dante 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
    212  1.10    dante 				    p, slashp, e_subp->s, machine);
    213  1.10    dante 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    214  1.10    dante 				    (ep->s = strdup(buf)) == NULL)
    215  1.10    dante 					err(1, "malloc");
    216  1.10    dante 				TAILQ_INSERT_TAIL(&defp->list, ep, q);
    217  1.10    dante 			}
    218  1.10    dante 		}
    219  1.10    dante 	}
    220  1.10    dante 
    221  1.10    dante 	/*
    222  1.10    dante 	 * 2: If the user did not specify MANPATH, -M or a section, rewrite
    223  1.10    dante 	 *    the _default list to include the _subdir list and the machine.
    224  1.10    dante 	 */
    225  1.10    dante 	if (m_path == NULL) {
    226  1.10    dante 		defp = getlist("_default");
    227  1.10    dante 		defnewp = addlist("_default_new");
    228  1.10    dante 		e_defp =
    229  1.10    dante 		    defp->list.tqh_first == NULL ? NULL : defp->list.tqh_first;
    230  1.10    dante 		for (; e_defp; e_defp = e_defp->q.tqe_next) {
    231  1.10    dante 			slashp =
    232  1.10    dante 			    e_defp->s[strlen(e_defp->s) - 1] == '/' ? "" : "/";
    233  1.10    dante 			e_subp = (subp = getlist("_subdir")) == NULL ?
    234  1.10    dante 			    NULL : subp->list.tqh_first;
    235  1.10    dante 			for (; e_subp; e_subp = e_subp->q.tqe_next) {
    236  1.10    dante 				if(!strncmp(e_subp->s, "cat", 3))
    237  1.10    dante 					continue;
    238  1.10    dante 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
    239  1.10    dante 				e_defp->s, slashp, e_subp->s, machine);
    240  1.10    dante 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    241  1.10    dante 				    (ep->s = strdup(buf)) == NULL)
    242  1.10    dante 					err(1, "malloc");
    243  1.10    dante 				TAILQ_INSERT_TAIL(&defnewp->list, ep, q);
    244  1.10    dante 			}
    245  1.10    dante 		}
    246  1.10    dante 		removelist("_default");
    247  1.10    dante 		renamelist("_default_new", "_default");
    248  1.10    dante 	}
    249  1.10    dante 
    250  1.10    dante 	/*
    251  1.10    dante 	 * 3: If the user set the -m option, insert the user's list before
    252  1.10    dante 	 *    whatever list we have, again appending the _subdir list and
    253  1.10    dante 	 *    the machine.
    254  1.10    dante 	 */
    255  1.10    dante 	if (m_add != NULL)
    256  1.10    dante 		for (p = strtok(m_add, ":"); p != NULL; p = strtok(NULL, ":")) {
    257  1.10    dante 			slashp = p[strlen(p) - 1] == '/' ? "" : "/";
    258  1.10    dante 			e_subp = (subp = getlist("_subdir")) == NULL ?
    259  1.10    dante 			    NULL : subp->list.tqh_first;
    260  1.10    dante 			for (; e_subp != NULL; e_subp = e_subp->q.tqe_next) {
    261  1.10    dante 				if(!strncmp(e_subp->s, "cat", 3))
    262  1.10    dante 					continue;
    263  1.10    dante 				(void)snprintf(buf, sizeof(buf), "%s%s%s{/%s,}",
    264  1.10    dante 				    p, slashp, e_subp->s, machine);
    265  1.10    dante 				if ((ep = malloc(sizeof(ENTRY))) == NULL ||
    266  1.10    dante 				    (ep->s = strdup(buf)) == NULL)
    267  1.10    dante 					err(1, "malloc");
    268  1.10    dante 				TAILQ_INSERT_HEAD(&defp->list, ep, q);
    269  1.10    dante 			}
    270  1.10    dante 		}
    271  1.10    dante }
    272   1.1      jtc 
    273  1.10    dante /*
    274  1.10    dante  * Remove entries (directory) which are symbolic links to other entries.
    275  1.10    dante  * Some examples are showed below:
    276  1.10    dante  * 1) if /usr/X11 -> /usr/X11R6 then remove all /usr/X11/man entries.
    277  1.10    dante  * 2) if /usr/local/man -> /usr/share/man then remove all /usr/local/man
    278  1.10    dante  *    entries
    279  1.10    dante  */
    280  1.10    dante static void
    281  1.10    dante uniquepath(void)
    282   1.1      jtc {
    283  1.10    dante 	TAG *defp, *defnewp;
    284  1.10    dante 	ENTRY *e_defp;
    285  1.10    dante 	glob_t manpaths;
    286  1.10    dante 	struct stat st1;
    287  1.10    dante 	struct stat st2;
    288  1.10    dante 	struct stat st3;
    289  1.10    dante 	int i,j,len,lnk;
    290  1.10    dante 	char path[PATH_MAX], *p;
    291  1.10    dante 
    292  1.10    dante 
    293  1.10    dante 	defp = getlist("_default");
    294  1.10    dante 	e_defp = defp->list.tqh_first;
    295  1.10    dante 	glob(e_defp->s, GLOB_BRACE | GLOB_NOSORT, NULL, &manpaths);
    296  1.10    dante 	for(e_defp = e_defp->q.tqe_next; e_defp; e_defp = e_defp->q.tqe_next) {
    297  1.10    dante 		glob(e_defp->s, GLOB_BRACE | GLOB_NOSORT | GLOB_APPEND, NULL,
    298  1.10    dante 				&manpaths);
    299  1.10    dante 	}
    300  1.10    dante 
    301  1.10    dante 	defnewp = addlist("_default_new");
    302  1.10    dante 
    303  1.10    dante 	for(i=0; i<manpaths.gl_pathc; i++) {
    304  1.10    dante 		lnk = 0;
    305  1.10    dante 		lstat(manpaths.gl_pathv[i], &st1);
    306  1.10    dante 		for(j=0; j<manpaths.gl_pathc; j++) {
    307  1.10    dante 			if(i!=j) {
    308  1.10    dante 				lstat(manpaths.gl_pathv[j], &st2);
    309  1.10    dante 				if(st1.st_ino == st2.st_ino) {
    310  1.10    dante 					strcpy(path, manpaths.gl_pathv[i]);
    311  1.10    dante 					for(p = path; *(p+1) != '\0';) {
    312  1.10    dante 						p = dirname(p);
    313  1.10    dante 						lstat(p, &st3);
    314  1.10    dante 						if(S_ISLNK(st3.st_mode)) {
    315  1.10    dante 							lnk = 1;
    316  1.10    dante 							break;
    317  1.10    dante 						}
    318  1.10    dante 					}
    319  1.10    dante 				} else {
    320  1.10    dante 					len = readlink(manpaths.gl_pathv[i],
    321  1.10    dante 							path, PATH_MAX);
    322  1.10    dante 					if(len == -1)
    323  1.10    dante 						continue;
    324  1.10    dante 					if(!strcmp(path, manpaths.gl_pathv[j]))
    325  1.10    dante 						lnk = 1;
    326  1.10    dante 				}
    327  1.10    dante 				if(lnk)
    328  1.10    dante 					break;
    329  1.10    dante 			}
    330  1.10    dante 		}
    331  1.10    dante 		if(!lnk) {
    332  1.10    dante 			if(!(e_defp = malloc(sizeof(ENTRY))))
    333  1.10    dante 				err(1, "malloc");
    334  1.10    dante 			if(!(e_defp->s = strdup(manpaths.gl_pathv[i])))
    335  1.10    dante 				err(1, "malloc");
    336  1.10    dante 			TAILQ_INSERT_TAIL(&defnewp->list, e_defp, q);
    337  1.10    dante 		}
    338  1.10    dante 	}
    339  1.10    dante 
    340  1.10    dante 	globfree(&manpaths);
    341  1.10    dante 
    342  1.10    dante 	removelist("_default");
    343  1.10    dante 	renamelist("_default_new", "_default");
    344  1.10    dante }
    345  1.10    dante 
    346  1.10    dante static void
    347  1.10    dante catman(void)
    348  1.10    dante {
    349  1.10    dante 	TAG *pathp;
    350  1.10    dante 	ENTRY *e_path;
    351  1.10    dante 	char *mandir;
    352  1.10    dante 	char catdir[PATH_MAX], *cp;
    353  1.10    dante 
    354  1.10    dante 
    355  1.10    dante 	pathp = getlist("_default");
    356  1.10    dante 	for(e_path = pathp->list.tqh_first; e_path;
    357  1.10    dante 				e_path = e_path->q.tqe_next) {
    358  1.10    dante 		mandir = e_path->s;
    359  1.10    dante 		strcpy(catdir,mandir);
    360  1.10    dante 		if(!(cp = strstr(catdir, "man/man")))
    361  1.10    dante 			continue;
    362  1.10    dante 		cp+=4; *cp++ = 'c'; *cp++ = 'a'; *cp = 't';
    363  1.10    dante 		scanmandir(catdir, mandir);
    364  1.10    dante 	}
    365  1.10    dante }
    366  1.10    dante 
    367  1.10    dante static void
    368  1.10    dante scanmandir(catdir, mandir)
    369  1.10    dante 	const char *catdir;
    370  1.10    dante 	const char *mandir;
    371  1.10    dante {
    372  1.10    dante 	TAG *buildp, *crunchp;
    373  1.10    dante 	ENTRY *e_build, *e_crunch;
    374   1.4   chopps 	char manpage[PATH_MAX];
    375   1.4   chopps 	char catpage[PATH_MAX];
    376  1.10    dante 	char linkname[PATH_MAX];
    377  1.10    dante 	char buffer[PATH_MAX], *bp;
    378  1.10    dante 	char tmp[PATH_MAX];
    379  1.10    dante 	char buildsuff[256], buildcmd[256];
    380  1.10    dante 	char crunchsuff[256], crunchcmd[256];
    381  1.10    dante 	char match[256];
    382   1.4   chopps 	struct stat manstat;
    383   1.4   chopps 	struct stat catstat;
    384  1.10    dante 	struct stat lnkstat;
    385   1.4   chopps 	struct dirent *dp;
    386   1.4   chopps 	DIR *dirp;
    387  1.10    dante 	int len, error;
    388  1.10    dante 
    389  1.10    dante 	if ((dirp = opendir(mandir)) == 0) {
    390  1.10    dante 		warn("can't open %s", mandir);
    391  1.10    dante 		return;
    392  1.10    dante 	}
    393   1.4   chopps 
    394  1.10    dante 	if (stat(catdir, &catstat) < 0) {
    395  1.10    dante 		if (errno != ENOENT) {
    396  1.10    dante 			warn("can't stat %s", catdir);
    397  1.10    dante 			closedir(dirp);
    398  1.10    dante 			return;
    399  1.10    dante 		}
    400  1.10    dante 		if (f_noprint == 0)
    401  1.10    dante 			printf("mkdir %s\n", catdir);
    402  1.10    dante 		if (f_noaction == 0 && mkdir(catdir,0755) < 0) {
    403  1.10    dante 			warn("can't create %s", catdir);
    404  1.10    dante 			closedir(dirp);
    405  1.10    dante 			return;
    406   1.4   chopps 		}
    407  1.10    dante 	}
    408  1.10    dante 
    409  1.10    dante 	while ((dp = readdir(dirp)) != NULL) {
    410  1.10    dante 		if (strcmp(dp->d_name, ".") == 0 ||
    411  1.10    dante 		    strcmp(dp->d_name, "..") == 0)
    412  1.10    dante 			continue;
    413  1.10    dante 
    414  1.10    dante 		snprintf(manpage, sizeof(manpage), "%s/%s", mandir, dp->d_name);
    415  1.10    dante 		snprintf(catpage, sizeof(catpage), "%s/%s", catdir, dp->d_name);
    416   1.4   chopps 
    417  1.13  mycroft 		e_build = NULL;
    418  1.10    dante 		buildp = getlist("_build");
    419  1.13  mycroft 		if(buildp) {
    420  1.13  mycroft 			for(e_build = buildp->list.tqh_first; e_build;
    421  1.13  mycroft 					e_build = e_build->q.tqe_next) {
    422  1.13  mycroft 				splitentry(e_build->s, buildsuff, buildcmd);
    423  1.13  mycroft 				snprintf(match, sizeof(match), "*%s",
    424  1.13  mycroft 							buildsuff);
    425  1.13  mycroft 				if(!fnmatch(match, manpage, 0))
    426  1.13  mycroft 					break;
    427  1.13  mycroft 			}
    428  1.10    dante 		}
    429   1.4   chopps 
    430  1.13  mycroft 		if(e_build == NULL)
    431   1.4   chopps 			continue;
    432  1.13  mycroft 
    433  1.13  mycroft 		e_crunch = NULL;
    434  1.13  mycroft 		crunchp = getlist("_crunch");
    435  1.13  mycroft 		if(crunchp) {
    436  1.13  mycroft 			for(e_crunch = crunchp->list.tqh_first; e_crunch;
    437  1.13  mycroft 					e_crunch=e_crunch->q.tqe_next) {
    438  1.13  mycroft 				splitentry(e_crunch->s, crunchsuff, crunchcmd);
    439  1.13  mycroft 				snprintf(match, sizeof(match), "*%s",
    440  1.13  mycroft 							crunchsuff);
    441  1.13  mycroft 				if(!fnmatch(match, manpage, 0))
    442  1.13  mycroft 					break;
    443  1.10    dante 			}
    444   1.4   chopps 		}
    445   1.4   chopps 
    446  1.10    dante 		if (lstat(manpage, &manstat) <0) {
    447  1.10    dante 			warn("can't stat %s", manpage);
    448  1.10    dante 			continue;
    449  1.10    dante 		} else {
    450  1.10    dante 			if(S_ISLNK(manstat.st_mode)) {
    451  1.10    dante 				strcpy(buffer, catpage);
    452  1.10    dante 				strcpy(linkname, basename(buffer));
    453  1.10    dante 				len = readlink(manpage, buffer, PATH_MAX);
    454  1.10    dante 				if(len == -1) {
    455  1.10    dante 					warn("can't stat read symbolic link %s",
    456  1.10    dante 							manpage);
    457  1.10    dante 					continue;
    458  1.10    dante 				}
    459  1.10    dante 				buffer[len] = '\0';
    460  1.10    dante 				bp = basename(buffer);
    461  1.10    dante 				strcpy(tmp, manpage);
    462  1.10    dante 				snprintf(manpage, sizeof(manpage), "%s/%s",
    463  1.10    dante 						dirname(tmp), bp);
    464  1.10    dante 				strcpy(tmp, catpage);
    465  1.10    dante 				snprintf(catpage, sizeof(catpage), "%s/%s",
    466  1.10    dante 						dirname(tmp), buffer);
    467   1.1      jtc 			}
    468  1.10    dante 			else
    469  1.10    dante 				*linkname='\0';
    470  1.10    dante 		}
    471  1.10    dante 
    472  1.10    dante 		if(!e_crunch) {
    473  1.10    dante 			*crunchsuff = *crunchcmd = '\0';
    474  1.10    dante 		}
    475  1.10    dante 		setcatsuffix(catpage, buildsuff, crunchsuff);
    476  1.10    dante 		if(*linkname != '\0')
    477  1.10    dante 			setcatsuffix(linkname, buildsuff, crunchsuff);
    478   1.1      jtc 
    479  1.10    dante 		if (stat(manpage, &manstat) < 0) {
    480  1.10    dante 			warn("can't stat %s", manpage);
    481  1.10    dante 			continue;
    482   1.1      jtc 		}
    483   1.1      jtc 
    484  1.10    dante 		if (!S_ISREG(manstat.st_mode)) {
    485  1.10    dante 			warnx("not a regular file %s",manpage);
    486  1.10    dante 			continue;
    487  1.10    dante 		}
    488   1.4   chopps 
    489  1.10    dante 		if ((error = stat(catpage, &catstat)) &&
    490  1.10    dante 		    errno != ENOENT) {
    491  1.10    dante 			warn("can't stat %s", catpage);
    492  1.10    dante 			continue;
    493  1.10    dante 		}
    494   1.4   chopps 
    495  1.10    dante 		if ((error && errno == ENOENT) ||
    496  1.11  mycroft 		    manstat.st_mtime > catstat.st_mtime) {
    497  1.10    dante 			if (f_noformat) {
    498  1.10    dante 				dowhatis = 1;
    499  1.10    dante 			} else {
    500  1.10    dante 				/*
    501  1.10    dante 				 * reformat out of date manpage
    502  1.10    dante 				 */
    503  1.10    dante 				makecat(manpage, catpage, buildcmd, crunchcmd);
    504  1.10    dante 				dowhatis = 1;
    505   1.4   chopps 			}
    506  1.10    dante 		}
    507   1.4   chopps 
    508  1.10    dante 		if(*linkname != '\0') {
    509  1.10    dante 			strcpy(tmp, catpage);
    510  1.10    dante 			snprintf(tmp, sizeof(tmp), "%s/%s", dirname(tmp),
    511  1.10    dante 					linkname);
    512  1.10    dante 			if ((error = lstat(tmp, &lnkstat)) &&
    513   1.5   chopps 			    errno != ENOENT) {
    514  1.10    dante 				warn("can't stat %s", tmp);
    515   1.4   chopps 				continue;
    516   1.4   chopps 			}
    517   1.4   chopps 
    518  1.10    dante 			if (error && errno == ENOENT)  {
    519  1.10    dante 				if (f_noformat) {
    520   1.5   chopps 					dowhatis = 1;
    521  1.10    dante 				} else {
    522   1.5   chopps 					/*
    523  1.10    dante 					 * create symbolic link
    524   1.5   chopps 					 */
    525   1.5   chopps 					if (f_noprint == 0)
    526  1.10    dante 						printf("ln -s %s %s\n", catpage,
    527  1.10    dante 								 linkname);
    528  1.10    dante 					if (f_noaction == 0) {
    529  1.10    dante 						strcpy(tmp, catpage);
    530  1.10    dante 						if(chdir(dirname(tmp)) == -1) {
    531  1.10    dante 							warn("can't chdir");
    532  1.10    dante 							continue;
    533  1.10    dante 						}
    534  1.10    dante 
    535  1.10    dante 						if(symlink(catpage, linkname)
    536  1.10    dante 								== -1) {
    537  1.10    dante 							warn("can't create"
    538  1.10    dante 								" symbolic"
    539  1.10    dante 								" link %s",
    540  1.10    dante 								linkname);
    541  1.10    dante 							continue;
    542  1.10    dante 						}
    543  1.10    dante 					}
    544   1.5   chopps 					dowhatis = 1;
    545   1.5   chopps 				}
    546   1.4   chopps 			}
    547   1.4   chopps 		}
    548   1.1      jtc 	}
    549  1.10    dante 	closedir(dirp);
    550   1.1      jtc }
    551   1.1      jtc 
    552  1.10    dante static int
    553  1.10    dante splitentry(s, first, second)
    554  1.10    dante 	char *s;
    555  1.10    dante 	char *first;
    556  1.10    dante 	char *second;
    557   1.1      jtc {
    558  1.10    dante 	char *c;
    559  1.10    dante 
    560  1.10    dante 	for(c = s; *c != '\0' && !isspace(*c); ++c);
    561  1.10    dante 	if(*c == '\0')
    562  1.10    dante 		return(0);
    563  1.10    dante 	strncpy(first, s, c-s);
    564  1.10    dante 	first[c-s] = '\0';
    565  1.10    dante 	for(; *c != '\0' && isspace(*c); ++c);
    566  1.10    dante 	strcpy(second, c);
    567  1.10    dante 		return(1);
    568  1.10    dante }
    569  1.10    dante 
    570  1.10    dante static void
    571  1.10    dante setcatsuffix(catpage, suffix, crunchsuff)
    572  1.10    dante 	char		*catpage;
    573  1.10    dante 	const char	*suffix;
    574  1.10    dante 	const char	*crunchsuff;
    575  1.10    dante {
    576  1.10    dante 	TAG *tp;
    577  1.10    dante 	ENTRY *ep;
    578  1.10    dante 	char *p;
    579  1.10    dante 
    580  1.10    dante 	for(p = catpage + strlen(catpage); p!=catpage; p--)
    581  1.10    dante 		if(!fnmatch(suffix, p, 0)) {
    582  1.10    dante 			if((tp = getlist("_suffix"))) {
    583  1.10    dante 				ep = tp->list.tqh_first;
    584  1.10    dante 				sprintf(p, "%s%s", ep->s, crunchsuff);
    585  1.10    dante 			} else {
    586  1.10    dante 				sprintf(p, ".0%s", crunchsuff);
    587  1.10    dante 			}
    588  1.10    dante 			break;
    589  1.10    dante 		}
    590  1.10    dante }
    591  1.10    dante 
    592  1.10    dante static void
    593  1.10    dante makecat(manpage, catpage, buildcmd, crunchcmd)
    594  1.10    dante 	const char	*manpage;
    595  1.10    dante 	const char	*catpage;
    596  1.10    dante 	const char	*buildcmd;
    597  1.10    dante 	const char	*crunchcmd;
    598  1.10    dante {
    599  1.10    dante 	char crunchbuf[1024];
    600  1.10    dante 	char sysbuf[2048];
    601  1.10    dante 
    602  1.10    dante 	snprintf(sysbuf, sizeof(sysbuf), buildcmd, manpage);
    603  1.10    dante 
    604  1.10    dante 	if(*crunchcmd != '\0') {
    605  1.10    dante 		snprintf(crunchbuf, sizeof(crunchbuf), crunchcmd, catpage);
    606  1.10    dante 		snprintf(sysbuf, sizeof(sysbuf), "%s | %s", sysbuf, crunchbuf);
    607  1.10    dante 	} else {
    608  1.10    dante 		snprintf(sysbuf, sizeof(sysbuf), "%s > %s", sysbuf, catpage);
    609  1.10    dante 	}
    610   1.1      jtc 
    611   1.4   chopps 	if (f_noprint == 0)
    612   1.1      jtc 		printf("%s\n", sysbuf);
    613   1.4   chopps 	if (f_noaction == 0)
    614   1.4   chopps 		dosystem(sysbuf);
    615   1.1      jtc }
    616   1.1      jtc 
    617  1.10    dante static void
    618  1.10    dante makewhatis(void)
    619  1.10    dante {
    620  1.10    dante 	TAG *whatdbp;
    621  1.10    dante 	ENTRY *e_whatdb;
    622  1.10    dante 	char sysbuf[1024];
    623  1.10    dante 
    624  1.10    dante 	whatdbp = getlist("_whatdb");
    625  1.10    dante 	for(e_whatdb = whatdbp->list.tqh_first; e_whatdb;
    626  1.10    dante 			e_whatdb = e_whatdb->q.tqe_next) {
    627  1.10    dante 		snprintf(sysbuf, sizeof(sysbuf), "%s %s",
    628  1.10    dante 				_PATH_WHATIS, dirname(e_whatdb->s));
    629  1.10    dante 		if (f_noprint == 0)
    630  1.10    dante 			printf("%s\n", sysbuf);
    631  1.10    dante 		if (f_noaction == 0)
    632  1.10    dante 			dosystem(sysbuf);
    633  1.10    dante 	}
    634  1.10    dante }
    635  1.10    dante 
    636  1.10    dante static void
    637   1.4   chopps dosystem(cmd)
    638   1.4   chopps 	const char *cmd;
    639   1.4   chopps {
    640   1.4   chopps 	int status;
    641   1.4   chopps 
    642   1.4   chopps 	if ((status = system(cmd)) == 0)
    643   1.4   chopps 		return;
    644   1.4   chopps 
    645   1.4   chopps 	if (status == -1)
    646   1.4   chopps 		err(1, "cannot execute action");
    647   1.4   chopps 	if (WIFSIGNALED(status))
    648   1.4   chopps 		errx(1, "child was signaled to quit. aborting");
    649   1.4   chopps 	if (WIFSTOPPED(status))
    650   1.4   chopps 		errx(1, "child was stopped. aborting");
    651   1.4   chopps 	if (f_ignerr == 0)
    652   1.7    lukem 		errx(1, "*** Exited %d", status);
    653   1.7    lukem 	warnx("*** Exited %d (continuing)", status);
    654   1.4   chopps }
    655   1.1      jtc 
    656  1.10    dante static void
    657  1.10    dante usage(void)
    658   1.1      jtc {
    659   1.6      cgd 	(void)fprintf(stderr,
    660  1.10    dante 	    "usage: catman [-knpsw] [-m manpath] [sections]\n");
    661  1.10    dante 	(void)fprintf(stderr,
    662  1.10    dante 	    "       catman [-knpsw] [-M manpath] [sections]\n");
    663   1.1      jtc 	exit(1);
    664   1.1      jtc }
    665