shithub: mc

Download patch

ref: d260994d8a89a651868a21677a42a9d21de7e46f
parent: aa906e108823e155dd2c5e9b1b4ef6967f55473b
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Sep 4 19:32:28 EDT 2016

Fix spork, remove sporkfd.

	Sporkfd is too hard to use, too many fds to keep track of.

--- a/lib/std/spork.myr
+++ b/lib/std/spork.myr
@@ -8,7 +8,6 @@
 pkg std =
 	const spork	: (cmd : byte[:][:]	-> result((pid, fd, fd), errno))
 	const espork	: (cmd : byte[:][:]	-> result((pid, fd, fd, fd), errno))
-	const sporkfd	: (cmd : byte[:][:], infd : fd, outfd : fd, errfd : fd	-> result(pid, errno))
 ;;
 
 const spork = {cmd
@@ -25,7 +24,7 @@
 		-> `Err err
 	;;
 
-	match sporkfd(cmd, infds[0], outfds[1], 2)
+	match sporkfd(cmd, infds, outfds, [-1, 2])
 	| `Ok pid:
 		/* close unused fd ends */
 		close(infds[0]);
@@ -54,7 +53,7 @@
 		-> `Err err
 	;;
 
-	match sporkfd(cmd, infds[0], outfds[1], errfds[1])
+	match sporkfd(cmd, infds, outfds, errfds)
 	| `Ok pid:
 		/* close unused fd ends */
 		close(infds[0]);
@@ -66,7 +65,7 @@
 	;;
 }
 
-const sporkfd = {cmd, infd, outfd, errfd
+const sporkfd = {cmd, infds, outfds, errfds
 	var pid, err
 
 	pid = fork()
@@ -76,21 +75,35 @@
 	/* child */
 	elif pid == 0
 		/* stdin/stdout for our communication. */
-		match dup2(infd, 0)
+		match dup2(infds[0], 0)
 		| `Ok _:	/* nothing */
 		| `Err e:	-> `Err e
 		;;
-		match dup2(outfd, 1)
+		match dup2(outfds[1], 1)
 		| `Ok _:	/* nothing */
 		| `Err e:	-> `Err e
 		;;
-		match dup2(errfd, 2)
+		match dup2(errfds[1], 2)
 		| `Ok _:	/* nothing */
 		| `Err e:	-> `Err e
 		;;
-		close(infd)
-		close(outfd)
-		close(errfd)
+
+		/* close the fds we duped */
+		if infds[0] != 0
+			close(infds[0])
+		;;
+		if outfds[1] != 1
+			close(outfds[1])
+		;;
+		if errfds[1] != 2
+			close(errfds[1])
+		;;
+
+		/* close the unused ends */
+		close(infds[1])
+		close(outfds[0])
+		close(errfds[0])
+
 		err = execvp(cmd[0], cmd)
 		if err != Enone
 			-> `Err err