560f4e
--- zsh-4.0.4/Src/builtin.c.open	Tue Oct 16 02:49:17 2001
560f4e
+++ zsh-4.0.4/Src/builtin.c	Wed May 15 11:55:32 2002
560f4e
@@ -5262,7 +5262,7 @@ bin_read(char *name, char **args, Options ops, UNUSED(int func))
560f4e
 	if (!zleactive) {
560f4e
 	    if (SHTTY == -1) {
560f4e
 		/* need to open /dev/tty specially */
560f4e
-		if ((SHTTY = open("/dev/tty", O_RDWR|O_NOCTTY)) != -1) {
560f4e
+		if ((SHTTY = block_open("/dev/tty", O_RDWR|O_NOCTTY)) != -1) {
560f4e
 		    haso = 1;
560f4e
 		    oshout = shout;
560f4e
 		    init_shout();
560f4e
--- zsh-4.0.4/Src/init.c.open	Wed Oct 24 04:16:32 2001
560f4e
+++ zsh-4.0.4/Src/init.c	Wed May 15 12:00:07 2002
560f4e
@@ -508,7 +508,7 @@ init_io(void)
560f4e
     if (isatty(0)) {
560f4e
 	zsfree(ttystrname);
560f4e
 	if ((ttystrname = ztrdup(ttyname(0)))) {
560f4e
-	    SHTTY = movefd(open(ttystrname, O_RDWR | O_NOCTTY));
560f4e
+	    SHTTY = movefd(block_open(ttystrname, O_RDWR | O_NOCTTY));
560f4e
 #ifdef TIOCNXCL
560f4e
 	    /*
560f4e
 	     * See if the terminal claims to be busy.  If so, and fd 0
560f4e
@@ -549,7 +549,7 @@ init_io(void)
560f4e
 	ttystrname = ztrdup(ttyname(1));
560f4e
     }
560f4e
     if (SHTTY == -1 &&
560f4e
-	(SHTTY = movefd(open("/dev/tty", O_RDWR | O_NOCTTY))) != -1) {
560f4e
+	(SHTTY = movefd(block_open("/dev/tty", O_RDWR | O_NOCTTY))) != -1) {
560f4e
 	zsfree(ttystrname);
560f4e
 	ttystrname = ztrdup(ttyname(SHTTY));
560f4e
     }
560f4e
@@ -1671,3 +1671,33 @@ zsh_main(UNUSED(int argc), char **argv)
560f4e
 		    : "use 'logout' to logout.");
560f4e
     }
560f4e
 }
560f4e
+
560f4e
+/**/
560f4e
+int
560f4e
+block_open (const char *tty, int flags)
560f4e
+{
560f4e
+    int saved_errno;
560f4e
+    int fd;
560f4e
+
560f4e
+    if ((flags & O_NONBLOCK) == 0) {
560f4e
+	fd = open (tty, flags | O_NONBLOCK);
560f4e
+	if (fd == -1)
560f4e
+	    return fd;
560f4e
+	flags = fcntl(fd, F_GETFL);
560f4e
+	if (flags == -1)
560f4e
+	    goto bad;
560f4e
+	flags &= ~O_NONBLOCK;
560f4e
+	if (fcntl(fd, F_SETFL, flags) == -1)
560f4e
+	    goto bad;
560f4e
+    }
560f4e
+    else
560f4e
+	fd = open (tty, flags);
560f4e
+
560f4e
+    return fd;
560f4e
+
560f4e
+bad:
560f4e
+    saved_errno = errno;
560f4e
+    close (fd);
560f4e
+    errno = saved_errno;
560f4e
+    return -1;
560f4e
+}