#include #include #include #include #include #include #include /* Nonstandard, but vastly superior to the standard functions */ int openpty(int *pm, int *ps, char *name, const struct termios *tio, const struct winsize *ws) { int m, s = -1, cs; char buf[20]; m = open("/dev/ptmx", O_RDWR|O_NOCTTY); if (m < 0) return -1; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); if (!name) name = buf; #ifdef TIOCGPTPEER int ret; /* Try to allocate slave fd solely based on the master fd in case the * kernel supports it. */ s = ioctl(m, TIOCGPTPEER, O_RDWR|O_NOCTTY); if (s < 0) goto fail; /* Check the contents of the symlink in case devpts has been mounted in * a non-standard location. */ ret = ttyname_r(s, name, sizeof buf); if (ret) goto fail; if (strncmp(name, "/dev/pts/", 9)) goto fail; #else int n = 0; if (ioctl(m, TIOCSPTLCK, &n) || ioctl (m, TIOCGPTN, &n)) goto fail; snprintf(name, sizeof buf, "/dev/pts/%d", n); s = open(name, O_RDWR|O_NOCTTY); if (s < 0) goto fail; #endif if (tio) tcsetattr(s, TCSANOW, tio); if (ws) ioctl(s, TIOCSWINSZ, ws); *pm = m; *ps = s; pthread_setcancelstate(cs, 0); return 0; fail: close(m); if (s >= 0) close(s); pthread_setcancelstate(cs, 0); return -1; }