PATCH: rewrite utils.exp:which

Ben Elliston <[email protected]>
Newsgroups gmane.comp.sysutils.dejagnu.general
Message-ID <[email protected]>
There was a '???' comment in utils.exp:which, so I rewrote it to
properly mimic the behaviour of the which(1) shell utility.  A nice
side effect of the new version is that, because the new proc
normalises the filename when it's found, the GCC testsuite reports:

  /home/bje/build/gcc/gcc/xg++  version 6.0.0 20160310 (experimental) (GCC)

instead of:

  /home/bje/build/gcc/gcc/testsuite/g++/../../xg++  version 6.0.0 20160310 (experimental) (GCC)

Here's the patch. Tested with `make check' and a GCC testsuite run.
Any comments?

Cheers, Ben

2016-03-22  Ben Elliston  <[email protected]>

	    * lib/utils.exp (which): Reimplement to more closely mimic the
	    behaviour of the UNIX which utility.
	    * testsuite/runtest.all/utils.test: Test proc which.

diff --git a/lib/utils.exp b/lib/utils.exp
index e2a5040..c61785c 100644
--- a/lib/utils.exp
+++ b/lib/utils.exp
@@ -130,24 +130,34 @@ proc which { file } {
     # strip off any extraneous arguments (like flags to the compiler)
     set file [lindex $file 0]
 
-    # if it exists then the path must be OK
-    # ??? What if $file has no path and "." isn't in $PATH?
-    if {[file exists $file]} {
-       return $file
+    # if the filename has a path component, then the file must exist
+    if {[llength [file split $file]] > 1} {
+       verbose "Checking $file" 2
+       if {[file exists $file] && \
+               [file executable $file] && [file type $file] == "file"} {
+           verbose "file $file is executable and not a link" 2
+           return [file normalize $file]
+       } else {
+           return 0
+       }
     }
+
+    # Otherwise the file must exist in the PATH
     if {[info exists env(PATH)]} {
        set path [split $env(PATH) ":"]
     } else {
        return 0
     }
 
-    foreach i $path {
-       verbose "Checking against $i" 3
-       if {[file exists [file join $i $file]]} {
-           if {[file executable [file join $i $file]]} {
-               return [file join $i $file]
+    foreach dir $path {
+       verbose "Checking $dir for $file" 3
+       set filename [file join $dir $file]
+       if {[file exists $filename]} {
+           if {[file executable $filename] && [file type $filename] == "file"} {
+               verbose "file $filename is executable and not a link" 2
+               return $filename
            } else {
-               warning "[file join $i $file] exists but is not an executable"
+               warning "file $filename exists but is not executable or is a link"
            }
        }
     }
diff --git a/testsuite/runtest.all/utils.test b/testsuite/runtest.all/utils.test
index c3e2f3c..de8c809 100644
--- a/testsuite/runtest.all/utils.test
+++ b/testsuite/runtest.all/utils.test
@@ -101,7 +101,30 @@ if [info exists env(TESTRUN)] {
     untested "unsetenv, unset an environment variable"
 }
 
-# which file
+# Test 'which' using a relative path.
+#
+if {[which ./config.status] != 0} {
+  pass "which, relative path to config.status"
+} else {
+  fail "which, relative path to config.status"
+}
+
+# Test 'which' using an absolute path.
+#
+if {[which [file join $objdir config.status]] != 0} {
+  pass "which, absolute path to config.status"
+} else {
+  fail "which, absolute path to config.status"
+}
+
+# Test 'which make'.
+#
+if {[which make] != 0} {
+  pass "which, make"
+} else {
+  pass "which, make"
+}
+
 # grep args
 # diff file_1 file_2
 # runtest_file_p

_______________________________________________
DejaGnu mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/dejagnu
signature.asc (application/pgp-signature, 811 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIVAwUBVvBQa3Y2KIi1WYjUAQgPaA/+KIFxTzyyMLe74hVu9VlEHKK6eBtkPkli
r3Gy3ct+vhSJ9QVSubSbkjf6rjH6VUmS27KFh6ONDUkyCgBYsylg9qaaXOBHcBco
Te+Uup9EuXm7H2KKrPN7LC+jdutWY+/tIhvnmyOKGtpXpBdcFD/afHN30H66EWdD
xYGErQtD27m1jovtul5mJaMrTR/SIIataoxT7WtHAJPwpXdgAIjCeMvXVP6feAAp
4iu4cs74wa6rpcPheki7myJfejDytaI1akZigUOOybOowO05g0+0rJiB9GipnqXr
s7hpRbp0yxmzHPR+lu1vFbhJihNdtf8srjRgERmZSIFjoGL9V4O6QDsHY7OnXyKw
K/gA74obLpADYNUn6rUCcgXrH4E2XdyumkJgD08uRaTC9xJLTtSU+gLIKFu1pwYF
Rvu22V7CjFEsiY73YdJPITQBLD9nPqEi1Qdk8b9lZC/9PGa9R4+Ny+nbg/ssDfkE
0QuS5W2n5J51piCv55BJaqhoAGtOr1cr+VSsnaRb0axQOfdOMuoUS/P6hJSU/IDY
Un/Ebz94fWBmQu2Mu3nJxrYbF+XZO6dun8ldmWBO+hPzLcypvxCrkhsqfu/GXj9W
VCB5WzUq0TC4ftpKUchrcgy1CNDXHyRa/bM3ju/78SZBm08Lhf04jghPK0DML7Dj
lP2WowssD/E=
=lMAo
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.