pty示例代码

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
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pty.h>

int main()
{
int fd_m, fd_s;
int len;
const char *pts_name;
char send_buf[64] = "abc\ndefghijk\nlmn";
char recv_buf[64] = {0};

fd_m = open("/dev/ptmx", O_RDWR | O_NOCTTY);
if (fd_m < 0) {
printf("open /dev/ptmx fail1\n");
return -1;
}

if (grantpt(fd_m) < 0 || unlockpt(fd_m) < 0) {
printf("grantpt and unlockpt fail\n");
goto err;
}

pts_name = (const char *)ptsname(fd_m);
fd_s = open(pts_name, O_RDONLY | O_NOCTTY);
if (fd_s < 0) {
printf("open /dev/ptmx fail2\n");
goto err;
}

len = write(fd_m, send_buf, strlen(send_buf));
printf("write len=%d\n", len);

len = read(fd_s, recv_buf, sizeof(recv_buf));
printf("read len=%d, recv_buf=[%s]\n", len, recv_buf);

len = read(fd_s, recv_buf, sizeof(recv_buf));
printf("read len=%d, recv_buf=[%s]\n", len, recv_buf);

close(fd_m);
close(fd_s);
return 0;

err:
if (fd_m)
close(fd_m);
if (fd_s)
close(fd_s);

return -1;
}

pty示例代码
http://blog.uanet.cn/DEVELOP/pty示例代码.html
作者
dnsnat
发布于
2022年5月30日
许可协议