Skip to content

Commit d47419b

Browse files
committed
os: Implement popen().
1 parent 92d32a6 commit d47419b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

os/os/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,23 @@ def urandom(n):
254254
import builtins
255255
with builtins.open("/dev/urandom", "rb") as f:
256256
return f.read(n)
257+
258+
def popen(cmd, mode="r"):
259+
import builtins
260+
i, o = pipe()
261+
if mode[0] == "w":
262+
i, o = o, i
263+
pid = fork()
264+
if not pid:
265+
if mode[0] == "r":
266+
close(1)
267+
else:
268+
close(0)
269+
close(i)
270+
dup(o)
271+
close(o)
272+
s = system(cmd)
273+
_exit(s)
274+
else:
275+
close(o)
276+
return builtins.open(i, mode)

0 commit comments

Comments
 (0)