diff --git a/src/common/process.cpp b/src/common/process.cpp index f05b5723..47d36b7f 100644 --- a/src/common/process.cpp +++ b/src/common/process.cpp @@ -66,6 +66,19 @@ std::vector split_path( return search_path; } +std::optional search_in_path( + const std::vector& path, + const std::string_view& target) { + for (const auto& dir : path) { + ghc::filesystem::path candidate = dir / target; + if (access(candidate.c_str(), X_OK) == 0) { + return candidate; + } + } + + return std::nullopt; +} + ProcessEnvironment::ProcessEnvironment(char** initial_env) { // We'll need to read all strings from `initial_env`. They _should_ all be // zero-terminated strings, with a null pointer to indicate the end of the diff --git a/src/common/process.h b/src/common/process.h index 8cfceea5..d1407d2a 100644 --- a/src/common/process.h +++ b/src/common/process.h @@ -47,6 +47,14 @@ bool pid_running(pid_t pid); */ std::vector split_path(const std::string_view& path_env); +/** + * Search through a search path vector created by `split_path` for an executable + * binary called `target`, returning the first match if any. + */ +std::optional search_in_path( + const std::vector& path, + const std::string_view& target); + /** * Helper to create an `environ`-like environment object for passing to the * `exec*e()` family of functions.