From: John Ericson Date: Sun, 7 Jun 2026 04:15:17 +0000 (-0600) Subject: [PATCH v6 1/4] find_a_program: Separate from find_a_file X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=948eb02800777d0318ee2a38bf32076afee739f2;p=thirdparty%2Fgcc.git [PATCH v6 1/4] find_a_program: Separate from find_a_file 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 --- diff --git a/gcc/gcc.cc b/gcc/gcc.cc index feefa0d48f3..029ec428ce0 100644 --- a/gcc/gcc.cc +++ b/gcc/gcc.cc @@ -3131,7 +3131,50 @@ find_a_program (const char *name) return xstrdup (DEFAULT_WINDRES); #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