From: John Ericson <g...@johnericson.me>

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

The cost of this is that there is now some code duplication, but in
subsequent commits in this patch series, the code paths will grow
further apart, reducing duplication.

gcc/ChangeLog:

        * gcc.cc (file_at_path): Remove program suffix logic (it is
        moving elsewhere). This function no longer cares about programs
        in particular, matching its name.
        (program_at_path): New function that does care about programs in
        particular. It has the preexisting suffixing logic (moved here)
        and the new prefixing logic.

Signed-off-by: John Ericson <g...@johnericson.me>
---
 gcc/gcc.cc | 67 +++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 54 insertions(+), 13 deletions(-)

diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index bfa588ee5f0..3e1e4bbd866 100644
--- a/gcc/gcc.cc
+++ b/gcc/gcc.cc
@@ -3043,15 +3043,6 @@ file_at_path (char *path, void *data)
   memcpy (path + len, info->name, info->name_len);
   len += info->name_len;
 
-  /* Some systems have a suffix for executable files.
-     So try appending that first.  */
-  if (info->suffix_len)
-    {
-      memcpy (path + len, info->suffix, info->suffix_len + 1);
-      if (access_check (path, info->mode) == 0)
-       return path;
-    }
-
   path[len] = '\0';
   if (access_check (path, info->mode) == 0)
     return path;
@@ -3081,16 +3072,45 @@ find_a_file (const struct path_prefix *pprefix, const 
char *name, int mode,
     }
 
   info.name = name;
-  info.suffix = (mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : "";
+  info.suffix = "";
   info.name_len = strlen (info.name);
-  info.suffix_len = strlen (info.suffix);
+  info.suffix_len = 0;
   info.mode = mode;
 
   return (char*) for_each_path (pprefix, do_multi,
-                               info.name_len + info.suffix_len,
+                               info.name_len,
                                file_at_path, &info);
 }
 
+/* Callback for find_a_program.  Appends the file name to the directory
+   path. Like file_at_path but tries machine prefix and exe suffix too. */
+
+static void *
+program_at_path (char *path, void *data)
+{
+  /* try first with machine-prefixed name */
+  struct file_at_path_info *info = (struct file_at_path_info *) data;
+  size_t len = strlen (path);
+
+  memcpy (path + len, info->name, info->name_len);
+  len += info->name_len;
+
+  /* Some systems have a suffix for executable files.
+     So try appending that first.  */
+  if (info->suffix_len)
+    {
+      memcpy (path + len, info->suffix, info->suffix_len + 1);
+      if (access_check (path, info->mode) == 0)
+       return path;
+    }
+
+  path[len] = '\0';
+  if (access_check (path, info->mode) == 0)
+    return path;
+
+  return NULL;
+}
+
 /* Specialization of find_a_file for programs that also takes into account
    configure-specified default programs. */
 
@@ -3114,7 +3134,28 @@ find_a_program (const char *name)
     return xstrdup (DEFAULT_DSYMUTIL);
 #endif
 
-  return find_a_file (&exec_prefixes, name, X_OK, false);
+  /* Find the filename in question (special case for absolute paths).  */
+
+  if (IS_ABSOLUTE_PATH (name))
+    {
+      if (access (name, X_OK) == 0)
+       return xstrdup (name);
+
+      return NULL;
+    }
+
+  struct file_at_path_info info;
+
+  info.name = name;
+  info.suffix = HOST_EXECUTABLE_SUFFIX;
+  info.name_len = strlen (info.name);
+  info.suffix_len = strlen (info.suffix);
+  info.mode = X_OK;
+
+  return (char*) for_each_path (
+    &exec_prefixes, false,
+    info.name_len + info.suffix_len,
+    program_at_path, &info);
 }
 
 /* Ranking of prefixes in the sort list. -B prefixes are put before
-- 
2.49.0

Reply via email to