提交 ac2181d9 编写于 作者: S Simon Fels

Add simple helper function to find program in PATH

上级 00ea7d96
...@@ -27,6 +27,8 @@ ...@@ -27,6 +27,8 @@
#include <fcntl.h> #include <fcntl.h>
#include <mntent.h> #include <mntent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "anbox/utils.h" #include "anbox/utils.h"
...@@ -34,7 +36,6 @@ namespace fs = boost::filesystem; ...@@ -34,7 +36,6 @@ namespace fs = boost::filesystem;
namespace anbox { namespace anbox {
namespace utils { namespace utils {
std::vector<std::string> collect_arguments(int argc, char **argv) { std::vector<std::string> collect_arguments(int argc, char **argv) {
std::vector<std::string> result; std::vector<std::string> result;
for (int i = 1; i < argc; i++) result.push_back(argv[i]); for (int i = 1; i < argc; i++) result.push_back(argv[i]);
...@@ -193,5 +194,17 @@ bool is_mounted(const std::string &path) { ...@@ -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 utils
} // namespace anbox } // namespace anbox
...@@ -56,6 +56,8 @@ std::string process_get_exe_path(const pid_t &pid); ...@@ -56,6 +56,8 @@ std::string process_get_exe_path(const pid_t &pid);
bool is_mounted(const std::string &path); bool is_mounted(const std::string &path);
std::string find_program_on_path(const std::string &name);
template <typename... Types> template <typename... Types>
static std::string string_format(const std::string &fmt_str, Types &&... args); static std::string string_format(const std::string &fmt_str, Types &&... args);
} // namespace utils } // namespace utils
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册