diff --git a/bin/main.cpp b/bin/main.cpp index 6eb7c59..4c6fdf5 100644 --- a/bin/main.cpp +++ b/bin/main.cpp @@ -178,4 +178,8 @@ int main(int argc, char *argv[]) auto combine ( hack::utils::func_concat(plus, minus) ); hack::log("")("func_concat result: ", combine(3)); } + + {// ex: utils::exec + hack::log()(hack::utils::exec("ls")); + } } diff --git a/src/utils/utils.hpp b/src/utils/utils.hpp index 6b1cfc9..c2b53d6 100644 --- a/src/utils/utils.hpp +++ b/src/utils/utils.hpp @@ -2,6 +2,8 @@ #include #include +#include +#include namespace hack::utils { @@ -43,4 +45,23 @@ namespace hack::utils }; } } + + inline std::string exec(const char* cmd) + { + std::array buffer; + std::string result; + std::unique_ptr pipe(popen(cmd, "r"), pclose); + + if (!pipe) + { + throw std::runtime_error("popen() failed!"); + } + + while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) + { + result += buffer.data(); + } + + return result; + } }