2 * Copyright (c) 1980 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
36 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
37 * - added Native Language Support
40 #include <sys/param.h>
41 #include <sys/types.h>
51 static char *bindirs[] = {
66 "/usr/lib/emacs/*/etc",
69 "/usr/interviews/bin/LINUX",
82 "/usr/local/games/bin",
83 "/usr/local/emacs/etc",
106 static char *mandirs[] = {
112 "/usr/interviews/man/mann",
116 static char *srcdirs[] = {
118 "/usr/src/lib/libc/*",
119 "/usr/src/lib/libc/net/*",
120 "/usr/src/ucb/pascal",
121 "/usr/src/ucb/pascal/utilities",
126 static char sflag = 1, bflag = 1, mflag = 1, uflag;
127 static char **Sflag, **Bflag, **Mflag;
128 static int Scnt, Bcnt, Mcnt, count, print;
130 static void __attribute__ ((__noreturn__)) usage(FILE * out)
132 fputs(_("\nUsage:\n"), out);
134 _(" %s [options] file\n"), program_invocation_short_name);
136 fputs(_("\nOptions:\n"), out);
137 fputs(_(" -f <file> define search scope\n"
138 " -b search only binaries\n"
139 " -B <dirs> define binaries lookup path\n"
140 " -m search only manual paths\n"
141 " -M <dirs> define man lookup path\n"
142 " -s search only sources path\n"
143 " -S <dirs> define sources lookup path\n"
144 " -u search from unusual enties\n"
145 " -V output version information and exit\n"
146 " -h display this help and exit\n\n"), out);
148 fputs(_("See how to use file and dirs arguments from whereis(1) manual.\n"), out);
149 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
153 itsit(char *cp, char *dp)
157 if (dp[0] == 's' && dp[1] == '.' && itsit(cp, dp + 2))
159 if (!strcmp(dp + i - 2, ".Z"))
161 else if (!strcmp(dp + i - 3, ".gz"))
163 else if (!strcmp(dp + i - 4, ".bz2"))
165 while (*cp && *dp && *cp == *dp)
167 if (*cp == 0 && *dp == 0)
171 if (*cp == 0 && *dp++ == '.') {
174 if (--i, *dp++ == '.')
175 return (*dp++ == 'C' && *dp++ == 0);
182 findin(char *dir, char *cp)
191 dd = strchr(dir, '*');
196 while ((dp = readdir(dirp)) != NULL) {
197 if (itsit(cp, dp->d_name)) {
200 printf(" %s/%s", dir, dp->d_name);
208 if (l < sizeof(dirbuf)) {
209 /* refuse excessively long names */
211 d = strchr(dirbuf, '*');
213 dirp = opendir(dirbuf);
216 while ((dp = readdir(dirp)) != NULL) {
217 if (!strcmp(dp->d_name, ".") ||
218 !strcmp(dp->d_name, ".."))
220 if (strlen(dp->d_name) + l > sizeof(dirbuf))
222 sprintf(d, "%s", dp->d_name);
223 if (stat(dirbuf, &statbuf))
225 if (!S_ISDIR(statbuf.st_mode))
237 findv(char **dirv, int dirc, char *cp)
240 findin(*dirv++, cp), dirc--;
247 findv(srcdirs, ARRAY_SIZE(srcdirs)-1, cp);
249 findv(Sflag, Scnt, cp);
256 findv(bindirs, ARRAY_SIZE(bindirs)-1, cp);
258 findv(Bflag, Bcnt, cp);
265 findv(mandirs, ARRAY_SIZE(mandirs)-1, cp);
267 findv(Mflag, Mcnt, cp);
271 getlist(int *argcp, char ***argvp, char ***flagp, int *cntp)
276 for ((*argcp)--; *argcp > 0 && (*argvp)[0][0] != '-'; (*argcp)--)
277 (*cntp)++, (*argvp)++;
285 if (sflag && bflag && mflag)
286 sflag = bflag = mflag = 0;
290 print_again(char *cp)
296 if (uflag && print == 0 && count != 1) {
304 if (uflag && print == 0 && count != 1) {
312 if (uflag && print == 0 && count != 1) {
325 for (dp = cp; *dp; dp++)
327 for (; dp > cp; dp--) {
333 for (dp = cp; *dp; dp++)
342 while (print_again(cp))
343 /* all in print_again() */ ;
351 * look for source, documentation and binaries
354 main(int argc, char **argv)
356 setlocale(LC_ALL, "");
357 bindtextdomain(PACKAGE, LOCALEDIR);
365 if (argv[0][0] == '-') {
366 register char *cp = argv[0] + 1;
367 while (*cp) switch (*cp++) {
373 getlist(&argc, &argv, &Sflag, &Scnt);
377 getlist(&argc, &argv, &Bflag, &Bcnt);
381 getlist(&argc, &argv, &Mflag, &Mcnt);
403 printf(_("%s from %s\n"),
404 program_invocation_short_name,