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