]> git.tdb.fi Git - builder.git/blobdiff - source/misc.cpp
Set PKG_CONFIG_PATH to include prefix
[builder.git] / source / misc.cpp
index ab61d9f1fe0f38058917de552b6d8bfc3b0deb19..2409e05a650a7fb5c67885d799f9359ad550037b 100644 (file)
@@ -1,25 +1,21 @@
 /* $Id$
 
 This file is part of builder
-Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
+Copyright © 2006-200 Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
-#include <iostream>
 #include <sys/wait.h>
 #include <fcntl.h>
 #include <cstdlib>
 #include <cstring>
+#include <msp/io/print.h>
 #include "misc.h"
 
 using namespace std;
 using namespace Msp;
 
-/**
-Runs a command and returns its output as a string.  The exit status of the
-command is lost.
-*/
-string run_command(const StringList &argv)
+string run_command(const StringList &argv, int *status)
 {
        int pfd[2];
        pipe(pfd);
@@ -47,7 +43,7 @@ string run_command(const StringList &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]);
@@ -57,12 +53,23 @@ string run_command(const StringList &argv)
                        int len=read(pfd[0], buf, sizeof(buf));
                        if(len<=0)
                        {
-                               if(waitpid(pid, 0, WNOHANG))
+                               int s;
+                               if(waitpid(pid, &s, WNOHANG))
+                               {
+                                       if(status)
+                                       {
+                                               if(WIFEXITED(s))
+                                                       *status=WEXITSTATUS(s);
+                                               else
+                                                       *status=-1;
+                                       }
                                        break;
+                               }
                        }
                        else
                                result.append(buf, len);
                }
+               close(pfd[0]);
        }
 
        return result;