[v6,2/4] driver: Simplify `find_a_program` and `find_a_file`

Message ID 20260111215359.1464516-3-git@JohnEricson.me
State New
Headers
Series [v6,1/4] find_a_program: Separate from find_a_file |

Commit Message

John Ericson Jan. 11, 2026, 9:12 p.m. UTC
  From: John Ericson <John.Ericson@Obsidian.Systems>

Now that `find_a_program` and `find_a_file` have been separated, we can
make a number of simplification in both of them. Most notably, we don't
need a mode parameter, because one is always doing `R_OK`, and the other
is always doing `X_OK`.

This change also proves that some of the code I removed from
`find_a_file` in the previous commit is actually dead.

gcc/ChangeLog:

	* gcc.cc (find_a_file): Remove mode parameter, because always
	R_OK. Skip suffix logic, because suffix is always empty in that
	case.
	(read_specs): Remove mode from find_a_file call.
	(find_a_program): Suffix is unconditional, inline mode constant.
	(end_going_arg): Remove mode from find_a_file call.
	(find_file): Remove mode from find_a_file call.
	(driver::set_up_specs): Remove mode from find_a_file call.
	(include_spec_function): Remove mode from find_a_file call.

Signed-off-by: John Ericson <git@JohnEricson.me>
---
 gcc/gcc.cc | 58 +++++++++++++++++++-----------------------------------
 1 file changed, 20 insertions(+), 38 deletions(-)
  

Patch

diff --git a/gcc/gcc.cc b/gcc/gcc.cc
index 60e0baf1456..038056c0722 100644
--- a/gcc/gcc.cc
+++ b/gcc/gcc.cc
@@ -388,7 +388,7 @@  static void xputenv (const char *);
 static void putenv_from_prefixes (const struct path_prefix *, const char *,
 				  bool);
 static int access_check (const char *, int);
-static char *find_a_file (const struct path_prefix *, const char *, int, bool);
+static char *find_a_file (const struct path_prefix *, const char *, bool);
 static char *find_a_program (const char *);
 static void add_prefix (struct path_prefix *, const char *, const char *,
 			int, int, int);
@@ -2450,7 +2450,7 @@  read_specs (const char *filename, bool main_p, bool user_p)
 			     "%td characters", p1 - buffer + 1);
 
 	      p[-2] = '\0';
-	      new_filename = find_a_file (&startfile_prefixes, p1, R_OK, true);
+	      new_filename = find_a_file (&startfile_prefixes, p1, true);
 	      read_specs (new_filename ? new_filename : p1, false, user_p);
 	      continue;
 	    }
@@ -2470,7 +2470,7 @@  read_specs (const char *filename, bool main_p, bool user_p)
 			     "%td characters", p1 - buffer + 1);
 
 	      p[-2] = '\0';
-	      new_filename = find_a_file (&startfile_prefixes, p1, R_OK, true);
+	      new_filename = find_a_file (&startfile_prefixes, p1, true);
 	      if (new_filename)
 		read_specs (new_filename, false, user_p);
 	      else if (verbose_flag)
@@ -3040,47 +3040,32 @@  access_check (const char *name, int mode)
    Return 0 if not found, otherwise return its name, allocated with malloc.  */
 
 static char *
-find_a_file (const struct path_prefix *pprefix, const char *name, int mode,
+find_a_file (const struct path_prefix *pprefix, const char *name,
 	     bool do_multi)
 {
   /* Find the filename in question (special case for absolute paths).  */
 
   if (IS_ABSOLUTE_PATH (name))
     {
-      if (access (name, mode) == 0)
+      if (access (name, R_OK) == 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 (pprefix, do_multi,
-			name_len + suffix_len,
+			name_len,
 			[=](char *path) -> char*
     {
-      size_t len = strlen (path);
+      memcpy (path + strlen (path), name, name_len + 1);
 
-      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)
+      if (access_check (path, R_OK) == 0)
 	return path;
 
       return NULL;
@@ -3115,23 +3100,20 @@  find_a_program (const char *name)
     return xstrdup (DEFAULT_WINDRES);
 #endif
 
-  int mode = X_OK;
-
   /* Find the filename in question (special case for absolute paths).  */
 
   if (IS_ABSOLUTE_PATH (name))
     {
-      if (access (name, mode) == 0)
+      if (access (name, X_OK) == 0)
 	return xstrdup (name);
 
       return NULL;
     }
 
-  const char *suffix = (mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : "";
+  const char *suffix = 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.  */
@@ -3149,12 +3131,12 @@  find_a_program (const char *name)
       if (suffix_len)
 	{
 	  memcpy (path + len, suffix, suffix_len + 1);
-	  if (access_check (path, mode) == 0)
+	  if (access_check (path, X_OK) == 0)
 	    return path;
 	}
 
       path[len] = '\0';
-      if (access_check (path, mode) == 0)
+      if (access_check (path, X_OK) == 0)
 	return path;
 
       return NULL;
@@ -5814,7 +5796,7 @@  end_going_arg (void)
 	string = find_file (string);
       if (this_is_linker_script)
 	{
-	  char * full_script_path = find_a_file (&startfile_prefixes, string, R_OK, true);
+	  char * full_script_path = find_a_file (&startfile_prefixes, string, true);
 
 	  if (full_script_path == NULL)
 	    {
@@ -8120,7 +8102,7 @@  out:
 static const char *
 find_file (const char *name)
 {
-  char *newname = find_a_file (&startfile_prefixes, name, R_OK, true);
+  char *newname = find_a_file (&startfile_prefixes, name, true);
   return newname ? newname : name;
 }
 
@@ -8546,7 +8528,7 @@  driver::set_up_specs () const
 			   accel_dir_suffix, dir_separator_str, NULL);
   just_machine_suffix = concat (spec_machine, dir_separator_str, NULL);
 
-  specs_file = find_a_file (&startfile_prefixes, "specs", R_OK, true);
+  specs_file = find_a_file (&startfile_prefixes, "specs", true);
   /* Read the specs file unless it is a default one.  */
   if (specs_file != 0 && strcmp (specs_file, "specs"))
     read_specs (specs_file, true, false);
@@ -8690,7 +8672,7 @@  driver::set_up_specs () const
   for (struct user_specs *uptr = user_specs_head; uptr; uptr = uptr->next)
     {
       char *filename = find_a_file (&startfile_prefixes, uptr->filename,
-				    R_OK, true);
+				    true);
       read_specs (filename ? filename : uptr->filename, false, true);
     }
 
@@ -9319,7 +9301,7 @@  driver::maybe_run_linker (const char *argv0) const
 #endif
 	    {
 	      char *temp_spec = find_a_file (&exec_prefixes,
-					     LTOPLUGINSONAME, R_OK,
+					     LTOPLUGINSONAME,
 					     false);
 	      if (!temp_spec)
 		fatal_error (input_location,
@@ -10808,7 +10790,7 @@  include_spec_function (int argc, const char **argv)
   if (argc != 1)
     abort ();
 
-  file = find_a_file (&startfile_prefixes, argv[0], R_OK, true);
+  file = find_a_file (&startfile_prefixes, argv[0], true);
   read_specs (file ? file : argv[0], false, false);
 
   return NULL;
@@ -11245,12 +11227,12 @@  find_fortran_preinclude_file (int argc, const char **argv)
 			     NULL, 0, 0, 0);
 #endif
 
-  const char *path = find_a_file (&include_prefixes, argv[1], R_OK, false);
+  const char *path = find_a_file (&include_prefixes, argv[1], false);
   if (path != NULL)
     result = concat (argv[0], path, NULL);
   else
     {
-      path = find_a_file (&prefixes, argv[1], R_OK, false);
+      path = find_a_file (&prefixes, argv[1], false);
       if (path != NULL)
 	result = concat (argv[0], path, NULL);
     }