catman.c revision 1.2 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
31 #ifndef lint
32 static char rcsid[] = "$Id: catman.c,v 1.2 1993/08/02 17:54:59 mycroft Exp $";
33 #endif /* not lint */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <dirent.h>
41 #include <unistd.h>
42 #include <limits.h>
43 #include <errno.h>
44 #include <err.h>
45
46 int no_whatis = 0;
47 int just_print = 0;
48 int just_whatis = 0;
49
50 char manpath[] = "/usr/share/man";
51 char sections[] = "12345678lnop";
52 char *mp = manpath;
53 char *sp = sections;
54
55 void catman();
56 void makewhatis();
57 void usage();
58
59 int
60 main(argc, argv)
61 int argc;
62 char **argv;
63 {
64 int c;
65
66 while ((c = getopt(argc, argv, "npwM:")) != EOF) {
67 switch (c) {
68 case 'n':
69 no_whatis = 1;
70 break;
71 case 'p':
72 just_print = 1;
73 break;
74 case 'w':
75 just_whatis = 1;
76 break;
77
78 case 'M':
79 mp = optarg;
80 break;
81
82 case '?':
83 default:
84 usage();
85 }
86 }
87
88 argc -= optind;
89 argv += optind;
90
91 if (argc > 1)
92 usage ();
93 if (argc == 0)
94 sp = *argv;
95
96 catman(mp, sp);
97
98 exit(0);
99 }
100
101
102 void
103 catman(path, section)
104 char *path;
105 char *section;
106 {
107 DIR *dirp;
108 struct dirent *dp;
109 struct stat manstat;
110 struct stat catstat;
111 char mandir[PATH_MAX];
112 char catdir[PATH_MAX];
113 char manpage[PATH_MAX];
114 char catpage[PATH_MAX];
115 char sysbuf[1024];
116 char *s;
117 int formatted = 0;
118
119 if (!just_whatis) {
120 for (s = section; *s; s++) {
121 sprintf(mandir, "%s/man%c", path, *s);
122 sprintf(catdir, "%s/cat%c", path, *s);
123
124 if ((dirp = opendir(mandir)) == 0) {
125 warn("can't open %s", mandir);
126 continue;
127 }
128
129 if (stat(catdir, &catstat) < 0) {
130 if (errno != ENOENT) {
131 warn("can't stat %s", catdir);
132 closedir(dirp);
133 continue;
134 }
135 if (just_print) {
136 printf("mkdir %s\n", catdir);
137 } else if (mkdir(catdir, 0755) < 0) {
138 warn("can't create %s", catdir);
139 closedir(dirp);
140 return;
141 }
142 }
143
144 while ((dp = readdir(dirp)) != NULL) {
145 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
146 continue;
147
148 sprintf(manpage, "%s/%s", mandir, dp->d_name);
149 sprintf(catpage, "%s/%s", catdir, dp->d_name);
150 if ((s = strrchr(catpage, '.')) != NULL)
151 strcpy(s, ".0");
152
153 if (stat(manpage, &manstat) < 0) {
154 warn("can't stat %s", manpage);
155 continue;
156 }
157
158 if (!S_ISREG(manstat.st_mode)) {
159 warnx("not a regular file %s", manpage);
160 continue;
161 }
162
163 if (stat(catpage, &catstat) < 0 && errno != ENOENT) {
164 warn("can't stat %s", catpage);
165 continue;
166 }
167
168 if (errno == ENOENT || manstat.st_mtime >= catstat.st_mtime) {
169 /*
170 * manpage is out of date,
171 * reformat
172 */
173 sprintf(sysbuf, "nroff -mandoc %s > %s", manpage, catpage);
174
175 if (just_print) {
176 printf("%s\n", sysbuf);
177 } else {
178 if (system(sysbuf) != 0) {
179 }
180 }
181 formatted = 1;
182 }
183 }
184
185 closedir(dirp);
186 }
187 }
188
189 if (!no_whatis && formatted) {
190 makewhatis(path);
191 }
192 }
193
194 void
195 makewhatis(path)
196 {
197 char sysbuf[1024];
198
199 sprintf(sysbuf, "/usr/libexec/makewhatis %s", path);
200 if (just_print) {
201 printf("%s\n", sysbuf);
202 } else {
203 if (system(sysbuf) != 0) {
204 }
205 }
206 }
207
208
209 void
210 usage()
211 {
212 (void) fprintf(stderr, "usage: catman [-npw] [-M manpath] [sections]\n");
213 exit(1);
214 }
215