blob: f0db290c836d764caf577658f98c470ab9cb6a73 [file] [log] [blame]
Jari Aaltob72432f1999-02-19 17:11:39 +00001#
2# which - emulation of `which' as it appears in FreeBSD
3#
4# usage: which [-as] command [command...]
5#
Chet Rameyac50fba2014-02-26 09:36:43 -05006#
7# Chet Ramey <chet.ramey@case.edu>
8#
9# Copyright 1999 Chester Ramey
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2, or (at your option)
14# any later version.
15#
16# TThis program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software Foundation,
23# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Jari Aaltob72432f1999-02-19 17:11:39 +000024
25which()
26{
Jari Aaltobb706242000-03-17 21:46:59 +000027 local aflag sflag ES a opt
Jari Aaltob72432f1999-02-19 17:11:39 +000028
29 OPTIND=1
30 while builtin getopts as opt ; do
31 case "$opt" in
32 a) aflag=-a ;;
33 s) sflag=1 ;;
34 ?) echo "which: usage: which [-as] command [command ...]" >&2
35 exit 2 ;;
36 esac
37 done
38
39 (( $OPTIND > 1 )) && shift $(( $OPTIND - 1 ))
40
41 # without command arguments, exit with status 1
42 ES=1
43
44 # exit status is 0 if all commands are found, 1 if any are not found
45 for command; do
46 # if $command is a function, make sure we add -a so type
47 # will look in $PATH after finding the function
48 a=$aflag
49 case "$(builtin type -t $command)" in
50 "function") a=-a;;
51 esac
52
53 if [ -n "$sflag" ]; then
54 builtin type -p $a $command >/dev/null 2>&1
55 else
56 builtin type -p $a $command
57 fi
58 ES=$?
59 done
60
61 return $ES
62}