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