Do this by inlining (a copy of) find_a_file into find_a_program.

This separation continues what I started in way back in
5fee8a0a9223d030c66d53c104fb0a431369248f --- there should be
executable-specific logic cluttering up find_a_file either, but instead
there is a clean separation.

This commit is pure duplication (for easy reading) but the rest of the
series will remove a bunch of dead code / extraneous parameters, until
there is hardly any duplication.

gcc/ChangeLog:

        * gcc.cc (find_a_program): Inline find_a_file, instead of
          calling.

Signed-off-by: John Ericson <g...@johnericson.me>
---
 gcc/gcc.cc | 45 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index 722d42c6968..d034cd698ba 100644
--- a/gcc/gcc.cc
+++ b/gcc/gcc.cc
@@ -3084,7 +3084,50 @@ find_a_program (const char *name)
     return xstrdup (DEFAULT_DSYMUTIL);
 #endif
 
-  return find_a_file (&exec_prefixes, name, X_OK, false);
+  int mode = X_OK;
+
+  /* Find the filename in question (special case for absolute paths).  */
+
+  if (IS_ABSOLUTE_PATH (name))
+    {
+      if (access (name, mode) == 0)
+       return xstrdup (name);
+
+      return NULL;
+    }
+
+  const char *suffix = (mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : "";
+  const int name_len = strlen (name);
+  const int suffix_len = strlen (suffix);
+
+
+  /* Callback appends the file name to the directory path.  If the
+     resulting file exists in the right mode, return the full pathname
+     to the file.  */
+  return for_each_path (&exec_prefixes, false,
+                       name_len + suffix_len,
+                       [=](char *path) -> char*
+    {
+      size_t len = strlen (path);
+
+      memcpy (path + len, name, name_len);
+      len += name_len;
+
+      /* Some systems have a suffix for executable files.
+        So try appending that first.  */
+      if (suffix_len)
+       {
+         memcpy (path + len, suffix, suffix_len + 1);
+         if (access_check (path, mode) == 0)
+           return path;
+       }
+
+      path[len] = '\0';
+      if (access_check (path, mode) == 0)
+       return path;
+
+      return NULL;
+    });
 }
 
 /* Ranking of prefixes in the sort list. -B prefixes are put before
-- 
2.49.0

Reply via email to