-
Notifications
You must be signed in to change notification settings - Fork 1
/
tty2tty.c
88 lines (73 loc) · 1.5 KB
/
tty2tty.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* tty2tty.c
*
* make a pair of virtual look back tty device
*/
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/select.h>
int ptym_open(char *pts_name, char *pts_name_s, int pts_namesz) {
char *ptr;
int fdm;
strncpy(pts_name, "/dev/ptmx", pts_namesz);
pts_name[pts_namesz - 1] = '\0';
fdm = posix_openpt(O_RDWR | O_NONBLOCK);
if (fdm < 0)
return (-1);
if (grantpt(fdm) < 0) {
close(fdm);
return (-2);
}
if (unlockpt(fdm) < 0) {
close(fdm);
return (-3);
}
if ((ptr = ptsname(fdm)) == NULL) {
close(fdm);
return (-4);
}
strncpy(pts_name_s, ptr, pts_namesz);
pts_name[pts_namesz - 1] = '\0';
return (fdm);
}
int main(void) {
char master1[1024];
char slave1[1024];
char master2[1024];
char slave2[1024];
int fd1;
int fd2;
char c1, c2;
fd1 = ptym_open(master1, slave1, 1024);
fd2 = ptym_open(master2, slave2, 1024);
printf("(%s) <=> (%s)\n", slave1, slave2);
while (1) {
int ret;
fd_set input;
FD_ZERO(&input);
FD_SET(fd1, &input);
FD_SET(fd2, &input);
ret = select(fd2 + 1, &input, NULL, NULL, NULL);
if (ret < 0) {
perror("select");
break;
} else if (ret == 0) {
continue;
} else {
if (FD_ISSET(fd1, &input)) {
if (read(fd1, &c1, 1) == 1 && write(fd2, &c1, 1) == 1)
continue;
} else if (FD_ISSET(fd2, &input)) {
if (read(fd2, &c2, 1) == 1 && write(fd1, &c2, 1) == 1)
continue;
}
}
};
close(fd1);
close(fd2);
return EXIT_SUCCESS;
}