add an initial hacky svn-runner capturing subversion standard output

This commit is contained in:
Josh Holtrop 2016-03-15 21:40:33 -04:00
parent d044fc5055
commit 4491e4e22e
3 changed files with 83 additions and 0 deletions

73
src/SvnRunner.cc Normal file
View File

@ -0,0 +1,73 @@
#include "SvnRunner.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
void SvnRunner::exec_svn(std::vector<std::string> 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);
}
}

8
src/SvnRunner.h Normal file
View File

@ -0,0 +1,8 @@
#include <string>
#include <vector>
class SvnRunner
{
public:
static void exec_svn(std::vector<std::string> arguments);
};

View File

@ -1,8 +1,10 @@
#include <iostream>
#include "SvnRunner.h"
using namespace std;
int main(int argc, char * argv[])
{
cout << "Hello there." << endl;
SvnRunner::exec_svn({"ls"});
}