3 # Copyright © 1995, 1996 Jörg Wunsch
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
17 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 # IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 # whereis -- search for binaries, man pages and source directories.
29 # Rewritten from scratch for FreeBSD after the 4.3BSD manual page.
31 # $FreeBSD: src/usr.bin/whereis/whereis.pl,v 1.8 1999/08/28 01:07:37 peter Exp $
36 print STDERR "usage: $0 [-bms] [-u] [-BMS dir... -f] name ...\n";
44 while ($ARGV[$i] =~ /^-/) {
46 for ($j = 1; $j < length($ARGV[$i]); $j++) {
47 local($_) = substr($ARGV[$i], $j, 1);
49 $opt_b++, next opt if /b/;
50 $opt_m++, next opt if /m/;
51 $opt_s++, next opt if /s/;
52 $opt_u++, next opt if /u/;
53 &usage unless /[BMS]/;
55 # directory list processing
56 $what = $_; @list = ();
57 push(@list, substr($ARGV[$i], $j+1)) if $j+1 < length($ARGV[$i]);
59 while ($i <= $#ARGV && $ARGV[$i] !~ /^-/) {
60 push(@list, $ARGV[$i++]);
62 if ($what eq "B") {@binaries = @list;}
63 elsif ($what eq "M") {@manuals = @list;}
64 elsif ($what eq "S") {@sources = @list;}
66 $i++, last arg if $ARGV[$i] =~ /^-f$/;
71 &usage if $i > $#ARGV;
74 push(@names, $ARGV[$i++]);
83 foreach(split(/:/, $list)) {
92 # default to all if no type requested
93 if ($opt_b + $opt_m + $opt_s == 0) {$opt_b = $opt_m = $opt_s = 1;}
95 if (!defined(@binaries)) {
97 # first, use default path, then append /usr/libexec and the user's path
99 local($cs_path) = `/sbin/sysctl -n user.cs_path`;
104 @list = &decolonify($cs_path);
105 push(@list, "/usr/libexec");
106 push(@list, &decolonify($ENV{'PATH'}));
108 # resolve ~, remove duplicates
110 s/^~/$ENV{'HOME'}/ if /^~/;
111 push(@binaries, $_) if !$path{$_};
116 if (!defined(@manuals)) {
118 # first, use default manpath, then append user's $MANPATH
120 local($usermanpath) = $ENV{'MANPATH'};
121 delete $ENV{'MANPATH'};
122 local($manpath) = `/usr/bin/manpath`;
123 local(@list, %path, $i);
127 @list = &decolonify($manpath);
128 push(@list, &decolonify($usermanpath));
132 push(@manuals, $_) if !$path{$_};
137 if (!defined(@sources)) {
139 # default command sources
143 @sources = ("/usr/src/bin", "/usr/src/usr.bin", "/usr/src/sbin",
144 "/usr/src/usr.sbin", "/usr/src/libexec",
145 "/usr/src/gnu/bin", "/usr/src/gnu/usr.bin",
146 "/usr/src/gnu/sbin", "/usr/src/gnu/usr.sbin",
147 "/usr/src/gnu/libexec", "/usr/src/contrib");
150 # if /usr/ports exists, look in all its subdirs, too
152 if (-d "/usr/ports" && opendir(PORTS, "/usr/ports")) {
153 while ($_ = readdir(PORTS)) {
155 next if /^distfiles$/; # magic
156 next if ! -d "/usr/ports/$_";
157 push(@sources, "/usr/ports/$_");
164 # construct a new MANPATH
167 if ($manpath) { $manpath .= ":$_"; }
168 else { $manpath = $_; }
175 foreach $name (@names) {
176 $name =~ s|^.*/||; # strip leading path name component
177 $name =~ s/,v$//; $name =~ s/^s\.//; # RCS or SCCS suffix/prefix
178 $name =~ s/\.(Z|z|gz)$//; # compression suffix
185 # Binaries have to match exactly, and must be regular executable
189 foreach (@binaries) {
190 $line .= " $_/$name", $unusual--, last if -f "$_/$name" && -x _;
196 # Ask the man command to do the search for us.
199 chop($result = `man -S 1:8 -M $manpath -w $name 2> /dev/null`);
202 ($cat, $junk, $src) = split(/[() \t\n]+/, $result);
203 if ($src ne '') { $line .= " $src"; }
204 else { $line .= " $cat"; }
210 # Sources match if a subdir with the exact name is found.
215 $line .= " $_/$name", $unusual--, $found++ if -d "$_/$name";
218 # If not yet found, ask locate(1) to do the search for us.
219 # This will find sources for things like lpr, but take longer.
220 # Do only match locate output that starts with one of our
221 # source directories, and at least one further level of
224 if (!$found && open(LOCATE, "locate */$name 2>/dev/null |")) {
226 while (chop($loc = <LOCATE>)) {
228 $line .= " $loc", $unusual--, last locate_item
229 if $loc =~ m|^$_/[^/]+/|;
237 print "$name:\n" if $unusual;
239 print "$name:$line\n";