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