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