]> git.tdb.fi Git - builder.git/blobdiff - source/misc.cpp
Add files.
[builder.git] / source / misc.cpp
diff --git a/source/misc.cpp b/source/misc.cpp
new file mode 100644 (file)
index 0000000..37616a9
--- /dev/null
@@ -0,0 +1,51 @@
+#include <iostream>
+#include <sys/wait.h>
+#include <msp/iter.h>
+#include "misc.h"
+
+using namespace std;
+using namespace Msp;
+
+string run_command(const list<string> &argv)
+{
+       int pfd[2];
+       pipe(pfd);
+
+       string result;
+
+       pid_t pid=fork();
+       if(pid==0)
+       {
+               char *argv_[argv.size()+1];
+               for(CountingIterator<const string, list<string>::const_iterator> i=argv.begin(); i!=argv.end(); ++i)
+                       argv_[i.count()]=strdup(i->c_str());
+               argv_[argv.size()]=0;
+               close(pfd[0]);
+               dup2(pfd[1], 1);
+               dup2(pfd[1], 2);
+               execvp(argv_[0], argv_);
+               ::exit(1);
+       }
+       else if(pid==-1)
+               cerr<<"Failed to execute "<<argv.front()<<'\n';
+       else
+       {
+               close(pfd[1]);
+               while(1)
+               {
+                       char buf[1024];
+                       int len=read(pfd[0], buf, sizeof(buf));
+                       if(len<=0)
+                       {
+                               if(waitpid(pid, 0, WNOHANG))
+                                       break;
+                       }
+                       else
+                               result.append(buf, len);
+               }
+       }
+       
+       return result;
+}
+
+