[1046] | 1 | #!/bin/sh
|
---|
| 2 | # Original copyright (C) 2002, Earnie Boyd
|
---|
| 3 | # mailto:earnie@users.sf.net
|
---|
| 4 | # This implementation copyright (C) 2006, 2008, Keith Marshall
|
---|
| 5 | # mailto:keithmarshall@users.sf.net
|
---|
| 6 | #
|
---|
| 7 | # This file is part of MSYS
|
---|
| 8 | # http://www.mingw.org/msys.shtml
|
---|
| 9 | #
|
---|
| 10 | # File: which
|
---|
| 11 | # $Id: which,v 1.4 2009/03/14 14:13:32 keithmarshall Exp $
|
---|
| 12 |
|
---|
| 13 | CMD=`IFS='\\/:'; set CMD $0; eval echo \$\{$#\}`
|
---|
| 14 | if test $# -lt 1
|
---|
| 15 | then
|
---|
| 16 | echo >&2 "$CMD: syntax error: missing argument"
|
---|
| 17 | echo >&2 "Usage: $CMD [ -a | --all ] cmd ..."
|
---|
| 18 | exit 1
|
---|
| 19 | fi
|
---|
| 20 |
|
---|
| 21 | # To accomodate Woe32's typically asinine $PATH, which frequently
|
---|
| 22 | # includes directory names with embedded spaces, we need to set up
|
---|
| 23 | # $IFS to consider only a newline as a field separator.
|
---|
| 24 | IFS=$'\n'
|
---|
| 25 |
|
---|
| 26 | break=break
|
---|
| 27 | for PROG
|
---|
| 28 | do
|
---|
| 29 | if test x"$PROG" = x-a || test x"$PROG" = x--all
|
---|
| 30 | then
|
---|
| 31 | break=""
|
---|
| 32 | else
|
---|
| 33 | WHICH=""
|
---|
| 34 | # need `type -ap -- "$PROG" || type -p -- "$PROG"'
|
---|
| 35 | # because `type -ap foo' reports nothing, if both `foo' and `foo.exe'
|
---|
| 36 | # are present, and are distinct.
|
---|
| 37 | for LIST in `type -ap -- "$PROG" || type -p -- "$PROG"`
|
---|
| 38 | do
|
---|
| 39 | if test -f "$LIST"
|
---|
| 40 | then
|
---|
| 41 | # preserve `.exe' extension
|
---|
| 42 | WHICH="$LIST"`test -f "$LIST.exe" && echo '.exe'`
|
---|
| 43 | if test "$LIST" != "$WHICH"
|
---|
| 44 | then
|
---|
| 45 | # detect distinct `foo' and `foo.exe'
|
---|
| 46 | # (this needs IFS=<space>, to get the INODE numbers)
|
---|
| 47 | IFS=" " INODE1=`ls -id "$LIST"` INODE2=`ls -id "$WHICH"`
|
---|
| 48 | if test `set ref $INODE1; echo $2` != `set ref $INODE2; echo $2`
|
---|
| 49 | then
|
---|
| 50 | # `foo' matches first, followed by `foo.exe'
|
---|
| 51 | test -z "$break" && echo "$LIST" || WHICH="$LIST"
|
---|
| 52 | fi
|
---|
| 53 | # reset IFS=<newline>, to get any further PROG names
|
---|
| 54 | IFS=$'\n'
|
---|
| 55 | fi
|
---|
| 56 | echo "$WHICH"
|
---|
| 57 | $break
|
---|
| 58 | fi
|
---|
| 59 | done
|
---|
| 60 | test x"$WHICH" = x && echo >&2 "$CMD: $PROG: "${ERROR="unknown command"}
|
---|
| 61 | fi
|
---|
| 62 | done
|
---|
| 63 | test ${ERROR+set} && exit 1
|
---|
| 64 | exit 0
|
---|
| 65 |
|
---|
| 66 | # $RCSfile: which,v $: end of file
|
---|