You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rfmon-to-influx/helper/exec.hpp

32 lines
868 B
C++

#ifndef B89BC3C5_AD59_4765_AA06_8110111D316F
#define B89BC3C5_AD59_4765_AA06_8110111D316F
#include <cstdio>
#include <stdexcept>
/// @brief Executes given command and optionally sends buffer to handler
/// @param cmd is the command
/// @param handler is the handler(char*)-function
/// @return Return-code form command
int exec(const char* cmd, const int *(handler)(char*) = nullptr){
const int buf_size = 256;
char buf[buf_size];
// Open execution-pipe
FILE *pipe = popen(cmd, "r");
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buf, buf_size, pipe) != nullptr) {
// When a handler is specified, call it
if(handler != nullptr) (*handler)(buf);
}
// Close pipe and read exit-code
return WEXITSTATUS(pclose(pipe));
}
#endif /* B89BC3C5_AD59_4765_AA06_8110111D316F */