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", |
d1e5ffd9 | 54 | "/usr/gnu", |
15637ed4 | 55 | "/usr/include", |
0cfe38bb RG |
56 | "/usr/libexec", |
57 | "/usr/sbin", | |
58 | "/usr/share", | |
59 | "/usr/X386/bin", | |
60 | "/usr/local/bin", | |
15637ed4 | 61 | "/etc", |
15637ed4 RG |
62 | 0 |
63 | }; | |
64 | /* This needs to be redone - man pages live with sources */ | |
65 | static char *mandirs[] = { | |
d1e5ffd9 RG |
66 | "/usr/share/man/man1", |
67 | "/usr/share/man/man2", | |
68 | "/usr/share/man/man3", | |
69 | "/usr/share/man/man4", | |
70 | "/usr/share/man/man5", | |
71 | "/usr/share/man/man6", | |
72 | "/usr/share/man/man7", | |
73 | "/usr/share/man/man8", | |
0cfe38bb RG |
74 | "/usr/X386/man/man1", |
75 | "/usr/X386/man/man2", | |
76 | "/usr/X386/man/man3", | |
77 | "/usr/X386/man/man4", | |
78 | "/usr/X386/man/man5", | |
79 | "/usr/X386/man/man6", | |
80 | "/usr/X386/man/man7", | |
81 | "/usr/X386/man/man8", | |
82 | "/usr/X386/man/cat1", | |
83 | "/usr/X386/man/cat2", | |
84 | "/usr/X386/man/cat3", | |
85 | "/usr/X386/man/cat4", | |
86 | "/usr/X386/man/cat5", | |
87 | "/usr/X386/man/cat6", | |
88 | "/usr/X386/man/cat7", | |
89 | "/usr/X386/man/cat8", | |
d1e5ffd9 RG |
90 | "/usr/local/man/man1", |
91 | "/usr/local/man/man2", | |
92 | "/usr/local/man/man3", | |
93 | "/usr/local/man/man4", | |
94 | "/usr/local/man/man5", | |
95 | "/usr/local/man/man6", | |
96 | "/usr/local/man/man7", | |
97 | "/usr/local/man/man8", | |
15637ed4 RG |
98 | 0 |
99 | }; | |
100 | static char *srcdirs[] = { | |
101 | "/usr/src/bin", | |
15637ed4 | 102 | "/usr/src/contrib", |
15637ed4 | 103 | "/usr/src/etc", |
15637ed4 | 104 | "/usr/src/games", |
0cfe38bb | 105 | "/usr/src/gnu", |
15637ed4 | 106 | "/usr/src/include", |
0cfe38bb RG |
107 | "/usr/src/lib", |
108 | "/usr/src/libexec", | |
109 | "/usr/src/sbin", | |
110 | "/usr/src/share", | |
111 | "/usr/src/usr.bin", | |
112 | "/usr/src/usr.sbin", | |
113 | "/usr/src/share/man", | |
114 | "/usr/local/src", | |
15637ed4 RG |
115 | 0 |
116 | }; | |
117 | ||
118 | char sflag = 1; | |
119 | char bflag = 1; | |
120 | char mflag = 1; | |
121 | char **Sflag; | |
122 | int Scnt; | |
123 | char **Bflag; | |
124 | int Bcnt; | |
125 | char **Mflag; | |
126 | int Mcnt; | |
127 | char uflag; | |
128 | /* | |
129 | * whereis name | |
130 | * look for source, documentation and binaries | |
131 | */ | |
132 | main(argc, argv) | |
133 | int argc; | |
134 | char *argv[]; | |
135 | { | |
136 | ||
137 | argc--, argv++; | |
138 | if (argc == 0) { | |
139 | usage: | |
140 | fprintf(stderr, "whereis [ -sbmu ] [ -SBM dir ... -f ] name...\n"); | |
141 | exit(1); | |
142 | } | |
143 | do | |
144 | if (argv[0][0] == '-') { | |
145 | register char *cp = argv[0] + 1; | |
146 | while (*cp) switch (*cp++) { | |
147 | ||
148 | case 'f': | |
149 | break; | |
150 | ||
151 | case 'S': | |
152 | getlist(&argc, &argv, &Sflag, &Scnt); | |
153 | break; | |
154 | ||
155 | case 'B': | |
156 | getlist(&argc, &argv, &Bflag, &Bcnt); | |
157 | break; | |
158 | ||
159 | case 'M': | |
160 | getlist(&argc, &argv, &Mflag, &Mcnt); | |
161 | break; | |
162 | ||
163 | case 's': | |
164 | zerof(); | |
165 | sflag++; | |
166 | continue; | |
167 | ||
168 | case 'u': | |
169 | uflag++; | |
170 | continue; | |
171 | ||
172 | case 'b': | |
173 | zerof(); | |
174 | bflag++; | |
175 | continue; | |
176 | ||
177 | case 'm': | |
178 | zerof(); | |
179 | mflag++; | |
180 | continue; | |
181 | ||
182 | default: | |
183 | goto usage; | |
184 | } | |
185 | argv++; | |
186 | } else | |
187 | lookup(*argv++); | |
188 | while (--argc > 0); | |
189 | exit(0); | |
190 | } | |
191 | ||
192 | getlist(argcp, argvp, flagp, cntp) | |
193 | char ***argvp; | |
194 | int *argcp; | |
195 | char ***flagp; | |
196 | int *cntp; | |
197 | { | |
198 | ||
199 | (*argvp)++; | |
200 | *flagp = *argvp; | |
201 | *cntp = 0; | |
202 | for ((*argcp)--; *argcp > 0 && (*argvp)[0][0] != '-'; (*argcp)--) | |
203 | (*cntp)++, (*argvp)++; | |
204 | (*argcp)++; | |
205 | (*argvp)--; | |
206 | } | |
207 | ||
208 | ||
209 | zerof() | |
210 | { | |
211 | ||
212 | if (sflag && bflag && mflag) | |
213 | sflag = bflag = mflag = 0; | |
214 | } | |
215 | int count; | |
216 | int print; | |
217 | ||
218 | ||
219 | lookup(cp) | |
220 | register char *cp; | |
221 | { | |
222 | register char *dp; | |
223 | ||
224 | for (dp = cp; *dp; dp++) | |
225 | continue; | |
226 | for (; dp > cp; dp--) { | |
227 | if (*dp == '.') { | |
228 | *dp = 0; | |
229 | break; | |
230 | } | |
231 | } | |
232 | for (dp = cp; *dp; dp++) | |
233 | if (*dp == '/') | |
234 | cp = dp + 1; | |
235 | if (uflag) { | |
236 | print = 0; | |
237 | count = 0; | |
238 | } else | |
239 | print = 1; | |
240 | again: | |
241 | if (print) | |
242 | printf("%s:", cp); | |
243 | if (sflag) { | |
244 | looksrc(cp); | |
245 | if (uflag && print == 0 && count != 1) { | |
246 | print = 1; | |
247 | goto again; | |
248 | } | |
249 | } | |
250 | count = 0; | |
251 | if (bflag) { | |
252 | lookbin(cp); | |
253 | if (uflag && print == 0 && count != 1) { | |
254 | print = 1; | |
255 | goto again; | |
256 | } | |
257 | } | |
258 | count = 0; | |
259 | if (mflag) { | |
260 | lookman(cp); | |
261 | if (uflag && print == 0 && count != 1) { | |
262 | print = 1; | |
263 | goto again; | |
264 | } | |
265 | } | |
266 | if (print) | |
267 | printf("\n"); | |
268 | } | |
269 | ||
270 | looksrc(cp) | |
271 | char *cp; | |
272 | { | |
273 | if (Sflag == 0) { | |
274 | find(srcdirs, cp); | |
275 | } else | |
276 | findv(Sflag, Scnt, cp); | |
277 | } | |
278 | ||
279 | lookbin(cp) | |
280 | char *cp; | |
281 | { | |
282 | if (Bflag == 0) | |
283 | find(bindirs, cp); | |
284 | else | |
285 | findv(Bflag, Bcnt, cp); | |
286 | } | |
287 | ||
288 | lookman(cp) | |
289 | char *cp; | |
290 | { | |
291 | if (Mflag == 0) { | |
292 | find(mandirs, cp); | |
293 | } else | |
294 | findv(Mflag, Mcnt, cp); | |
295 | } | |
296 | ||
297 | findv(dirv, dirc, cp) | |
298 | char **dirv; | |
299 | int dirc; | |
300 | char *cp; | |
301 | { | |
302 | ||
303 | while (dirc > 0) | |
304 | findin(*dirv++, cp), dirc--; | |
305 | } | |
306 | ||
307 | find(dirs, cp) | |
308 | char **dirs; | |
309 | char *cp; | |
310 | { | |
311 | ||
312 | while (*dirs) | |
313 | findin(*dirs++, cp); | |
314 | } | |
315 | ||
316 | findin(dir, cp) | |
317 | char *dir, *cp; | |
318 | { | |
319 | DIR *dirp; | |
320 | struct direct *dp; | |
321 | ||
322 | dirp = opendir(dir); | |
323 | if (dirp == NULL) | |
324 | return; | |
325 | while ((dp = readdir(dirp)) != NULL) { | |
326 | if (itsit(cp, dp->d_name)) { | |
327 | count++; | |
328 | if (print) | |
329 | printf(" %s/%s", dir, dp->d_name); | |
330 | } | |
331 | } | |
332 | closedir(dirp); | |
333 | } | |
334 | ||
335 | itsit(cp, dp) | |
336 | register char *cp, *dp; | |
337 | { | |
338 | register int i = strlen(dp); | |
339 | ||
340 | if (dp[0] == 's' && dp[1] == '.' && itsit(cp, dp+2)) | |
341 | return (1); | |
342 | while (*cp && *dp && *cp == *dp) | |
343 | cp++, dp++, i--; | |
344 | if (*cp == 0 && *dp == 0) | |
345 | return (1); | |
346 | while (isdigit(*dp)) | |
347 | dp++; | |
348 | if (*cp == 0 && *dp++ == '.') { | |
349 | --i; | |
350 | while (i > 0 && *dp) | |
351 | if (--i, *dp++ == '.') | |
352 | return (*dp++ == 'C' && *dp++ == 0); | |
353 | return (1); | |
354 | } | |
355 | return (0); | |
356 | } |