/
which
executable file
·220 lines (170 loc) · 4.81 KB
/
which
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/perl
=begin metadata
Name: which
Description: report full paths of commands
Author: Abigail, perlpowertools@abigail.be
License: perl
=end metadata
=cut
use strict;
use File::Basename qw(basename);
use Getopt::Std qw(getopts);
use constant EX_SUCCESS => 0;
use constant EX_PARTIAL => 1;
use constant EX_FAILURE => 2;
my $Program = basename($0);
my ($VERSION) = '1.4';
sub usage {
warn "$Program version $VERSION\n";
warn "usage: $Program [-a] filename ...\n";
exit EX_FAILURE;
}
my %opt;
getopts('a', \%opt) or usage();
@ARGV or usage();
my @PATH = ();
my $PATHVAR = 'PATH';
my $path_sep = ':';
my $file_sep = '/';
my @PATHEXT = ();
my $Is_DOSish = ($^O eq 'MSWin32') ||
($^O eq 'dos') ||
($^O eq 'os2') ;
if ($Is_DOSish) {
$path_sep = ';';
}
if ($^O eq 'MacOS') {
$path_sep = '\,';
$PATHVAR = 'Commands';
# since $ENV{Commands} contains a trailing ':'
# we don't need it here:
$file_sep = '';
}
# Split the path.
if (defined($ENV{$PATHVAR})) {
@PATH = split /$path_sep/ => $ENV{$PATHVAR};
}
# Add OS dependent elements.
if ($^O eq 'VMS') {
my $i = 0;
my $path_element = undef;
while (defined($path_element = $ENV{"DCL\$PATH;$i"})) {
push(@PATH, $path_element);
$i++;
}
# PATH may be a search list too
$i = 0;
$path_element = undef;
while (defined($path_element = $ENV{"PATH;$i"})) {
push(@PATH, $path_element);
$i++;
}
# PATH and DCL$PATH are likely to use native dirspecs.
$file_sep = '';
}
# trailing file types (NT/VMS)
if (defined($ENV{PATHEXT})) {
@PATHEXT = split /$path_sep/ => $ENV{PATHEXT};
}
if ($^O eq 'VMS') { @PATHEXT = qw(.exe .com); }
my $rc = EX_SUCCESS;
COMMAND:
foreach my $command (@ARGV) {
my $found = 0;
if ($^O eq 'VMS') {
my $symbol = `SHOW SYMBOL $command`; # line feed returned
if (!$?) {
print "$symbol";
$found = 1;
next COMMAND unless $opt{'a'};
}
}
elsif ($^O eq 'MacOS') {
my @aliases = split /$path_sep/ => $ENV{Aliases};
foreach my $alias (@aliases) {
if (lc($alias) eq lc($command)) {
# MPW-Perl cannot resolve using `Alias $alias`
print "Alias $alias\n";
$found = 1;
next COMMAND unless $opt{'a'};
}
}
}
next COMMAND if $found;
foreach my $dir (@PATH) {
my $path = $dir . $file_sep . $command;
if (-d $path) {
next;
}
if ($^O eq 'MacOS') {
if (-e $path) {
print "$path\n";
$found = 1;
next COMMAND unless $opt{'a'};
}
}
else {
if (-x $path) {
print "$path\n";
$found = 1;
next COMMAND unless $opt{'a'};
}
}
foreach my $ext (@PATHEXT) {
my $pathext = $path . $ext;
if (-d $pathext) {
next;
}
if (-x $pathext) {
print "$pathext\n";
$found = 1;
next COMMAND unless $opt{'a'};
}
}
}
next COMMAND if $found;
warn "$Program: $command: command not found\n";
$rc = EX_PARTIAL;
}
exit $rc;
__END__
=pod
=head1 NAME
which - report full paths of commands
=head1 SYNOPSIS
which [-a] filename ...
=head1 DESCRIPTION
I<which> prints the full paths to the commands given as arguments,
depending on the I<$PATH> environment variable. Nothing is printed if
the command is not found.
=head2 OPTIONS
I<which> accepts the following options:
=over 4
=item -a
Print out all instances of command on I<$PATH> not just the first.
=item --
Stop parsing for options.
Use I<which -- --> to find the path to I<-->.
=back
=head1 ENVIRONMENT
The environment variable I<$PATH> (also I<DCL$PATH> under DCL; or
I<$Commands> under MPW) is used to find the list of directories
to check for commands. The variable I<%PATHEXT%> is examined for
command extensions if it exists.
=head1 BUGS
I<which> has no known bugs.
=head1 COMPATABILITY
Traditionally, I<which> also parses ones F<~/.cshrc> file to look for
aliases, and reporting the alias when applicable. This version of
I<which> does not do that, because there are more shells than I<csh>.
I<which> will examine aliases under MPW (Mac) and symbols under DCL
(VMS).
=head1 AUTHOR
The Perl implementation of I<which> was written by Abigail, I<perlpowertools@abigail.be>.
Portability enhancements by Peter Prymmer.
=head1 COPYRIGHT and LICENSE
This program is copyright by Abigail 1999.
This program is free and open software. You may use, modify, distribute
and sell this program (and any modified variants) in any way you wish,
provided you do not restrict others to do the same.
=cut