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