2018-08-18 15:54:20 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
namespace waybar::util::command {
|
|
|
|
|
2018-09-05 17:20:19 +00:00
|
|
|
struct res {
|
2018-08-18 15:54:20 +00:00
|
|
|
int exit_code;
|
|
|
|
std::string out;
|
|
|
|
};
|
|
|
|
|
2018-09-05 17:20:19 +00:00
|
|
|
inline struct res exec(const std::string cmd)
|
2018-08-18 15:54:20 +00:00
|
|
|
{
|
|
|
|
FILE* fp(popen(cmd.c_str(), "r"));
|
|
|
|
if (!fp) {
|
|
|
|
return { -1, "" };
|
|
|
|
}
|
|
|
|
|
|
|
|
std::array<char, 128> buffer = {0};
|
|
|
|
std::string output;
|
|
|
|
while (feof(fp) == 0) {
|
|
|
|
if (fgets(buffer.data(), 128, fp) != nullptr) {
|
|
|
|
output += buffer.data();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove last newline
|
|
|
|
if (!output.empty() && output[output.length()-1] == '\n') {
|
|
|
|
output.erase(output.length()-1);
|
|
|
|
}
|
|
|
|
int exit_code = WEXITSTATUS(pclose(fp));
|
|
|
|
return { exit_code, output };
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|