Previously, if the command given to Valgrind didn't exist, the 'stat -c'
suid/sgid test failed, and the startup script aborted with an ugly message.
I've changed it so that it first checks that the program exists in the user's
path (as determined by 'which'), and aborts if not.

This was quite fiddly to get right;  yell if I've broken anything.


git-svn-id: svn://svn.valgrind.org/valgrind/trunk@1967 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/coregrind/valgrind.in b/coregrind/valgrind.in
index 4b3eb37..a763f4b 100755
--- a/coregrind/valgrind.in
+++ b/coregrind/valgrind.in
@@ -109,28 +109,40 @@
    export LD_ASSUME_KERNEL
 fi
 
-which_prog="`which $1`"
+# Check that the program looks ok
+is_prog=0
 
-# Ensure the program isn't statically linked.
 if [ $# != 0 ] ; then
-   case `file "$which_prog"` in
-   *"statically linked"*)
-     echo "\`$which_prog' is statically linked"
-     echo "Valgrind only works on dynamically linked executables;  your"
-     echo "program must rely on at least one shared object for Valgrind"
-     echo "to work with it.  Read FAQ #5 for more information."
-     exit 1 ;;
-   esac
-fi
 
-# Ensure that there is no suid or sgid flag
-if [ `stat -c %a "$which_prog"` -gt 2000 ] ; then
-  echo "\`$which_prog' is suid/sgid."
-  echo "Valgrind can't handle these executables, as it"
-  echo "requires the LD_PRELOAD feature in order to work."
-  echo ""
-  echo "Remove those flags and try again."
-  exit 1
+   # Ensure the program exists.  Ignore any error messages from 'which'.
+   which_prog=`which $1 2> /dev/null`
+   if [ z$which_prog = z ] ; then
+       echo "'$1' not found in \$PATH, aborting."
+       exit
+   fi
+
+   # Ensure the program isn't statically linked.
+   if [ $# != 0 ] ; then
+      case `file "$which_prog"` in
+      *"statically linked"*)
+        echo "\`$which_prog' is statically linked"
+        echo "Valgrind only works on dynamically linked executables;  your"
+        echo "program must rely on at least one shared object for Valgrind"
+        echo "to work with it.  Read FAQ #5 for more information."
+        exit 1 ;;
+      esac
+   fi
+
+   # Ensure that there is no suid or sgid flag
+   if [ `stat -c %a "$which_prog"` -gt 2000 ] ; then
+     echo "\`$which_prog' is suid/sgid."
+     echo "Valgrind can't handle these executables, as it"
+     echo "requires the LD_PRELOAD feature in order to work."
+     echo ""
+     echo "Remove those flags and try again."
+     exit 1
+   fi
+   is_prog=1
 fi
 
 # A bit subtle.  The LD_PRELOAD added entry must be absolute
@@ -149,13 +161,14 @@
 #LD_DEBUG=symbols
 #export LD_DEBUG
     
-# If no command given, act like -h was given so vg_main.c prints out
-# the usage string.  And pass to 'exec' tha name of any program -- it doesn't
-# matter which -- because it won't be run anyway (we use 'true').
-if [ $# != 0 ] ; then
+# Actually run the program, under Valgrind's control
+if [ $is_prog = 1 ] ; then
    exec "$@"
 else
-   VG_ARGS="$VG_ARGS -h" 
+   # If no command given, act like -h was given so vg_main.c prints out the
+   # usage string.  And pass to 'exec' the name of any program -- it doesn't
+   # matter which -- because it won't be run anyway (we use 'true').
+   VG_ARGS="$VG_ARGS -h"
    exec true
 fi