summaryrefslogtreecommitdiff
path: root/which
diff options
context:
space:
mode:
Diffstat (limited to 'which')
-rw-r--r--which54
1 files changed, 54 insertions, 0 deletions
diff --git a/which b/which
new file mode 100644
index 0000000..3898be0
--- /dev/null
+++ b/which
@@ -0,0 +1,54 @@
+#! /bin/sh
+set -ef
+
+ALLMATCHES=0
+
+while getopts a whichopts
+do
+ case "$whichopts" in
+ a) ALLMATCHES=1 ;;
+ ?) printf "Usage: %s [-a] args\n" $0 ; exit 2 ;;
+ esac
+done
+shift $(($OPTIND - 1))
+
+if [ "$#" -eq 0 ]; then
+ ALLRET=1
+else
+ ALLRET=0
+fi
+case $PATH in
+ *::) : "not *DIR:" ;;
+ *:) PATH="$PATH:" ;;
+esac
+for PROGRAM in "$@"; do
+ RET=1
+ IFS_SAVE="$IFS"
+ IFS=:
+ case $PROGRAM in
+ */*)
+ if [ -f "$PROGRAM" ] && [ -x "$PROGRAM" ]; then
+ printf '%s\n' "$PROGRAM"
+ RET=0
+ fi
+ ;;
+ *)
+ for ELEMENT in $PATH; do
+ if [ -z "$ELEMENT" ]; then
+ ELEMENT=.
+ fi
+ if [ -f "$ELEMENT/$PROGRAM" ] && [ -x "$ELEMENT/$PROGRAM" ]; then
+ printf '%s\n' "$ELEMENT/$PROGRAM"
+ RET=0
+ [ "$ALLMATCHES" -eq 1 ] || break
+ fi
+ done
+ ;;
+ esac
+ IFS="$IFS_SAVE"
+ if [ "$RET" -ne 0 ]; then
+ ALLRET=1
+ fi
+done
+
+exit "$ALLRET"