From 4491e4e22e1b3ab48cc0e8af62b149afe3cb5231 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 15 Mar 2016 21:40:33 -0400 Subject: [PATCH] add an initial hacky svn-runner capturing subversion standard output --- src/SvnRunner.cc | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ src/SvnRunner.h | 8 ++++++ src/svi.cc | 2 ++ 3 files changed, 83 insertions(+) create mode 100644 src/SvnRunner.cc create mode 100644 src/SvnRunner.h diff --git a/src/SvnRunner.cc b/src/SvnRunner.cc new file mode 100644 index 0000000..de07219 --- /dev/null +++ b/src/SvnRunner.cc @@ -0,0 +1,73 @@ +#include "SvnRunner.h" +#include +#include +#include +#include +#include + +void SvnRunner::exec_svn(std::vector arguments) +{ + int fd[2]; + int pipe_rc = pipe(fd); + if (pipe_rc == -1) + { + perror("pipe"); + return; + } + pid_t pid = fork(); + if (pid == -1) + { + perror("fork"); + return; + } + if (pid == 0) + { + close(fd[0]); + int dup2_rc = dup2(fd[1], STDOUT_FILENO); + if (dup2_rc == -1) + { + perror("dup2"); + return; + } + char const * argv[arguments.size() + 2]; + argv[0] = "svn"; + argv[arguments.size() + 1] = NULL; + for (unsigned int i = 0; i < arguments.size(); i++) + { + argv[i + 1] = arguments[i].c_str(); + } + int exec_rc = execvp("/usr/bin/svn", (char **)argv); + if (exec_rc == -1) + { + perror("exec"); + exit(1); + } + } + else + { + close(fd[1]); + size_t n = 100; + char * line = (char *)malloc(n); + FILE * in = fdopen(fd[0], "r"); + ssize_t getline_rc; + for (;;) + { + getline_rc = getline(&line, &n, in); + if (getline_rc == -1) + break; + printf("Got >%s<\n", line); + } + fclose(in); + int status; + int waitpid_rc = waitpid(pid, &status, 0); + if (waitpid_rc == -1) + { + perror("waitpid"); + } + if (WEXITSTATUS(status) != 0) + { + fprintf(stderr, "Warning: svn exit status = %d\n", WEXITSTATUS(status)); + } + free(line); + } +} diff --git a/src/SvnRunner.h b/src/SvnRunner.h new file mode 100644 index 0000000..a36d180 --- /dev/null +++ b/src/SvnRunner.h @@ -0,0 +1,8 @@ +#include +#include + +class SvnRunner +{ +public: + static void exec_svn(std::vector arguments); +}; diff --git a/src/svi.cc b/src/svi.cc index 9373e18..402acea 100644 --- a/src/svi.cc +++ b/src/svi.cc @@ -1,8 +1,10 @@ #include +#include "SvnRunner.h" using namespace std; int main(int argc, char * argv[]) { cout << "Hello there." << endl; + SvnRunner::exec_svn({"ls"}); }