From ac2181d92bfcfd487e7c6b28dea657ad8771dcdb Mon Sep 17 00:00:00 2001 From: Simon Fels Date: Wed, 12 Jul 2017 09:26:21 +0200 Subject: [PATCH] Add simple helper function to find program in PATH --- src/anbox/utils.cpp | 15 ++++++++++++++- src/anbox/utils.h | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/anbox/utils.cpp b/src/anbox/utils.cpp index a4f0c8fb..df3a815d 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 28b95a13..4b45a088 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 -- GitLab