int main int argc char argv int master slave char name char buf char m

 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
int main(int argc, char** argv) {
int master, slave;
char *name;
char *buf = (char*)malloc(4096);
master = open("/dev/ptmx",O_RDWR|O_NOCTTY|O_NONBLOCK);
if (master < 0) return 0;
if (grantpt (master) < 0 || unlockpt (master) < 0) throw 1;
name = ptsname (master);
if (name == NULL) throw 1;
fd_set readset;
timeval tv;
tv.tv_sec = 30;
tv.tv_usec = 0;
while (true){
FD_ZERO(&readset);
FD_SET(0, &readset);
FD_SET(master, &readset);
if(select(master+1, &readset, NULL, NULL, &tv) <= 0) {
continue;
}
//stdin
if(FD_ISSET(0, &readset)) {
int bytesRead = read(0,buf,4096);
write(master,buf,bytesRead);
}
//pty
if(FD_ISSET(master, &readset)) {
int bytesRead;
while (bytesRead = read(master,buf,4096) != -1){
write(1,buf,bytesRead);
}
int i = errno;
}
}
return (EXIT_SUCCESS);
}