X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fmisc.cpp;h=aadc056ea334f3784ad5adf124ca5919c465239d;hb=8d7926359d2477a9928d7367678314bcbc1f6e81;hp=cf97c5506db3526535cfdbbfe3e3a9390224e8e6;hpb=5622fc20f0be8bff0938d24f6f45d3ab384288ca;p=builder.git diff --git a/source/misc.cpp b/source/misc.cpp index cf97c55..aadc056 100644 --- a/source/misc.cpp +++ b/source/misc.cpp @@ -15,27 +15,27 @@ Distributed under the LGPL using namespace std; using namespace Msp; -string run_command(const StringList &argv) +string run_command(const StringList &argv, int *status) { int pfd[2]; pipe(pfd); string result; - pid_t pid=fork(); + pid_t pid = fork(); if(pid==0) { char *argv_[argv.size()+1]; - unsigned j=0; + unsigned j = 0; for(StringList::const_iterator i=argv.begin(); i!=argv.end(); ++i) - argv_[j++]=strdup(i->c_str()); - argv_[j]=0; + argv_[j++] = strdup(i->c_str()); + argv_[j] = 0; close(pfd[0]); dup2(pfd[1], 1); close(pfd[1]); - int devnull=open("/dev/null", O_WRONLY); + int devnull = open("/dev/null", O_WRONLY); dup2(devnull, 2); close(devnull); @@ -50,11 +50,21 @@ string run_command(const StringList &argv) while(1) { char buf[1024]; - int len=read(pfd[0], buf, sizeof(buf)); + 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);