Add -f flag to riju-pty

This commit is contained in:
Radon Rosborough 2021-08-12 19:58:16 -07:00
parent ac79035580
commit b3d4f7cf9c
1 changed files with 47 additions and 32 deletions

View File

@ -18,7 +18,7 @@ void __attribute__((noreturn)) die(char *msg)
exit(1); exit(1);
} }
void die_with_usage() { die("usage: riju-pty CMDLINE..."); } void die_with_usage() { die("usage: riju-pty [-f] CMDLINE..."); }
struct termios orig_termios; struct termios orig_termios;
@ -30,18 +30,29 @@ void restore_tty()
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if (argc <= 1) argc -= 1;
argv += 1;
int no_pty = argc >= 1 && strcmp(argv[0], "-f") == 0;
if (no_pty) {
argc -= 1;
argv += 1;
}
if (argc <= 0)
die_with_usage(); die_with_usage();
int pty_master_fd = posix_openpt(O_RDWR); int pty_master_fd;
if (pty_master_fd < 0) char *pty_slave_name;
die("posix_openpt failed"); if (!no_pty) {
if (grantpt(pty_master_fd) < 0) pty_master_fd = posix_openpt(O_RDWR);
die("grantpt failed"); if (pty_master_fd < 0)
if (unlockpt(pty_master_fd) < 0) die("posix_openpt failed");
die("unlockpt failed"); if (grantpt(pty_master_fd) < 0)
char *pty_slave_name = ptsname(pty_master_fd); die("grantpt failed");
if (pty_slave_name == NULL) if (unlockpt(pty_master_fd) < 0)
die("ptsname failed"); die("unlockpt failed");
pty_slave_name = ptsname(pty_master_fd);
if (pty_slave_name == NULL)
die("ptsname failed");
}
if (isatty(STDIN_FILENO)) { if (isatty(STDIN_FILENO)) {
if (tcgetattr(STDIN_FILENO, &orig_termios) < 0) if (tcgetattr(STDIN_FILENO, &orig_termios) < 0)
die("tcgetattr failed"); die("tcgetattr failed");
@ -63,24 +74,26 @@ int main(int argc, char **argv)
if (exec_pid < 0) if (exec_pid < 0)
die("fork failed"); die("fork failed");
else if (exec_pid == 0) { else if (exec_pid == 0) {
close(pty_master_fd); if (!no_pty) {
if (setsid() < 0) close(pty_master_fd);
die("setsid failed"); if (setsid() < 0)
int pty_slave_fd = open(pty_slave_name, O_RDWR); die("setsid failed");
if (pty_slave_fd < 0) int pty_slave_fd = open(pty_slave_name, O_RDWR);
die("open failed"); if (pty_slave_fd < 0)
if (dup2(pty_slave_fd, STDIN_FILENO) < 0) die("open failed");
die("dup2 failed"); if (dup2(pty_slave_fd, STDIN_FILENO) < 0)
if (dup2(pty_slave_fd, STDOUT_FILENO) < 0) die("dup2 failed");
die("dup2 failed"); if (dup2(pty_slave_fd, STDOUT_FILENO) < 0)
if (dup2(pty_slave_fd, STDERR_FILENO) < 0) die("dup2 failed");
die("dup2 failed"); if (dup2(pty_slave_fd, STDERR_FILENO) < 0)
if (close(pty_slave_fd) < 0) die("dup2 failed");
die("close failed"); if (close(pty_slave_fd) < 0)
execvp(argv[1], &argv[1]); die("close failed");
}
execvp(argv[0], &argv[0]);
die("execvp failed"); die("execvp failed");
} }
int pid = fork(); int pid = no_pty ? 1 : fork();
if (pid < 0) if (pid < 0)
die("fork failed"); die("fork failed");
else if (pid > 0) { else if (pid > 0) {
@ -124,8 +137,9 @@ int main(int argc, char **argv)
ptr += len_written; ptr += len_written;
} }
} }
if (len < 0) if (len < 0) {
die("read failed"); // ignore read failure
}
} else { } else {
if (setvbuf(stdout, NULL, _IONBF, 0) != 0) if (setvbuf(stdout, NULL, _IONBF, 0) != 0)
die("setvbuf failed"); die("setvbuf failed");
@ -136,8 +150,9 @@ int main(int argc, char **argv)
if (feof(stdout)) if (feof(stdout))
break; break;
} }
if (len < 0) if (len < 0) {
die("read failed"); // ignore read failure
}
} }
return 0; return 0;
} }