summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-10-25 04:48:41 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-10-25 04:48:41 +0000
commit9b5c48abedd1fc81709589deeb82846c3d17eb1d (patch)
treea68ad5aeb59d3ba25da856a99f2e5344ad46fda3 /lib
parent52a2acbad3e0252ba3c4be59d92262d003615a72 (diff)
matz
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1014 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/open3.rb39
1 files changed, 21 insertions, 18 deletions
diff --git a/lib/open3.rb b/lib/open3.rb
index 11b9813bee..18984f5726 100644
--- a/lib/open3.rb
+++ b/lib/open3.rb
@@ -9,39 +9,42 @@
module Open3
#[stdin, stdout, stderr] = popen3(command);
- def popen3(cmd)
+ def popen3(*cmd)
pw = IO::pipe # pipe[0] for read, pipe[1] for write
pr = IO::pipe
pe = IO::pipe
pid = fork{
# child
- pw[1].close
- STDIN.reopen(pw[0])
- pw[0].close
+ fork{
+ # grandchild
+ pw[1].close
+ STDIN.reopen(pw[0])
+ pw[0].close
- pr[0].close
- STDOUT.reopen(pr[1])
- pr[1].close
+ pr[0].close
+ STDOUT.reopen(pr[1])
+ pr[1].close
- pe[0].close
- STDERR.reopen(pe[1])
- pe[1].close
+ pe[0].close
+ STDERR.reopen(pe[1])
+ pe[1].close
- exec(cmd)
- _exit 127
+ exec(*cmd)
+ }
}
pw[0].close
pr[1].close
pe[1].close
- Thread.start do
- sleep 1
- Process.waitpid(pid)
- end
- pi = [ pw[1], pr[0], pe[0] ]
+ Process.waitpid(pid)
+ pi = [pw[1], pr[0], pe[0]]
if defined? yield
- return yield *pi
+ begin
+ return yield *pi
+ ensure
+ pi.each{|p| p.close unless p.closed?}
+ end
end
pi
end