]> git.tdb.fi Git - builder.git/blobdiff - source/misc.cpp
Use mspio for all I/O operations
[builder.git] / source / misc.cpp
index 37616a95f3dfeba4a5994d6f00eb727f04caaccf..cf97c5506db3526535cfdbbfe3e3a9390224e8e6 100644 (file)
@@ -1,12 +1,21 @@
-#include <iostream>
+/* $Id$
+
+This file is part of builder
+Copyright © 2006-2009  Mikko Rasa, Mikkosoft Productions
+Distributed under the LGPL
+*/
+
 #include <sys/wait.h>
-#include <msp/iter.h>
+#include <fcntl.h>
+#include <cstdlib>
+#include <cstring>
+#include <msp/io/print.h>
 #include "misc.h"
 
 using namespace std;
 using namespace Msp;
 
-string run_command(const list<string> &argv)
+string run_command(const StringList &argv)
 {
        int pfd[2];
        pipe(pfd);
@@ -17,17 +26,24 @@ string run_command(const list<string> &argv)
        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;
+
+               unsigned j=0;
+               for(StringList::const_iterator i=argv.begin(); i!=argv.end(); ++i)
+                       argv_[j++]=strdup(i->c_str());
+               argv_[j]=0;
+
                close(pfd[0]);
                dup2(pfd[1], 1);
-               dup2(pfd[1], 2);
+               close(pfd[1]);
+               int devnull=open("/dev/null", O_WRONLY);
+               dup2(devnull, 2);
+               close(devnull);
+
                execvp(argv_[0], argv_);
                ::exit(1);
        }
        else if(pid==-1)
-               cerr<<"Failed to execute "<<argv.front()<<'\n';
+               IO::print(IO::cerr, "Failed to execute %s\n", argv.front());
        else
        {
                close(pfd[1]);
@@ -43,8 +59,9 @@ string run_command(const list<string> &argv)
                        else
                                result.append(buf, len);
                }
+               close(pfd[0]);
        }
-       
+
        return result;
 }