m8
/
src
/
ob
/
sys_command.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "ob/sys_command.hh"
#include <string>
#include <array>
#include <memory>
namespace OB
{
int exec(std::string& result, std::string const& command)
{
const char* cmd {command.c_str()};
int const buf_len {1024};
std::array<char, buf_len> buf;
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe)
{
return -1;
}
while (!feof(pipe.get()))
{
if (fgets(buf.data(), buf_len, pipe.get()) != nullptr)
{
result += buf.data();
}
}
return 0;
}
} // namespace OB