mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-10 13:56:48 +00:00
Add pty test
This commit is contained in:
parent
25c4f0f2bc
commit
a042da1847
@ -3,7 +3,7 @@ MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
|
||||
CUR_DIR := $(patsubst %/,%,$(dir $(MKFILE_PATH)))
|
||||
INITRAMFS ?= $(CUR_DIR)/../build/initramfs
|
||||
REGRESSION_BUILD_DIR ?= $(INITRAMFS)/regression
|
||||
TEST_APPS := signal_c pthread network hello_world hello_pie hello_c fork_c fork execve
|
||||
TEST_APPS := signal_c pthread network hello_world hello_pie hello_c fork_c fork execve pty
|
||||
|
||||
.PHONY: all
|
||||
|
||||
|
3
regression/apps/pty/Makefile
Normal file
3
regression/apps/pty/Makefile
Normal file
@ -0,0 +1,3 @@
|
||||
include ../test_common.mk
|
||||
|
||||
EXTRA_C_FLAGS :=
|
53
regression/apps/pty/open_pty.c
Normal file
53
regression/apps/pty/open_pty.c
Normal file
@ -0,0 +1,53 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <pty.h>
|
||||
|
||||
int main() {
|
||||
int master, slave;
|
||||
char name[256];
|
||||
struct termios term;
|
||||
|
||||
if (openpty(&master, &slave, name, NULL, NULL) == -1) {
|
||||
perror("openpty");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("master fd: %d\n", master);
|
||||
printf("slave fd: %d\n", slave);
|
||||
printf("slave name: %s\n", name);
|
||||
|
||||
// Set pty slave terminal attributes
|
||||
tcgetattr(slave, &term);
|
||||
term.c_lflag &= ~(ICANON | ECHO);
|
||||
term.c_cc[VMIN] = 1;
|
||||
term.c_cc[VTIME] = 0;
|
||||
tcsetattr(slave, TCSANOW, &term);
|
||||
|
||||
// Print to pty slave
|
||||
dprintf(slave, "Hello world!\n");
|
||||
|
||||
// Read from pty slave
|
||||
char buf[256];
|
||||
ssize_t n = read(master, buf, sizeof(buf));
|
||||
if (n > 0) {
|
||||
printf("read %ld bytes from slave: %.*s\n", n, (int)n, buf);
|
||||
}
|
||||
|
||||
// Write to pty master
|
||||
dprintf(master, "hello world from master\n");
|
||||
|
||||
// Read from pty master
|
||||
char nbuf[256];
|
||||
ssize_t nn = read(slave, nbuf, sizeof(nbuf));
|
||||
if (nn > 0) {
|
||||
printf("read %ld bytes from master: %.*s\n", nn, (int)nn, nbuf);
|
||||
}
|
||||
|
||||
close(master);
|
||||
close(slave);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user