xref: /DragonOS/user/apps/test_pty/test_pty.c (revision dfe53cf087ef4c7b6db63d992906b062dc63e93f)
1*dfe53cf0SGnoCiYeH #include <stdio.h>
2*dfe53cf0SGnoCiYeH #include <stdlib.h>
3*dfe53cf0SGnoCiYeH #include <unistd.h>
4*dfe53cf0SGnoCiYeH #include <fcntl.h>
5*dfe53cf0SGnoCiYeH #include <termios.h>
6*dfe53cf0SGnoCiYeH #include <pty.h>
7*dfe53cf0SGnoCiYeH 
main()8*dfe53cf0SGnoCiYeH int main()
9*dfe53cf0SGnoCiYeH {
10*dfe53cf0SGnoCiYeH 	int ptm, pts;
11*dfe53cf0SGnoCiYeH 	char name[256];
12*dfe53cf0SGnoCiYeH 	struct termios term;
13*dfe53cf0SGnoCiYeH 
14*dfe53cf0SGnoCiYeH 	if (openpty(&ptm, &pts, name, NULL, NULL) == -1) {
15*dfe53cf0SGnoCiYeH 		perror("openpty");
16*dfe53cf0SGnoCiYeH 		exit(EXIT_FAILURE);
17*dfe53cf0SGnoCiYeH 	}
18*dfe53cf0SGnoCiYeH 
19*dfe53cf0SGnoCiYeH 	printf("slave name: %s fd: %d\n", name,pts);
20*dfe53cf0SGnoCiYeH 
21*dfe53cf0SGnoCiYeH 	tcgetattr(pts, &term);
22*dfe53cf0SGnoCiYeH 	term.c_lflag &= ~(ICANON | ECHO);
23*dfe53cf0SGnoCiYeH 	term.c_cc[VMIN] = 1;
24*dfe53cf0SGnoCiYeH 	term.c_cc[VTIME] = 0;
25*dfe53cf0SGnoCiYeH 	tcsetattr(pts, TCSANOW, &term);
26*dfe53cf0SGnoCiYeH 
27*dfe53cf0SGnoCiYeH 	printf("before print to pty slave\n");
28*dfe53cf0SGnoCiYeH 
29*dfe53cf0SGnoCiYeH 	dprintf(pts, "Hello world!\n");
30*dfe53cf0SGnoCiYeH 
31*dfe53cf0SGnoCiYeH 	char buf[256];
32*dfe53cf0SGnoCiYeH 	ssize_t n = read(ptm, buf, sizeof(buf));
33*dfe53cf0SGnoCiYeH 	if (n > 0) {
34*dfe53cf0SGnoCiYeH 		printf("read %ld bytes from slave: %.*s", n, (int)n, buf);
35*dfe53cf0SGnoCiYeH 	}
36*dfe53cf0SGnoCiYeH 
37*dfe53cf0SGnoCiYeH 	dprintf(ptm, "hello world from master\n");
38*dfe53cf0SGnoCiYeH 
39*dfe53cf0SGnoCiYeH 	char nbuf[256];
40*dfe53cf0SGnoCiYeH 	ssize_t nn = read(pts, nbuf, sizeof(nbuf));
41*dfe53cf0SGnoCiYeH 	if (nn > 0) {
42*dfe53cf0SGnoCiYeH 		printf("read %ld bytes from master: %.*s", nn, (int)nn, nbuf);
43*dfe53cf0SGnoCiYeH 	}
44*dfe53cf0SGnoCiYeH 
45*dfe53cf0SGnoCiYeH 	close(ptm);
46*dfe53cf0SGnoCiYeH 	close(pts);
47*dfe53cf0SGnoCiYeH 
48*dfe53cf0SGnoCiYeH 	return 0;
49*dfe53cf0SGnoCiYeH }