26inline int run_bash(
const std::string& filename,
const std::vector<char*>& arguments) {
27 std::vector<std::string> exec_args;
28 exec_args.reserve(2 + arguments.size());
29 exec_args.emplace_back(
"bash");
30 exec_args.emplace_back(filename);
31 for (
auto* argument : arguments) {
32 exec_args.emplace_back(argument ? argument :
"");
35 std::vector<char*> argv;
36 argv.reserve(exec_args.size() + 1);
37 for (
auto& s : exec_args) {
38 argv.push_back(s.data());
40 argv.push_back(
nullptr);
44 std::error_code ec(errno, std::generic_category());
50 execvp(argv[0], argv.data());
59 waited = waitpid(child, &status, 0);
60 }
while (waited < 0 && errno == EINTR);
63 std::error_code ec(errno, std::generic_category());
67 if (WIFEXITED(status)) {
68 return WEXITSTATUS(status);
69 }
else if (WIFSIGNALED(status)) {
77 return 128 + WTERMSIG(status);
int run_bash(const std::string &filename, const std::vector< char * > &arguments)
Runs a Bash script with the given filename and arguments, and returns the exit code of the script.
Definition run_bash.h:26