1#include <cstdio>
2#include <iostream>
3#include <memory>
4#include <stdexcept>
5#include <string>
6#include <array>
7
8std::string exec(const char* cmd) {
9 std::array<char, 128> buffer;
10 std::string result;
11 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
12 if (!pipe) {
13 throw std::runtime_error("popen() failed!");
14 }
15 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
16 result += buffer.data();
17 }
18 return result;
19}
20