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