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