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