mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
36 lines
647 B
C++
36 lines
647 B
C++
#pragma once
|
|
|
|
#include <sys/wait.h>
|
|
|
|
namespace waybar::util::command {
|
|
|
|
struct cmd_res {
|
|
int exit_code;
|
|
std::string out;
|
|
};
|
|
|
|
inline struct cmd_res exec(const std::string cmd)
|
|
{
|
|
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 };
|
|
}
|
|
|
|
}
|