1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
| | #include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <pty.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
/* 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;
}
|