Home | History | Annotate | Line # | Download | only in catman
catman.c revision 1.3
      1  1.1      jtc /*
      2  1.1      jtc  * Copyright (c) 1993 Winning Strategies, Inc.
      3  1.1      jtc  * All rights reserved.
      4  1.1      jtc  *
      5  1.1      jtc  * Redistribution and use in source and binary forms, with or without
      6  1.1      jtc  * modification, are permitted provided that the following conditions
      7  1.1      jtc  * are met:
      8  1.1      jtc  * 1. Redistributions of source code must retain the above copyright
      9  1.1      jtc  *    notice, this list of conditions and the following disclaimer.
     10  1.1      jtc  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      jtc  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      jtc  *    documentation and/or other materials provided with the distribution.
     13  1.1      jtc  * 3. All advertising materials mentioning features or use of this software
     14  1.1      jtc  *    must display the following acknowledgement:
     15  1.1      jtc  *      This product includes software developed by Winning Strategies, Inc.
     16  1.1      jtc  * 4. The name of the author may not be used to endorse or promote products
     17  1.3      jtc  *    derived from this software without specific prior written permission
     18  1.1      jtc  *
     19  1.1      jtc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1      jtc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1      jtc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1      jtc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1      jtc  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1      jtc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1      jtc  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1      jtc  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1      jtc  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1      jtc  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1      jtc  */
     30  1.2  mycroft 
     31  1.2  mycroft #ifndef lint
     32  1.3      jtc static char rcsid[] = "$Id: catman.c,v 1.3 1994/01/29 01:43:13 jtc Exp $";
     33  1.2  mycroft #endif /* not lint */
     34  1.1      jtc 
     35  1.1      jtc #include <stdio.h>
     36  1.1      jtc #include <stdlib.h>
     37  1.1      jtc #include <string.h>
     38  1.1      jtc #include <sys/types.h>
     39  1.1      jtc #include <sys/stat.h>
     40  1.1      jtc #include <dirent.h>
     41  1.1      jtc #include <unistd.h>
     42  1.1      jtc #include <limits.h>
     43  1.1      jtc #include <errno.h>
     44  1.1      jtc #include <err.h>
     45  1.1      jtc 
     46  1.1      jtc int             no_whatis = 0;
     47  1.1      jtc int             just_print = 0;
     48  1.1      jtc int             just_whatis = 0;
     49  1.1      jtc 
     50  1.1      jtc char            manpath[] = "/usr/share/man";
     51  1.1      jtc char            sections[] = "12345678lnop";
     52  1.1      jtc char           *mp = manpath;
     53  1.1      jtc char	       *sp = sections;
     54  1.1      jtc 
     55  1.1      jtc void            catman();
     56  1.1      jtc void            makewhatis();
     57  1.1      jtc void            usage();
     58  1.1      jtc 
     59  1.1      jtc int
     60  1.1      jtc main(argc, argv)
     61  1.1      jtc 	int             argc;
     62  1.1      jtc 	char          **argv;
     63  1.1      jtc {
     64  1.1      jtc 	int             c;
     65  1.1      jtc 
     66  1.1      jtc 	while ((c = getopt(argc, argv, "npwM:")) != EOF) {
     67  1.1      jtc 		switch (c) {
     68  1.1      jtc 		case 'n':
     69  1.1      jtc 			no_whatis = 1;
     70  1.1      jtc 			break;
     71  1.1      jtc 		case 'p':
     72  1.1      jtc 			just_print = 1;
     73  1.1      jtc 			break;
     74  1.1      jtc 		case 'w':
     75  1.1      jtc 			just_whatis = 1;
     76  1.1      jtc 			break;
     77  1.1      jtc 
     78  1.1      jtc 		case 'M':
     79  1.1      jtc 			mp = optarg;
     80  1.1      jtc 			break;
     81  1.1      jtc 
     82  1.1      jtc 		case '?':
     83  1.1      jtc 		default:
     84  1.1      jtc 			usage();
     85  1.1      jtc 		}
     86  1.1      jtc 	}
     87  1.1      jtc 
     88  1.1      jtc 	argc -= optind;
     89  1.1      jtc 	argv += optind;
     90  1.1      jtc 
     91  1.1      jtc 	if (argc > 1)
     92  1.1      jtc 		usage ();
     93  1.1      jtc 	if (argc == 0)
     94  1.1      jtc 		sp = *argv;
     95  1.1      jtc 
     96  1.1      jtc 	catman(mp, sp);
     97  1.1      jtc 
     98  1.1      jtc 	exit(0);
     99  1.1      jtc }
    100  1.1      jtc 
    101  1.1      jtc 
    102  1.1      jtc void
    103  1.1      jtc catman(path, section)
    104  1.1      jtc 	char           *path;
    105  1.1      jtc 	char           *section;
    106  1.1      jtc {
    107  1.1      jtc 	DIR            *dirp;
    108  1.1      jtc 	struct dirent  *dp;
    109  1.1      jtc 	struct stat     manstat;
    110  1.1      jtc 	struct stat	catstat;
    111  1.1      jtc 	char            mandir[PATH_MAX];
    112  1.1      jtc 	char            catdir[PATH_MAX];
    113  1.1      jtc 	char            manpage[PATH_MAX];
    114  1.1      jtc 	char            catpage[PATH_MAX];
    115  1.1      jtc 	char            sysbuf[1024];
    116  1.1      jtc 	char           *s;
    117  1.1      jtc 	int             formatted = 0;
    118  1.1      jtc 
    119  1.1      jtc 	if (!just_whatis) {
    120  1.1      jtc 		for (s = section; *s; s++) {
    121  1.1      jtc 			sprintf(mandir, "%s/man%c", path, *s);
    122  1.1      jtc 			sprintf(catdir, "%s/cat%c", path, *s);
    123  1.1      jtc 
    124  1.1      jtc 			if ((dirp = opendir(mandir)) == 0) {
    125  1.1      jtc 				warn("can't open %s", mandir);
    126  1.1      jtc 				continue;
    127  1.1      jtc 			}
    128  1.1      jtc 
    129  1.1      jtc 			if (stat(catdir, &catstat) < 0) {
    130  1.1      jtc 				if (errno != ENOENT) {
    131  1.1      jtc 					warn("can't stat %s", catdir);
    132  1.1      jtc 					closedir(dirp);
    133  1.1      jtc 					continue;
    134  1.1      jtc 				}
    135  1.1      jtc 				if (just_print) {
    136  1.1      jtc 					printf("mkdir %s\n", catdir);
    137  1.1      jtc 				} else if (mkdir(catdir, 0755) < 0) {
    138  1.1      jtc 					warn("can't create %s", catdir);
    139  1.1      jtc 					closedir(dirp);
    140  1.1      jtc 					return;
    141  1.1      jtc 				}
    142  1.1      jtc 			}
    143  1.1      jtc 
    144  1.1      jtc 			while ((dp = readdir(dirp)) != NULL) {
    145  1.1      jtc 				if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
    146  1.1      jtc 					continue;
    147  1.1      jtc 
    148  1.1      jtc 				sprintf(manpage, "%s/%s", mandir, dp->d_name);
    149  1.1      jtc 				sprintf(catpage, "%s/%s", catdir, dp->d_name);
    150  1.1      jtc 				if ((s = strrchr(catpage, '.')) != NULL)
    151  1.1      jtc 					strcpy(s, ".0");
    152  1.1      jtc 
    153  1.1      jtc 				if (stat(manpage, &manstat) < 0) {
    154  1.1      jtc 					warn("can't stat %s", manpage);
    155  1.1      jtc 					continue;
    156  1.1      jtc 				}
    157  1.1      jtc 
    158  1.1      jtc 				if (!S_ISREG(manstat.st_mode)) {
    159  1.1      jtc 					warnx("not a regular file %s", manpage);
    160  1.1      jtc 					continue;
    161  1.1      jtc 				}
    162  1.1      jtc 
    163  1.1      jtc 				if (stat(catpage, &catstat) < 0 && errno != ENOENT) {
    164  1.1      jtc 					warn("can't stat %s", catpage);
    165  1.1      jtc 					continue;
    166  1.1      jtc 				}
    167  1.1      jtc 
    168  1.1      jtc 				if (errno == ENOENT || manstat.st_mtime >= catstat.st_mtime) {
    169  1.1      jtc 					/*
    170  1.1      jtc 					 * manpage is out of date,
    171  1.1      jtc 					 * reformat
    172  1.1      jtc 					 */
    173  1.1      jtc 					sprintf(sysbuf, "nroff -mandoc %s > %s", manpage, catpage);
    174  1.1      jtc 
    175  1.1      jtc 					if (just_print) {
    176  1.1      jtc 						printf("%s\n", sysbuf);
    177  1.1      jtc 					} else {
    178  1.1      jtc 						if (system(sysbuf) != 0) {
    179  1.1      jtc 						}
    180  1.1      jtc 					}
    181  1.1      jtc 					formatted = 1;
    182  1.1      jtc 				}
    183  1.1      jtc 			}
    184  1.1      jtc 
    185  1.1      jtc 			closedir(dirp);
    186  1.1      jtc 		}
    187  1.1      jtc 	}
    188  1.1      jtc 
    189  1.1      jtc 	if (!no_whatis && formatted) {
    190  1.1      jtc 		makewhatis(path);
    191  1.1      jtc 	}
    192  1.1      jtc }
    193  1.1      jtc 
    194  1.1      jtc void
    195  1.1      jtc makewhatis(path)
    196  1.1      jtc {
    197  1.1      jtc 	char            sysbuf[1024];
    198  1.1      jtc 
    199  1.1      jtc 	sprintf(sysbuf, "/usr/libexec/makewhatis %s", path);
    200  1.1      jtc 	if (just_print) {
    201  1.1      jtc 		printf("%s\n", sysbuf);
    202  1.1      jtc 	} else {
    203  1.1      jtc 		if (system(sysbuf) != 0) {
    204  1.1      jtc 		}
    205  1.1      jtc 	}
    206  1.1      jtc }
    207  1.1      jtc 
    208  1.1      jtc 
    209  1.1      jtc void
    210  1.1      jtc usage()
    211  1.1      jtc {
    212  1.1      jtc 	(void) fprintf(stderr, "usage: catman [-npw] [-M manpath] [sections]\n");
    213  1.1      jtc 	exit(1);
    214  1.1      jtc }
    215