mirror of
https://github.com/GothenburgBitFactory/taskwarrior.git
synced 2025-06-26 10:54:26 +02:00
exec
- moved function that executes external commands from Transport.cpp to util.cpp since it is not dedicated to push/pull/merge anymore
This commit is contained in:
parent
398371d324
commit
7e1f8591f6
3 changed files with 54 additions and 45 deletions
51
src/util.cpp
51
src/util.cpp
|
@ -31,6 +31,7 @@
|
|||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/wait.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
@ -380,3 +381,53 @@ std::string compressIds (const std::vector <int>& ids)
|
|||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Run an external executable with execvp. This means stdio goes to
|
||||
// the child process, so that it can receive user input (e.g. passwords).
|
||||
//
|
||||
int execute(const std::string& executable, std::vector<std::string> arguments)
|
||||
{
|
||||
if (executable == "")
|
||||
return -1;
|
||||
|
||||
pid_t child_pid = fork();
|
||||
|
||||
if (child_pid == 0)
|
||||
{
|
||||
// this is done by the child process
|
||||
char shell[] = "bash";
|
||||
char opt[] = "-c";
|
||||
|
||||
std::string cmdline = executable;
|
||||
|
||||
std::vector <std::string>::iterator it;
|
||||
for (it = arguments.begin(); it != arguments.end(); ++it)
|
||||
{
|
||||
cmdline += " " + (std::string)*it;
|
||||
}
|
||||
|
||||
char** argv = new char*[4];
|
||||
argv[0] = shell; // bash
|
||||
argv[1] = opt; // -c
|
||||
argv[2] = (char*)cmdline.c_str(); // e.g. scp undo.data user@host:.task/
|
||||
argv[3] = NULL; // required by execv
|
||||
|
||||
int ret = execvp(shell, argv);
|
||||
delete[] argv;
|
||||
|
||||
exit(ret);
|
||||
}
|
||||
else
|
||||
{
|
||||
// this is done by the parent process
|
||||
int child_status;
|
||||
|
||||
pid_t pid = waitpid(child_pid, &child_status, 0);
|
||||
|
||||
if (pid == -1)
|
||||
return -1;
|
||||
else
|
||||
return child_status;
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue