commit fe50c328d43728d9b338e21dd1772fba74eec34b
parent a760023437c0cf376ca3b030972009c48f3a7dee
Author: Louis Burda <quent.burda@gmail.com>
Date: Sun, 19 Nov 2023 02:36:38 +0100
Add copy flag for getting tty settings
Diffstat:
M | apty.c | | | 26 | ++++++++++++++++++-------- |
1 file changed, 18 insertions(+), 8 deletions(-)
diff --git a/apty.c b/apty.c
@@ -86,7 +86,7 @@ main(int argc, char **argv)
struct termios termios = {0};
struct winsize winsize = {0};
int exitcode;
- int master, tty;
+ int fd, master, tty;
pid_t child;
if (argc <= 1) errx(1, "missing args");
@@ -95,24 +95,34 @@ main(int argc, char **argv)
winsize.ws_row = 80;
cfmakeraw(&termios);
- if (isatty(0)) {
- if (ioctl(0, TIOCGWINSZ, &winsize))
+ if (isatty(STDIN_FILENO)) {
+ if (ioctl(STDIN_FILENO, TIOCGWINSZ, &winsize))
err(1, "tcgetwinsize");
- if (tcgetattr(0, &termios))
+ if (tcgetattr(STDIN_FILENO, &termios))
err(1, "tcgettattr");
termios.c_lflag &= ~ICANON;
- if (tcsetattr(0, TCSANOW, &termios))
+ if (tcsetattr(STDIN_FILENO, TCSANOW, &termios))
err(1, "tcsetattr");
}
for (arg = argv + 1; *arg; arg++) {
- if (!strcmp(*arg, "-w") || !strcmp(*arg, "--width"))
+ if (!strcmp(*arg, "-c") || !strcmp(*arg, "--copy")) {
+ if (isatty(STDIN_FILENO)) {
+ fd = STDIN_FILENO;
+ } else {
+ fd = open("/dev/tty", O_RDONLY);
+ if (fd < 0) err(1, "open /dev/tty");
+ }
+ if (tcgetattr(fd, &termios))
+ err(1, "tcgetattr tty");
+ } else if (!strcmp(*arg, "-w") || !strcmp(*arg, "--width")) {
winsize.ws_col = atoi(*++arg);
- else if (!strcmp(*arg, "-h") || !strcmp(*arg, "--height"))
+ } else if (!strcmp(*arg, "-h") || !strcmp(*arg, "--height")) {
winsize.ws_row = atoi(*++arg);
- else
+ } else {
break;
+ }
}
if (!*arg) errx(1, "missing command");