1995-01-12 08:29:47 -04:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import string
|
|
|
|
|
|
|
|
MAXFD = 100 # Max number of file descriptors (os.getdtablesize()???)
|
|
|
|
|
1997-04-21 11:15:55 -03:00
|
|
|
def _popen2(cmd, capturestderr=0):
|
1995-08-07 17:17:23 -03:00
|
|
|
cmd = ['/bin/sh', '-c', cmd]
|
1995-01-12 08:29:47 -04:00
|
|
|
p2cread, p2cwrite = os.pipe()
|
|
|
|
c2pread, c2pwrite = os.pipe()
|
1997-04-21 11:15:55 -03:00
|
|
|
if capturestderr:
|
|
|
|
errout, errin = os.pipe()
|
1995-01-12 08:29:47 -04:00
|
|
|
pid = os.fork()
|
|
|
|
if pid == 0:
|
|
|
|
# Child
|
|
|
|
os.close(0)
|
|
|
|
os.close(1)
|
|
|
|
if os.dup(p2cread) <> 0:
|
|
|
|
sys.stderr.write('popen2: bad read dup\n')
|
|
|
|
if os.dup(c2pwrite) <> 1:
|
|
|
|
sys.stderr.write('popen2: bad write dup\n')
|
1997-04-21 11:15:55 -03:00
|
|
|
if capturestderr:
|
|
|
|
os.close(2)
|
|
|
|
if os.dup(errin) <> 2: pass
|
1995-01-12 08:29:47 -04:00
|
|
|
for i in range(3, MAXFD):
|
|
|
|
try:
|
|
|
|
os.close(i)
|
1997-04-21 11:15:55 -03:00
|
|
|
except: pass
|
1995-01-12 08:29:47 -04:00
|
|
|
try:
|
|
|
|
os.execv(cmd[0], cmd)
|
|
|
|
finally:
|
|
|
|
os._exit(1)
|
|
|
|
# Shouldn't come here, I guess
|
|
|
|
os._exit(1)
|
|
|
|
os.close(p2cread)
|
|
|
|
tochild = os.fdopen(p2cwrite, 'w')
|
|
|
|
os.close(c2pwrite)
|
|
|
|
fromchild = os.fdopen(c2pread, 'r')
|
1997-04-21 11:15:55 -03:00
|
|
|
if capturestderr:
|
|
|
|
os.close(errin)
|
|
|
|
childerr = os.fdopen(errout, 'r')
|
|
|
|
return fromchild, tochild, childerr
|
|
|
|
else:
|
|
|
|
return fromchild, tochild, None
|
|
|
|
|
|
|
|
def popen2(cmd):
|
|
|
|
return _popen2(cmd, 0)[:2]
|
|
|
|
|
|
|
|
def popen3(cmd):
|
|
|
|
return _popen2(cmd, 1)
|