1 #include <stdlib.h>
2 #include <fcntl.h>
3 #include <unistd.h>
4 #include <pty.h>
5 #include <stdio.h>
6 #include <pthread.h>
7 #include <unsupported_api.h>
8
9 /* Nonstandard, but vastly superior to the standard functions */
10
openpty(int * pm,int * ps,char * name,const struct termios * tio,const struct winsize * ws)11 int openpty(int *pm, int *ps, char *name, const struct termios *tio, const struct winsize *ws)
12 {
13 int m, s, n=0, cs;
14 char buf[20];
15
16 unsupported_api(__FUNCTION__);
17 m = open("/dev/ptmx", O_RDWR|O_NOCTTY);
18 if (m < 0) return -1;
19
20 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
21
22 if (ioctl(m, TIOCSPTLCK, &n) || ioctl (m, TIOCGPTN, &n))
23 goto fail;
24
25 if (!name) name = buf;
26 snprintf(name, sizeof buf, "/dev/pts/%d", n);
27 if ((s = open(name, O_RDWR|O_NOCTTY)) < 0)
28 goto fail;
29
30 if (tio) tcsetattr(s, TCSANOW, tio);
31 if (ws) ioctl(s, TIOCSWINSZ, ws);
32
33 *pm = m;
34 *ps = s;
35
36 pthread_setcancelstate(cs, 0);
37 return 0;
38 fail:
39 close(m);
40 pthread_setcancelstate(cs, 0);
41 return -1;
42 }
43