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