When using -k, make sure to flush all of the subprocess's output to
[pintos-anon] / src / utils / pintos
index 856ebfa473ac270c70858547aabef498d7360f8d..079ab5a34d22e8970a631203acdf6913e2a7a4fe 100755 (executable)
@@ -701,12 +701,18 @@ sub xsystem {
            # Filter output.
            my ($buf) = "";
            my ($boots) = 0;
-           while (waitpid ($pid, WNOHANG) == 0) {
+           local ($|) = 1;
+           for (;;) {
+               if (waitpid ($pid, WNOHANG) == 0) {
+                   # Subprocess died.  Pass through any remaining data.
+                   print $buf while sysread ($in, $buf, 4096) > 0;
+                   last;
+               }
+
                # Read and print out pipe data.
                my ($len) = length ($buf);
-               waitpid ($pid, 0), last
-                 if sysread ($in, $buf, 4096, $len) <= 0;
-               print STDOUT substr ($buf, $len);
+               last if sysread ($in, $buf, 4096, $len) <= 0;
+               print substr ($buf, $len);
 
                # Remove full lines from $buf and scan them for keywords.
                while ((my $idx = index ($buf, "\n")) >= 0) {
@@ -724,9 +730,8 @@ sub xsystem {
                    }
                }
            }
-       } else {
-           waitpid ($pid, 0);
        }
+       waitpid ($pid, 0);
        alarm (0);
        &$cleanup ();