Rewrite of exec()
@@ -16,26 +16,15 @@ logData currentLogData = {};
std::unique_ptr<Logger> logger;
string exec(string command) {
- char buffer[128];
- string result = "";
-#ifdef __linux__
-
- // Open pipe to file
- FILE* pipe = popen(command.c_str(), "r");
+ std::array<char, 128> buffer;
+ std::string result;
+ std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(command.c_str(), "r"), pclose);
if (!pipe) {
return "popen failed!";
}
-
- // read till end of process:
- while (!feof(pipe)) {
-
- // use buffer to read and add to result
- if (fgets(buffer, 128, pipe) != NULL)
- result += buffer;
+ while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
+ result += buffer.data();
}
-
- pclose(pipe);
-#endif
return result;
}
