path: Improve $PATH search directory case
Previously:
1. last_error wouldn't be updated with errors from is_dir;
2. We'd always issue a stat(), even for binaries without execute;
3. We used stat() instead of access(), which is cheaper.
This change avoids all of those, by only checking inside X_OK-positive
case whether access() works on the path with an extra slash appended.
Thanks to Lennart for the suggestion.
(cherry picked from commit
33e1a5d8d3f792e1d98377fe439e123231032ec7)
(cherry picked from commit
a4236a27644705e58836f5d547d5aef50d568c11)
(cherry picked from commit
6a30d4e98032575d385a09d15782be74cbef6dfe)