Kamil Dudka b84eb4
 Src/builtin.c |  2 +-
Kamil Dudka b84eb4
 Src/init.c    | 34 ++++++++++++++++++++++++++++++++--
Kamil Dudka b84eb4
 2 files changed, 33 insertions(+), 3 deletions(-)
Kamil Dudka b84eb4
Kamil Dudka b84eb4
diff --git a/Src/builtin.c b/Src/builtin.c
Kamil Dudka b84eb4
index 9358e8b..ee14f91 100644
Kamil Dudka b84eb4
--- a/Src/builtin.c
Kamil Dudka b84eb4
+++ b/Src/builtin.c
Kamil Dudka b84eb4
@@ -5411,7 +5411,7 @@ bin_read(char *name, char **args, Options ops, UNUSED(int func))
cvsdist 92bd44
 	if (!zleactive) {
cvsdist 92bd44
 	    if (SHTTY == -1) {
cvsdist 92bd44
 		/* need to open /dev/tty specially */
cvsdist 92bd44
-		if ((SHTTY = open("/dev/tty", O_RDWR|O_NOCTTY)) != -1) {
cvsdist 92bd44
+		if ((SHTTY = block_open("/dev/tty", O_RDWR|O_NOCTTY)) != -1) {
cvsdist 92bd44
 		    haso = 1;
cvsdist 92bd44
 		    oshout = shout;
cvsdist 92bd44
 		    init_shout();
Kamil Dudka b84eb4
diff --git a/Src/init.c b/Src/init.c
Kamil Dudka b84eb4
index 102276a..238f6ed 100644
Kamil Dudka b84eb4
--- a/Src/init.c
Kamil Dudka b84eb4
+++ b/Src/init.c
Kamil Dudka b84eb4
@@ -542,7 +542,7 @@ init_io(void)
cvsdist 92bd44
     if (isatty(0)) {
cvsdist 92bd44
 	zsfree(ttystrname);
cvsdist 92bd44
 	if ((ttystrname = ztrdup(ttyname(0)))) {
cvsdist 92bd44
-	    SHTTY = movefd(open(ttystrname, O_RDWR | O_NOCTTY));
cvsdist 92bd44
+	    SHTTY = movefd(block_open(ttystrname, O_RDWR | O_NOCTTY));
cvsdist 92bd44
 #ifdef TIOCNXCL
cvsdist 92bd44
 	    /*
cvsdist 92bd44
 	     * See if the terminal claims to be busy.  If so, and fd 0
Kamil Dudka b84eb4
@@ -583,7 +583,7 @@ init_io(void)
cvsdist 92bd44
 	ttystrname = ztrdup(ttyname(1));
cvsdist 92bd44
     }
cvsdist 92bd44
     if (SHTTY == -1 &&
cvsdist 92bd44
-	(SHTTY = movefd(open("/dev/tty", O_RDWR | O_NOCTTY))) != -1) {
cvsdist 92bd44
+	(SHTTY = movefd(block_open("/dev/tty", O_RDWR | O_NOCTTY))) != -1) {
cvsdist 92bd44
 	zsfree(ttystrname);
cvsdist 92bd44
 	ttystrname = ztrdup(ttyname(SHTTY));
cvsdist 92bd44
     }
Kamil Dudka b84eb4
@@ -1707,3 +1707,33 @@ zsh_main(UNUSED(int argc), char **argv)
Kamil Dudka da97c7
 		    : "use 'logout' to logout.");
cvsdist 92bd44
     }
cvsdist 92bd44
 }
cvsdist 92bd44
+
cvsdist 92bd44
+/**/
cvsdist 92bd44
+int
cvsdist 92bd44
+block_open (const char *tty, int flags)
cvsdist 92bd44
+{
cvsdist 92bd44
+    int saved_errno;
cvsdist 92bd44
+    int fd;
cvsdist 92bd44
+
cvsdist 92bd44
+    if ((flags & O_NONBLOCK) == 0) {
cvsdist 92bd44
+	fd = open (tty, flags | O_NONBLOCK);
cvsdist 92bd44
+	if (fd == -1)
cvsdist 92bd44
+	    return fd;
cvsdist 92bd44
+	flags = fcntl(fd, F_GETFL);
cvsdist 92bd44
+	if (flags == -1)
cvsdist 92bd44
+	    goto bad;
cvsdist 92bd44
+	flags &= ~O_NONBLOCK;
cvsdist 92bd44
+	if (fcntl(fd, F_SETFL, flags) == -1)
cvsdist 92bd44
+	    goto bad;
cvsdist 92bd44
+    }
cvsdist 92bd44
+    else
cvsdist 92bd44
+	fd = open (tty, flags);
cvsdist 92bd44
+
cvsdist 92bd44
+    return fd;
cvsdist 92bd44
+
cvsdist 92bd44
+bad:
cvsdist 92bd44
+    saved_errno = errno;
cvsdist 92bd44
+    close (fd);
cvsdist 92bd44
+    errno = saved_errno;
cvsdist 92bd44
+    return -1;
cvsdist 92bd44
+}