mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-18 12:16:31 +00:00
实现pty,附带测试程序 (#685)
* 实现pty,附带测试程序 * fmt ** clippy * 将file层的锁粒度缩小,从而不使用no_preempt。更改pipe在sleep部分的bug * 修复拼写错误
This commit is contained in:
20
user/apps/test_pty/Makefile
Normal file
20
user/apps/test_pty/Makefile
Normal file
@ -0,0 +1,20 @@
|
||||
ifeq ($(ARCH), x86_64)
|
||||
CROSS_COMPILE=x86_64-linux-musl-
|
||||
else ifeq ($(ARCH), riscv64)
|
||||
CROSS_COMPILE=riscv64-linux-musl-
|
||||
endif
|
||||
|
||||
CC=$(CROSS_COMPILE)gcc
|
||||
|
||||
|
||||
all:
|
||||
$(CC) -static -o test_pty test_pty.c
|
||||
|
||||
.PHONY: install clean
|
||||
install: all
|
||||
mv test_pty $(DADK_CURRENT_BUILD_DIR)/test_pty
|
||||
|
||||
clean:
|
||||
rm test_pty *.o
|
||||
|
||||
fmt:
|
49
user/apps/test_pty/test_pty.c
Normal file
49
user/apps/test_pty/test_pty.c
Normal file
@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <pty.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int ptm, pts;
|
||||
char name[256];
|
||||
struct termios term;
|
||||
|
||||
if (openpty(&ptm, &pts, name, NULL, NULL) == -1) {
|
||||
perror("openpty");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("slave name: %s fd: %d\n", name,pts);
|
||||
|
||||
tcgetattr(pts, &term);
|
||||
term.c_lflag &= ~(ICANON | ECHO);
|
||||
term.c_cc[VMIN] = 1;
|
||||
term.c_cc[VTIME] = 0;
|
||||
tcsetattr(pts, TCSANOW, &term);
|
||||
|
||||
printf("before print to pty slave\n");
|
||||
|
||||
dprintf(pts, "Hello world!\n");
|
||||
|
||||
char buf[256];
|
||||
ssize_t n = read(ptm, buf, sizeof(buf));
|
||||
if (n > 0) {
|
||||
printf("read %ld bytes from slave: %.*s", n, (int)n, buf);
|
||||
}
|
||||
|
||||
dprintf(ptm, "hello world from master\n");
|
||||
|
||||
char nbuf[256];
|
||||
ssize_t nn = read(pts, nbuf, sizeof(nbuf));
|
||||
if (nn > 0) {
|
||||
printf("read %ld bytes from master: %.*s", nn, (int)nn, nbuf);
|
||||
}
|
||||
|
||||
close(ptm);
|
||||
close(pts);
|
||||
|
||||
return 0;
|
||||
}
|
23
user/dadk/config/test_pty-0.1.0.dadk
Normal file
23
user/dadk/config/test_pty-0.1.0.dadk
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "test_pty",
|
||||
"version": "0.1.0",
|
||||
"description": "简单的pty测试程序",
|
||||
"task_type": {
|
||||
"BuildFromSource": {
|
||||
"Local": {
|
||||
"path": "apps/test_pty"
|
||||
}
|
||||
}
|
||||
},
|
||||
"depends": [],
|
||||
"build": {
|
||||
"build_command": "make install -j $(nproc)"
|
||||
},
|
||||
"install": {
|
||||
"in_dragonos_path": "/bin"
|
||||
},
|
||||
"clean": {
|
||||
"clean_command": "make clean"
|
||||
},
|
||||
"envs": []
|
||||
}
|
Reference in New Issue
Block a user