Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /*- |
2 | * Copyright (c) 1980 The Regents of the University of California. | |
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 the University of | |
16 | * California, Berkeley and its contributors. | |
17 | * 4. Neither the name of the University nor the names of its contributors | |
18 | * may be used to endorse or promote products derived from this software | |
19 | * without specific prior written permission. | |
20 | * | |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
35 | char copyright[] = | |
36 | "@(#) Copyright (c) 1980 The Regents of the University of California.\n\ | |
37 | All rights reserved.\n"; | |
38 | #endif /* not lint */ | |
39 | ||
40 | #ifndef lint | |
41 | static char sccsid[] = "@(#)whereis.c 5.5 (Berkeley) 4/18/91"; | |
42 | #endif /* not lint */ | |
43 | ||
44 | #include <sys/param.h> | |
45 | #include <sys/dir.h> | |
46 | #include <stdio.h> | |
47 | #include <ctype.h> | |
48 | ||
49 | static char *bindirs[] = { | |
50 | "/bin", | |
51 | "/sbin", | |
15637ed4 | 52 | "/usr/bin", |
15637ed4 | 53 | "/usr/games", |
9f97dc86 | 54 | "/usr/gnu/bin", |
15637ed4 | 55 | "/usr/include", |
0cfe38bb RG |
56 | "/usr/libexec", |
57 | "/usr/sbin", | |
58 | "/usr/share", | |
59 | "/usr/X386/bin", | |
11cc4440 | 60 | "/usr/gnu/bin", |
0cfe38bb | 61 | "/usr/local/bin", |
15637ed4 | 62 | "/etc", |
15637ed4 RG |
63 | 0 |
64 | }; | |
65 | /* This needs to be redone - man pages live with sources */ | |
66 | static char *mandirs[] = { | |
d1e5ffd9 RG |
67 | "/usr/share/man/man1", |
68 | "/usr/share/man/man2", | |
69 | "/usr/share/man/man3", | |
70 | "/usr/share/man/man4", | |
71 | "/usr/share/man/man5", | |
72 | "/usr/share/man/man6", | |
73 | "/usr/share/man/man7", | |
74 | "/usr/share/man/man8", | |
0cfe38bb RG |
75 | "/usr/X386/man/man1", |
76 | "/usr/X386/man/man2", | |
77 | "/usr/X386/man/man3", | |
78 | "/usr/X386/man/man4", | |
79 | "/usr/X386/man/man5", | |
80 | "/usr/X386/man/man6", | |
81 | "/usr/X386/man/man7", | |
82 | "/usr/X386/man/man8", | |
83 | "/usr/X386/man/cat1", | |
84 | "/usr/X386/man/cat2", | |
85 | "/usr/X386/man/cat3", | |
86 | "/usr/X386/man/cat4", | |
87 | "/usr/X386/man/cat5", | |
88 | "/usr/X386/man/cat6", | |
89 | "/usr/X386/man/cat7", | |
90 | "/usr/X386/man/cat8", | |
d1e5ffd9 RG |
91 | "/usr/local/man/man1", |
92 | "/usr/local/man/man2", | |
93 | "/usr/local/man/man3", | |
94 | "/usr/local/man/man4", | |
95 | "/usr/local/man/man5", | |
96 | "/usr/local/man/man6", | |
97 | "/usr/local/man/man7", | |
98 | "/usr/local/man/man8", | |
11cc4440 AC |
99 | "/usr/gnu/man/man1", |
100 | "/usr/gnu/man/man2", | |
101 | "/usr/gnu/man/man3", | |
102 | "/usr/gnu/man/man4", | |
103 | "/usr/gnu/man/man5", | |
104 | "/usr/gnu/man/man6", | |
105 | "/usr/gnu/man/man7", | |
106 | "/usr/gnu/man/man8", | |
15637ed4 RG |
107 | 0 |
108 | }; | |
109 | static char *srcdirs[] = { | |
110 | "/usr/src/bin", | |
15637ed4 | 111 | "/usr/src/contrib", |
15637ed4 | 112 | "/usr/src/etc", |
15637ed4 | 113 | "/usr/src/games", |
9f97dc86 RG |
114 | "/usr/src/gnu/lib", |
115 | "/usr/src/gnu/libexec", | |
116 | "/usr/src/gnu/usr.bin", | |
15637ed4 | 117 | "/usr/src/include", |
0cfe38bb RG |
118 | "/usr/src/lib", |
119 | "/usr/src/libexec", | |
120 | "/usr/src/sbin", | |
121 | "/usr/src/share", | |
122 | "/usr/src/usr.bin", | |
123 | "/usr/src/usr.sbin", | |
124 | "/usr/src/share/man", | |
125 | "/usr/local/src", | |
15637ed4 RG |
126 | 0 |
127 | }; | |
128 | ||
129 | char sflag = 1; | |
130 | char bflag = 1; | |
131 | char mflag = 1; | |
132 | char **Sflag; | |
133 | int Scnt; | |
134 | char **Bflag; | |
135 | int Bcnt; | |
136 | char **Mflag; | |
137 | int Mcnt; | |
138 | char uflag; | |
139 | /* | |
140 | * whereis name | |
141 | * look for source, documentation and binaries | |
142 | */ | |
143 | main(argc, argv) | |
144 | int argc; | |
145 | char *argv[]; | |
146 | { | |
147 | ||
148 | argc--, argv++; | |
149 | if (argc == 0) { | |
150 | usage: | |
151 | fprintf(stderr, "whereis [ -sbmu ] [ -SBM dir ... -f ] name...\n"); | |
152 | exit(1); | |
153 | } | |
154 | do | |
155 | if (argv[0][0] == '-') { | |
156 | register char *cp = argv[0] + 1; | |
157 | while (*cp) switch (*cp++) { | |
158 | ||
159 | case 'f': | |
160 | break; | |
161 | ||
162 | case 'S': | |
163 | getlist(&argc, &argv, &Sflag, &Scnt); | |
164 | break; | |
165 | ||
166 | case 'B': | |
167 | getlist(&argc, &argv, &Bflag, &Bcnt); | |
168 | break; | |
169 | ||
170 | case 'M': | |
171 | getlist(&argc, &argv, &Mflag, &Mcnt); | |
172 | break; | |
173 | ||
174 | case 's': | |
175 | zerof(); | |
176 | sflag++; | |
177 | continue; | |
178 | ||
179 | case 'u': | |
180 | uflag++; | |
181 | continue; | |
182 | ||
183 | case 'b': | |
184 | zerof(); | |
185 | bflag++; | |
186 | continue; | |
187 | ||
188 | case 'm': | |
189 | zerof(); | |
190 | mflag++; | |
191 | continue; | |
192 | ||
193 | default: | |
194 | goto usage; | |
195 | } | |
196 | argv++; | |
197 | } else | |
198 | lookup(*argv++); | |
199 | while (--argc > 0); | |
200 | exit(0); | |
201 | } | |
202 | ||
203 | getlist(argcp, argvp, flagp, cntp) | |
204 | char ***argvp; | |
205 | int *argcp; | |
206 | char ***flagp; | |
207 | int *cntp; | |
208 | { | |
209 | ||
210 | (*argvp)++; | |
211 | *flagp = *argvp; | |
212 | *cntp = 0; | |
213 | for ((*argcp)--; *argcp > 0 && (*argvp)[0][0] != '-'; (*argcp)--) | |
214 | (*cntp)++, (*argvp)++; | |
215 | (*argcp)++; | |
216 | (*argvp)--; | |
217 | } | |
218 | ||
219 | ||
220 | zerof() | |
221 | { | |
222 | ||
223 | if (sflag && bflag && mflag) | |
224 | sflag = bflag = mflag = 0; | |
225 | } | |
226 | int count; | |
227 | int print; | |
228 | ||
229 | ||
230 | lookup(cp) | |
231 | register char *cp; | |
232 | { | |
233 | register char *dp; | |
234 | ||
235 | for (dp = cp; *dp; dp++) | |
236 | continue; | |
237 | for (; dp > cp; dp--) { | |
238 | if (*dp == '.') { | |
239 | *dp = 0; | |
240 | break; | |
241 | } | |
242 | } | |
243 | for (dp = cp; *dp; dp++) | |
244 | if (*dp == '/') | |
245 | cp = dp + 1; | |
246 | if (uflag) { | |
247 | print = 0; | |
248 | count = 0; | |
249 | } else | |
250 | print = 1; | |
251 | again: | |
252 | if (print) | |
253 | printf("%s:", cp); | |
254 | if (sflag) { | |
255 | looksrc(cp); | |
256 | if (uflag && print == 0 && count != 1) { | |
257 | print = 1; | |
258 | goto again; | |
259 | } | |
260 | } | |
261 | count = 0; | |
262 | if (bflag) { | |
263 | lookbin(cp); | |
264 | if (uflag && print == 0 && count != 1) { | |
265 | print = 1; | |
266 | goto again; | |
267 | } | |
268 | } | |
269 | count = 0; | |
270 | if (mflag) { | |
271 | lookman(cp); | |
272 | if (uflag && print == 0 && count != 1) { | |
273 | print = 1; | |
274 | goto again; | |
275 | } | |
276 | } | |
277 | if (print) | |
278 | printf("\n"); | |
279 | } | |
280 | ||
281 | looksrc(cp) | |
282 | char *cp; | |
283 | { | |
284 | if (Sflag == 0) { | |
285 | find(srcdirs, cp); | |
286 | } else | |
287 | findv(Sflag, Scnt, cp); | |
288 | } | |
289 | ||
290 | lookbin(cp) | |
291 | char *cp; | |
292 | { | |
293 | if (Bflag == 0) | |
294 | find(bindirs, cp); | |
295 | else | |
296 | findv(Bflag, Bcnt, cp); | |
297 | } | |
298 | ||
299 | lookman(cp) | |
300 | char *cp; | |
301 | { | |
302 | if (Mflag == 0) { | |
303 | find(mandirs, cp); | |
304 | } else | |
305 | findv(Mflag, Mcnt, cp); | |
306 | } | |
307 | ||
308 | findv(dirv, dirc, cp) | |
309 | char **dirv; | |
310 | int dirc; | |
311 | char *cp; | |
312 | { | |
313 | ||
314 | while (dirc > 0) | |
315 | findin(*dirv++, cp), dirc--; | |
316 | } | |
317 | ||
318 | find(dirs, cp) | |
319 | char **dirs; | |
320 | char *cp; | |
321 | { | |
322 | ||
323 | while (*dirs) | |
324 | findin(*dirs++, cp); | |
325 | } | |
326 | ||
327 | findin(dir, cp) | |
328 | char *dir, *cp; | |
329 | { | |
330 | DIR *dirp; | |
331 | struct direct *dp; | |
332 | ||
333 | dirp = opendir(dir); | |
334 | if (dirp == NULL) | |
335 | return; | |
336 | while ((dp = readdir(dirp)) != NULL) { | |
337 | if (itsit(cp, dp->d_name)) { | |
338 | count++; | |
339 | if (print) | |
340 | printf(" %s/%s", dir, dp->d_name); | |
341 | } | |
342 | } | |
343 | closedir(dirp); | |
344 | } | |
345 | ||
346 | itsit(cp, dp) | |
347 | register char *cp, *dp; | |
348 | { | |
349 | register int i = strlen(dp); | |
350 | ||
351 | if (dp[0] == 's' && dp[1] == '.' && itsit(cp, dp+2)) | |
352 | return (1); | |
353 | while (*cp && *dp && *cp == *dp) | |
354 | cp++, dp++, i--; | |
355 | if (*cp == 0 && *dp == 0) | |
356 | return (1); | |
357 | while (isdigit(*dp)) | |
358 | dp++; | |
359 | if (*cp == 0 && *dp++ == '.') { | |
360 | --i; | |
361 | while (i > 0 && *dp) | |
362 | if (--i, *dp++ == '.') | |
363 | return (*dp++ == 'C' && *dp++ == 0); | |
364 | return (1); | |
365 | } | |
366 | return (0); | |
367 | } |