add run linux commands
This commit is contained in:
parent
dbd0a07f57
commit
ed20a19521
@ -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"));
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user