1#
2# which - emulation of `which' as it appears in FreeBSD
3#
4# usage: which [-as] command [command...]
5#
6
7which()
8{
9	local aflag sflag ES a opt
10
11	OPTIND=1
12	while builtin getopts as opt ; do
13		case "$opt" in
14		a)	aflag=-a ;;
15		s)	sflag=1 ;;
16		?)	echo "which: usage: which [-as] command [command ...]" >&2
17			exit 2 ;;
18		esac
19	done
20
21	(( $OPTIND > 1 )) && shift $(( $OPTIND - 1 ))
22
23	# without command arguments, exit with status 1
24	ES=1
25
26	# exit status is 0 if all commands are found, 1 if any are not found
27	for command; do
28		# if $command is a function, make sure we add -a so type
29		# will look in $PATH after finding the function
30		a=$aflag
31		case "$(builtin type -t $command)" in
32		"function")	a=-a;;
33		esac
34
35		if [ -n "$sflag" ]; then
36			builtin type -p $a $command >/dev/null 2>&1
37		else
38			builtin type -p $a $command
39		fi
40		ES=$?
41	done
42
43	return $ES
44}
45