Files
dm-cli/dm-cli/ExecCommand.cpp
Vladimir N. Korotenko 57b6611085 Initial
2025-11-10 12:30:48 +03:00

25 lines
625 B
C++

#include "ExecCommand.hpp"
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
#ifdef WINDOWS
#define pclose _pclose
#define popen _popen
#endif // WINDOWS
std::string ExecCommand(const char* cmd) {
std::array<char, 512> 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(), static_cast<int>(buffer.size()), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}