From 227426ae7b205153d968058881070e65cec808f0 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Tue, 28 Jul 2020 20:06:36 +0200 Subject: [PATCH] C++-ify execute functions --- src/ccache.cpp | 4 ++-- src/execute.cpp | 43 +++++++++++++++++++++---------------------- src/execute.hpp | 15 ++++++++++----- src/hashutil.cpp | 3 +-- 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/ccache.cpp b/src/ccache.cpp index ccae62e1c..d535ade01 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -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); } diff --git a/src/execute.cpp b/src/execute.cpp index 003b28768..e444a70cd 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -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 {}; } diff --git a/src/execute.hpp b/src/execute.hpp index e66f73a28..5bcff8ccd 100644 --- a/src/execute.hpp +++ b/src/execute.hpp @@ -27,11 +27,16 @@ 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); diff --git a/src/hashutil.cpp b/src/hashutil.cpp index f636f6886..a43e14cd4 100644 --- a/src/hashutil.cpp +++ b/src/hashutil.cpp @@ -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]; } -- 2.47.2