oksh-noxz

[fork] Portable OpenBSD ksh, based on the Public Domain Korn Shell (pdksh).
git clone https://noxz.tech/git/oksh-noxz.git
oksh-noxz

commit: 5dd5c001f007c562816e0562a3cabfad24beee77
parent: bc20e260f540302280cbd17d52dce424467fa037
author: Tim Sedlmeyer <tim@sedlmeyer.org>
date:   Mon, 4 Jun 2018 03:49:00 -0400
If F_DUPFD_CLOEXEC missing use F_DUPFD and set FD_CLOEXEC
Mio.c11+++++++++++
Mtty.c11+++++++++++
2 files changed, 22 insertions(+)
diff --git a/io.c b/io.c
@@ -259,6 +259,7 @@ savefd(int fd)
 	int nfd;
 
 	if (fd < FDBASE) {
+#ifdef F_DUPFD_CLOEXEC
 		nfd = fcntl(fd, F_DUPFD_CLOEXEC, FDBASE);
 		if (nfd < 0) {
 			if (errno == EBADF)
@@ -266,6 +267,16 @@ savefd(int fd)
 			else
 				errorf("too many files open in shell");
 		}
+#else
+		nfd = fcntl(fd, F_DUPFD, FDBASE);
+		if (nfd < 0) {
+			if (errno == EBADF)
+				return -1;
+			else
+				errorf("too many files open in shell");
+		}
+		fcntl(nfd, F_SETFD, FD_CLOEXEC);
+#endif
 	} else {
 		nfd = fd;
 		fcntl(nfd, F_SETFD, FD_CLOEXEC);
diff --git a/tty.c b/tty.c
@@ -49,9 +49,20 @@ tty_init(int init_ttystate)
 			return;
 		}
 	}
+#ifdef F_DUPFD_CLOEXEC
 	if ((tty_fd = fcntl(tfd, F_DUPFD_CLOEXEC, FDBASE)) < 0) {
 		warningf(false, "%s: dup of tty fd failed: %s",
 		    __func__, strerror(errno));
+#else
+	if ((tty_fd = fcntl(tfd, F_DUPFD, FDBASE)) < 0) {
+		warningf(false, "%s: dup of tty fd failed: %s",
+		    __func__, strerror(errno));
+	} else if (fcntl(tty_fd, F_SETFD, FD_CLOEXEC) < 0) {
+		warningf(false, "%s: set tty fd close-on-exec flag failed: %s",
+		    __func__, strerror(errno));
+		close(tty_fd);
+		tty_fd = -1;
+#endif
 	} else if (init_ttystate)
 		tcgetattr(tty_fd, &tty_state);
 	if (do_close)