——#!/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::Spec;
my
$Program
= basename($0);
my
(
$VERSION
) =
'1.5'
;
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
@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:
}
# 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.
}
# 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
(!$?) {
"$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`
"Alias $alias\n"
;
$found
= 1;
next
COMMAND
unless
$opt
{
'a'
};
}
}
}
next
COMMAND
if
$found
;
foreach
my
$dir
(
@PATH
) {
my
$path
= File::Spec->catfile(
$dir
,
$command
);
if
(-d
$path
) {
next
;
}
if
($^O eq
'MacOS'
) {
if
(-e
$path
) {
"$path\n"
;
$found
= 1;
next
COMMAND
unless
$opt
{
'a'
};
}
}
else
{
if
(-x
$path
) {
"$path\n"
;
$found
= 1;
next
COMMAND
unless
$opt
{
'a'
};
}
}
foreach
my
$ext
(
@PATHEXT
) {
my
$pathext
=
$path
.
$ext
;
if
(-d
$pathext
) {
next
;
}
if
(-x
$pathext
) {
"$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