5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/guns.rb', line 5
def sh(cmd, *args)
env = args.pop if args.last.kind_of?(Hash)
rout, wout = IO.pipe
rerr, werr = IO.pipe
GC.disable
if fork
wout.close
werr.close
Process.wait
exitstatus = $?.exitstatus
out = rout.read
err = rerr.read
rout.close
rerr.close
[out, err, exitstatus]
else
if env
env.each {|k,v| ENV[k] = v}
end
STDOUT.reopen(wout)
STDERR.reopen(werr)
$0 = "#$0 -> [guns] #{cmd}"
system(cmd, *args)
exit! $?.exitstatus
end
ensure
GC.enable
end
|