Loading...
--- Libc/Libc-1725.40.4/util/pty.c
+++ Libc/Libc-391/util/pty.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2007 Apple Inc. All rights reserved.
+ * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
@@ -64,77 +64,85 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <syslog.h>
 #include <termios.h>
 #include <unistd.h>
 #include <util.h>
 
-int openpty(int *aprimary, int *areplica, char *name, struct termios *termp, struct winsize *winp)
+int openpty(amaster, aslave, name, termp, winp)
+	int *amaster, *aslave;
+	char *name;
+	struct termios *termp;
+	struct winsize *winp;
 {
-	int primary, replica;
-	char rname[__PTYNAMELEN];
-	int errno_saved;
+	static char line[] = "/dev/ptyXX";
+	register const char *cp1, *cp2;
+	register int master, slave, ttygid;
+	struct group *gr;
 
-	if ((primary = posix_openpt(O_RDWR|O_NOCTTY)) < 0)
-		return -1;
-	if (grantpt(primary) < 0 || unlockpt(primary) < 0
-	    || ptsname_r(primary, rname, sizeof(rname)) == -1
-	    || (replica = open(rname, O_RDWR|O_NOCTTY, 0)) < 0) {
-		errno_saved = errno;
-		(void) close(primary);
-		errno = errno_saved;
-		return -1;
+	if ((gr = getgrnam("tty")) != NULL)
+		ttygid = gr->gr_gid;
+	else
+		ttygid = -1;
+
+	for (cp1 = "pqrstuvwxy"; *cp1; cp1++) {
+		line[8] = *cp1;
+		for (cp2 = "0123456789abcdef"; *cp2; cp2++) {
+			line[5] = 'p';
+			line[9] = *cp2;
+			if ((master = open(line, O_RDWR, 0)) == -1) {
+				if (errno == ENOENT)
+					return (-1);	/* out of ptys */
+			} else {
+				line[5] = 't';
+				(void) grantpt(master);
+				(void) revoke(line);
+				if ((slave = open(line, O_RDWR, 0)) != -1) {
+					*amaster = master;
+					*aslave = slave;
+					if (name)
+						strcpy(name, line);
+					if (termp)
+						(void) tcsetattr(slave, 
+							TCSAFLUSH, termp);
+					if (winp)
+						(void) ioctl(slave, TIOCSWINSZ, 
+							(char *)winp);
+					return (0);
+				}
+				(void) close(master);
+			}
+		}
 	}
-	*aprimary = primary;
-	*areplica = replica;
-	if (name)
-		strlcpy(name, rname, __PTYNAMELEN);
-	if (termp)
-		(void) tcsetattr(replica, TCSAFLUSH, termp);
-	if (winp)
-		(void) ioctl(replica, TIOCSWINSZ, (char *)winp);
-	return (0);
+	errno = ENOENT;	/* out of ptys */
+	return (-1);
 }
 
 int
-forkpty(int *aprimary, char *name, struct termios *termp, struct winsize *winp)
+forkpty(amaster, name, termp, winp)
+	int *amaster;
+	char *name;
+	struct termios *termp;
+	struct winsize *winp;
 {
-	int primary, replica, pid;
-	int errno_saved;
+	int master, slave, pid;
 
-	if (openpty(&primary, &replica, name, termp, winp) == -1)
+	if (openpty(&master, &slave, name, termp, winp) == -1)
 		return (-1);
 	switch (pid = fork()) {
 	case -1:
-		errno_saved = errno;
-		(void) close(primary);
-		(void) close(replica);
-		errno = errno_saved;
 		return (-1);
 	case 0:
 		/* 
 		 * child
 		 */
-		(void) close(primary);
-		/*
-		 * 4300297: login_tty() may fail to set the controlling tty.
-		 * Since we have already forked, the best we can do is to 
-		 * dup the replica as if login_tty() succeeded.
-		 */
-		if (login_tty(replica) < 0) {
-			syslog(LOG_ERR, "forkpty: login_tty could't make controlling tty");
-			(void) dup2(replica, 0);
-			(void) dup2(replica, 1);
-			(void) dup2(replica, 2);
-			if (replica > 2)
-				(void) close(replica);
-		}
+		(void) close(master);
+		login_tty(slave);
 		return (0);
 	}
 	/*
 	 * parent
 	 */
-	*aprimary = primary;
-	(void) close(replica);
+	*amaster = master;
+	(void) close(slave);
 	return (pid);
 }