-
Notifications
You must be signed in to change notification settings - Fork 36
/
openpty.c
45 lines (36 loc) · 802 Bytes
/
openpty.c
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
#include "includes.h"
int
openpty (int *amaster, int *aslave, char *name, struct termios *termp,
struct winsize *winp)
{
int master, slave;
char *name_slave;
master = open("/dev/ptmx", O_RDWR | O_NONBLOCK);
if (master == -1) {
TRACE(("Fail to open master"))
return -1;
}
if (grantpt(master))
goto fail;
if (unlockpt(master))
goto fail;
name_slave = ptsname(master);
TRACE(("openpty: slave name %s", name_slave))
slave = open(name_slave, O_RDWR | O_NOCTTY);
if (slave == -1)
{
goto fail;
}
if(termp)
tcsetattr(slave, TCSAFLUSH, termp);
if (winp)
ioctl (slave, TIOCSWINSZ, winp);
*amaster = master;
*aslave = slave;
if (name != NULL)
strcpy(name, name_slave);
return 0;
fail:
close (master);
return -1;
}