Handle ctrl-C correctly in riju-pty

This commit is contained in:
Radon Rosborough 2021-08-10 07:28:10 -07:00
parent 1b654a4162
commit e34ef70358
1 changed files with 20 additions and 5 deletions

View File

@ -1,6 +1,7 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include <fcntl.h> #include <fcntl.h>
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -35,10 +36,10 @@ int main(int argc, char **argv)
char *pty_slave_name = ptsname(pty_master_fd); char *pty_slave_name = ptsname(pty_master_fd);
if (pty_slave_name == NULL) if (pty_slave_name == NULL)
die("ptsname failed"); die("ptsname failed");
pid_t pid = fork(); pid_t exec_pid = fork();
if (pid < 0) if (exec_pid < 0)
die("fork failed"); die("fork failed");
else if (pid == 0) { else if (exec_pid == 0) {
close(pty_master_fd); close(pty_master_fd);
int pty_slave_fd = open(pty_slave_name, O_RDWR); int pty_slave_fd = open(pty_slave_name, O_RDWR);
if (pty_slave_fd < 0) if (pty_slave_fd < 0)
@ -56,7 +57,7 @@ int main(int argc, char **argv)
} }
char buf[1024]; char buf[1024];
int len, len_written; int len, len_written;
pid = fork(); int pid = fork();
if (pid < 0) if (pid < 0)
die("fork failed"); die("fork failed");
else if (pid == 0) { else if (pid == 0) {
@ -75,7 +76,21 @@ int main(int argc, char **argv)
while ((len = read(STDIN_FILENO, buf, 1024)) > 0) { while ((len = read(STDIN_FILENO, buf, 1024)) > 0) {
char *ptr = buf; char *ptr = buf;
while (len > 0) { while (len > 0) {
len_written = write(pty_master_fd, ptr, len); if (*ptr == '\x03') {
if (kill(exec_pid, SIGINT) < 0)
die("kill failed");
len -= 1;
ptr += 1;
continue;
}
int limit = len;
for (int idx = 0; idx < len; ++idx) {
if (buf[idx] == '\x03') {
limit = idx;
break;
}
}
len_written = write(pty_master_fd, ptr, limit);
if (len_written < 0) if (len_written < 0)
die("write failed"); die("write failed");
len -= len_written; len -= len_written;