update func my_system to get the output if needed
authorYigang Wen <yigangx.wen@intel.com>
Thu, 7 Nov 2013 06:17:26 +0000 (14:17 +0800)
committerZhang Qiang <qiang.z.zhang@intel.com>
Fri, 15 Nov 2013 01:08:32 +0000 (09:08 +0800)
update function my_system to return the output of child process
according to the context.

Change-Id: I49d7adba181e3f31ee254c8f808672656eda6d10

depanneur

index a20bd40d3084594abc5cb92219b006a7600000cd..8ac710078fcaf373df124867f2baa0c11498430b 100755 (executable)
--- a/depanneur
+++ b/depanneur
@@ -273,15 +273,24 @@ sub my_system {
     my $cmd = shift;
     debug("my_system: $cmd");
     my $ret;
-    defined(my $pid=fork) or die "Can not fork: $!\n";
-    unless ($pid) {
+    my @out = ();
+
+    defined(my $pid=open(PIPE, "-|")) or die "Can not fork: $!\n";
+    unless ($pid) {  # Child
+        open(STDERR, ">&STDOUT");
         exec ($cmd);
         exit -1;
+    } else {  # Parent
+        while (my $line = <PIPE>) {
+            print $line;
+            push @out, $line if wantarray;
+        }
+        waitpid ($pid,0);
+        $ret = $?;
+        close(PIPE);
+
+        return wantarray ? ($ret, @out): $ret;
     }
-    waitpid ($pid,0);
-    $ret = WIFEXITED($?);
-    $ret = $?;
-    return $ret;
 }
 
 sub expand_filename {