- 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) chown(line, getuid(), ttygid);
- (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
- (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);
- }
- }
+ if ((master = posix_openpt(O_RDWR|O_NOCTTY)) < 0)
+ return -1;
+ if (grantpt(master) < 0 || unlockpt(master) < 0
+ || ptsname_r(master, sname, sizeof(sname)) == -1
+ || (slave = open(sname, O_RDWR|O_NOCTTY, 0)) < 0) {
+ (void) close(master);
+ return -1;