| 174 | // ************************************************************************** |
| 175 | |
| 176 | int STTY::findTTY() |
| 177 | { |
| 178 | int ptyfd = -1; |
| 179 | bool needGrantPty = true; |
| 180 | #ifndef Q_OS_WIN |
| 181 | // Find a master pty that we can open //////////////////////////////// |
| 182 | |
| 183 | #ifdef __sgi__ |
| 184 | ptyfd = open("/dev/ptmx",O_RDWR); |
| 185 | #elif defined(Q_OS_MAC) || defined(Q_OS_FREEBSD) |
| 186 | ptyfd = posix_openpt(O_RDWR); |
| 187 | #endif |
| 188 | #if defined(__sgi__) || defined(Q_OS_MAC) || defined(Q_OS_FREEBSD) |
| 189 | if (ptyfd == -1) { |
| 190 | perror("Can't open a pseudo teletype"); |
| 191 | return(-1); |
| 192 | } else if (ptyfd >= 0) { |
| 193 | strncpy(tty_slave, ptsname(ptyfd), 50); |
| 194 | grantpt(ptyfd); |
| 195 | unlockpt(ptyfd); |
| 196 | needGrantPty = false; |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | // first we try UNIX PTY's |
| 201 | #if defined(TIOCGPTN) && !defined(Q_OS_FREEBSD) |
| 202 | strcpy(pty_master,"/dev/ptmx"); |
| 203 | strcpy(tty_slave,"/dev/pts/"); |
| 204 | ptyfd = open(pty_master,O_RDWR); |
| 205 | if (ptyfd >= 0) { // got the master pty |
| 206 | int ptyno; |
| 207 | if (ioctl(ptyfd, TIOCGPTN, &ptyno) == 0) { |
| 208 | struct stat sbuf; |
| 209 | sprintf(tty_slave,"/dev/pts/%d",ptyno); |
| 210 | if (stat(tty_slave,&sbuf) == 0 && S_ISCHR(sbuf.st_mode)) |
| 211 | needGrantPty = false; |
| 212 | else { |
| 213 | close(ptyfd); |
| 214 | ptyfd = -1; |
| 215 | } |
| 216 | } else { |
| 217 | close(ptyfd); |
| 218 | ptyfd = -1; |
| 219 | } |
| 220 | } |
| 221 | #endif |
| 222 | |
| 223 | #if defined(_SCO_DS) || defined(__USLC__) /* SCO OSr5 and UnixWare */ |
| 224 | if (ptyfd < 0) { |
| 225 | for (int idx = 0; idx < 256; idx++) |
| 226 | { sprintf(pty_master, "/dev/ptyp%d", idx); |
| 227 | sprintf(tty_slave, "/dev/ttyp%d", idx); |
| 228 | if (access(tty_slave, F_OK) < 0) { idx = 256; break; } |
| 229 | if ((ptyfd = open (pty_master, O_RDWR)) >= 0) |
| 230 | { if (access (tty_slave, R_OK|W_OK) == 0) break; |
| 231 | close(ptyfd); ptyfd = -1; |
| 232 | } |
| 233 | } |