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
7 # This file is part of MSYS
8 # http://www.mingw.org/msys.shtml
11 # $Id: which,v 1.4 2009/03/14 14:13:32 keithmarshall Exp $
13 CMD=`IFS='\\/:'; set CMD $0; eval echo \$\{$#\}`
16 echo >&2 "$CMD: syntax error: missing argument"
17 echo >&2 "Usage: $CMD [ -a | --all ] cmd ..."
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.
29 if test x"$PROG" = x-a || test x"$PROG" = x--all
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"`
41 # preserve `.exe' extension
42 WHICH="$LIST"`test -f "$LIST.exe" && echo '.exe'`
43 if test "$LIST" != "$WHICH"
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`
50 # `foo' matches first, followed by `foo.exe'
51 test -z "$break" && echo "$LIST" || WHICH="$LIST"
53 # reset IFS=<newline>, to get any further PROG names
60 test x"$WHICH" = x && echo >&2 "$CMD: $PROG: "${ERROR="unknown command"}
63 test ${ERROR+set} && exit 1
66 # $RCSfile: which,v $: end of file