Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 1 | # |
| 2 | # which - emulation of `which' as it appears in FreeBSD |
| 3 | # |
| 4 | # usage: which [-as] command [command...] |
| 5 | # |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 6 | # |
| 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 Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 24 | |
| 25 | which() |
| 26 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 27 | local aflag sflag ES a opt |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 28 | |
| 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 | } |