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