aboutsummaryrefslogtreecommitdiffstats
path: root/examples/functions/which
diff options
context:
space:
mode:
authorJari Aalto <jari.aalto@cante.net>1999-02-19 17:11:39 +0000
committerJari Aalto <jari.aalto@cante.net>2009-09-12 16:46:52 +0000
commitb72432fdcc59300c6fe7c9d6c8a31ad3447933f5 (patch)
treeb9899162338c2ff3fd83a8aef8831cb119e85cd7 /examples/functions/which
parentbc4cd23ce958feda898c618215f94d8a4e8f4ffa (diff)
downloadandroid_external_bash-b72432fdcc59300c6fe7c9d6c8a31ad3447933f5.tar.gz
android_external_bash-b72432fdcc59300c6fe7c9d6c8a31ad3447933f5.tar.bz2
android_external_bash-b72432fdcc59300c6fe7c9d6c8a31ad3447933f5.zip
Imported from ../bash-2.03.tar.gz.
Diffstat (limited to 'examples/functions/which')
-rw-r--r--examples/functions/which44
1 files changed, 44 insertions, 0 deletions
diff --git a/examples/functions/which b/examples/functions/which
new file mode 100644
index 0000000..54ace77
--- /dev/null
+++ b/examples/functions/which
@@ -0,0 +1,44 @@
+#
+# which - emulation of `which' as it appears in FreeBSD
+#
+# usage: which [-as] command [command...]
+#
+
+which()
+{
+ local aflag sflag ES a
+
+ OPTIND=1
+ while builtin getopts as opt ; do
+ case "$opt" in
+ a) aflag=-a ;;
+ s) sflag=1 ;;
+ ?) echo "which: usage: which [-as] command [command ...]" >&2
+ exit 2 ;;
+ esac
+ done
+
+ (( $OPTIND > 1 )) && shift $(( $OPTIND - 1 ))
+
+ # without command arguments, exit with status 1
+ ES=1
+
+ # exit status is 0 if all commands are found, 1 if any are not found
+ for command; do
+ # if $command is a function, make sure we add -a so type
+ # will look in $PATH after finding the function
+ a=$aflag
+ case "$(builtin type -t $command)" in
+ "function") a=-a;;
+ esac
+
+ if [ -n "$sflag" ]; then
+ builtin type -p $a $command >/dev/null 2>&1
+ else
+ builtin type -p $a $command
+ fi
+ ES=$?
+ done
+
+ return $ES
+}