]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
C++-ify execute functions
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 28 Jul 2020 18:06:36 +0000 (20:06 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 29 Jul 2020 14:43:59 +0000 (16:43 +0200)
src/ccache.cpp
src/execute.cpp
src/execute.hpp
src/hashutil.cpp

index ccae62e1c1c253228eb2369fbe56a830257f0a2a..d535ade01bd94f7a9b239cab01c0ab521ff9f783 100644 (file)
@@ -145,7 +145,7 @@ add_prefix(const Context& ctx, Args& args, const std::string& prefix_command)
 
   Args prefix;
   for (const auto& word : Util::split_into_strings(prefix_command, " ")) {
-    std::string path = find_executable(ctx, word.c_str(), MYNAME);
+    std::string path = find_executable(ctx, word, MYNAME);
     if (path.empty()) {
       FATAL("{}: {}", word, strerror(errno));
     }
@@ -1736,7 +1736,7 @@ find_compiler(Context& ctx, const char* const* argv)
     base = ctx.config.compiler();
   }
 
-  std::string compiler = find_executable(ctx, base.c_str(), MYNAME);
+  std::string compiler = find_executable(ctx, base, MYNAME);
   if (compiler.empty()) {
     FATAL("Could not find compiler \"{}\" in PATH", base);
   }
index 003b28768d3dec373c71a2d7ef735e2f3502f240..e444a70cd730400ea27a6a33b685a787ef3ec105 100644 (file)
@@ -114,9 +114,8 @@ win32getshell(const char* path)
 {
   const char* path_env = getenv("PATH");
   std::string sh;
-  std::string ext = std::string(Util::get_extension(path));
-  if (!ext.empty() && strcasecmp(ext.c_str(), ".sh") == 0 && path_env) {
-    sh = find_executable_in_path("sh.exe", nullptr, path_env);
+  if (Util::to_lowercase(Util::get_extension(path)) == ".sh" && path_env) {
+    sh = find_executable_in_path("sh.exe", "", path_env);
   }
   if (sh.empty() && getenv("CCACHE_DETECT_SHEBANG")) {
     // Detect shebang.
@@ -125,7 +124,7 @@ win32getshell(const char* path)
       char buf[10] = {0};
       fgets(buf, sizeof(buf) - 1, fp.get());
       if (std::string(buf) == "#!/bin/sh" && path_env) {
-        sh = find_executable_in_path("sh.exe", NULL, path_env);
+        sh = find_executable_in_path("sh.exe", "", path_env);
       }
     }
   }
@@ -297,43 +296,43 @@ execute(const char* const* argv, Fd&& fd_out, Fd&& fd_err, pid_t* pid)
 }
 #endif
 
-// Find an executable by name in $PATH. Exclude any that are links to
-// exclude_name.
 std::string
-find_executable(const Context& ctx, const char* name, const char* exclude_name)
+find_executable(const Context& ctx,
+                const std::string& name,
+                const std::string& exclude_name)
 {
   if (Util::is_absolute_path(name)) {
     return name;
   }
 
-  const char* path = ctx.config.path().c_str();
-  if (str_eq(path, "")) {
+  std::string path = ctx.config.path();
+  if (path.empty()) {
     path = getenv("PATH");
   }
-  if (!path) {
+  if (path.empty()) {
     cc_log("No PATH variable");
-    return "";
+    return {};
   }
 
   return find_executable_in_path(name, exclude_name, path);
 }
 
 std::string
-find_executable_in_path(const char* name,
-                        const char* exclude_name,
-                        const char* path)
+find_executable_in_path(const std::string& name,
+                        const std::string& exclude_name,
+                        const std::string& path)
 {
-  if (!path) {
+  if (path.empty()) {
     return {};
   }
 
-  // Search the path looking for the first compiler of the right name that
-  // isn't us.
+  // Search the path looking for the first compiler of the right name that isn't
+  // us.
   for (const std::string& dir : Util::split_into_strings(path, PATH_DELIM)) {
 #ifdef _WIN32
     char namebuf[MAX_PATH];
-    int ret =
-      SearchPath(dir.c_str(), name, nullptr, sizeof(namebuf), namebuf, nullptr);
+    int ret = SearchPath(
+      dir.c_str(), name.c_str(), nullptr, sizeof(namebuf), namebuf, nullptr);
     if (!ret) {
       std::string exename = fmt::format("{}.exe", name);
       ret = SearchPath(dir.c_str(),
@@ -345,10 +344,10 @@ find_executable_in_path(const char* name,
     }
     (void)exclude_name;
     if (ret) {
-      return std::string(namebuf);
+      return namebuf;
     }
 #else
-    assert(exclude_name);
+    assert(!exclude_name.empty());
     std::string fname = fmt::format("{}/{}", dir, name);
     auto st1 = Stat::lstat(fname);
     auto st2 = Stat::stat(fname);
@@ -368,5 +367,5 @@ find_executable_in_path(const char* name,
 #endif
   }
 
-  return "";
+  return {};
 }
index e66f73a285ba2464b5c8f541a0fc8f832d767b6a..5bcff8ccdc80ec467916ed99c48511d5d70bff62 100644 (file)
 class Context;
 
 int execute(const char* const* argv, Fd&& fd_out, Fd&& fd_err, pid_t* pid);
-std::string
-find_executable(const Context& ctx, const char* name, const char* exclude_name);
-std::string find_executable_in_path(const char* name,
-                                    const char* exclude_name,
-                                    const char* path);
+
+// Find an executable named `name` in `$PATH`. Exclude any executables that are
+// links to `exclude_name`.
+std::string find_executable(const Context& ctx,
+                            const std::string& name,
+                            const std::string& exclude_name);
+
+std::string find_executable_in_path(const std::string& name,
+                                    const std::string& exclude_name,
+                                    const std::string& path);
 
 #ifdef _WIN32
 char* win32argvtos(const char* prefix, const char* const* argv, int* length);
index f636f6886ff81a55c1b9043c91784256a03d8964..a43e14cd4eef67fd45ac3690052da5707732913d 100644 (file)
@@ -419,8 +419,7 @@ hash_command_output(Hash& hash, const char* command, const char* compiler)
   STARTUPINFO si;
   memset(&si, 0x00, sizeof(si));
 
-  std::string path =
-    find_executable_in_path(args[0].c_str(), nullptr, getenv("PATH"));
+  std::string path = find_executable_in_path(args[0], "", getenv("PATH"));
   if (path.empty()) {
     path = args[0];
   }