add run linux commands

This commit is contained in:
chatlanin 2022-04-04 14:53:33 +03:00
parent dbd0a07f57
commit ed20a19521
2 changed files with 25 additions and 0 deletions

View File

@ -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"));
}
}

View File

@ -2,6 +2,8 @@
#include <map>
#include <functional>
#include <memory>
#include <string>
namespace hack::utils
{
@ -43,4 +45,23 @@ namespace hack::utils
};
}
}
inline std::string exec(const char* cmd)
{
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> 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;
}
}