diff --git a/src/anbox/utils.cpp b/src/anbox/utils.cpp index a4f0c8fb5062b31811fb60265a67e5dcf19edefd..df3a815d18880784671997212e968171b93c8d62 100644 --- a/src/anbox/utils.cpp +++ b/src/anbox/utils.cpp @@ -27,6 +27,8 @@ #include #include +#include +#include #include "anbox/utils.h" @@ -34,7 +36,6 @@ namespace fs = boost::filesystem; namespace anbox { namespace utils { - std::vector collect_arguments(int argc, char **argv) { std::vector result; for (int i = 1; i < argc; i++) result.push_back(argv[i]); @@ -193,5 +194,17 @@ bool is_mounted(const std::string &path) { } +std::string find_program_on_path(const std::string &name) { + struct stat sb; + std::string path = std::string(getenv("PATH")); + size_t start_pos = 0, end_pos = 0; + while ((end_pos = path.find(':', start_pos)) != std::string::npos) { + const auto current_path = path.substr(start_pos, end_pos - start_pos) + "/" + name; + if ((::stat(current_path.c_str(), &sb) == 0) && (sb.st_mode & S_IXOTH)) + return current_path; + start_pos = end_pos + 1; + } + return ""; +} } // namespace utils } // namespace anbox diff --git a/src/anbox/utils.h b/src/anbox/utils.h index 28b95a1313ae130fe07bfd6573ece073fefa6ad1..4b45a088e66c7024c5779cd937b4b0e0f12671f9 100644 --- a/src/anbox/utils.h +++ b/src/anbox/utils.h @@ -56,6 +56,8 @@ std::string process_get_exe_path(const pid_t &pid); bool is_mounted(const std::string &path); +std::string find_program_on_path(const std::string &name); + template static std::string string_format(const std::string &fmt_str, Types &&... args); } // namespace utils