Add pty test

This commit is contained in:
Jianfeng Jiang 2023-08-01 14:18:23 +08:00 committed by Tate, Hongliang Tian
parent 25c4f0f2bc
commit a042da1847
3 changed files with 57 additions and 1 deletions

View File

@ -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

View File

@ -0,0 +1,3 @@
include ../test_common.mk
EXTRA_C_FLAGS :=

View 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;
}