commit 99df364525c9a6119769f01f43c7836890f75ff1
parent e5f5e5e6e5e5515d41a5ed8d662d3181d94a08d2
Author: Louis Burda <quent.burda@gmail.com>
Date: Sun, 19 Nov 2023 02:53:47 +0100
Add usage, rename --copy to --tty
Diffstat:
M | apty.c | | | 41 | +++++++++++++++++++++++++---------------- |
1 file changed, 25 insertions(+), 16 deletions(-)
diff --git a/apty.c b/apty.c
@@ -1,3 +1,4 @@
+#include <asm-generic/ioctls.h>
#define _XOPEN_SOURCE 600
#define _DEFAULT_SOURCE
@@ -61,14 +62,11 @@ mitm(int pts)
FD_SET(0, &fds);
FD_SET(pts, &fds);
- //fprintf(stderr, "?\n");
if (select(pts + 1, &fds, 0, 0, 0) == -1) {
if (errno == EINTR) continue;
return;
}
- //fprintf(stderr, "!\n");
-
if (FD_ISSET(0, &fds)) { /* stdin -> ptty */
if (fwd(0, pts)) return;
}
@@ -79,6 +77,14 @@ mitm(int pts)
}
}
+static void
+usage(int rc)
+{
+
+ fprintf(stderr, "Usage: apty [-r ROWS] [-c COLS] [-h] [--] CMD\n");
+ exit(rc);
+}
+
int
main(int argc, char **argv)
{
@@ -86,10 +92,10 @@ main(int argc, char **argv)
struct termios termios = {0};
struct winsize winsize = {0};
int exitcode;
- int fd, master, tty;
+ int master, tty;
pid_t child;
- if (argc <= 1) errx(1, "missing args");
+ if (argc <= 1) usage(1);
winsize.ws_col = 120;
winsize.ws_row = 80;
@@ -107,22 +113,25 @@ main(int argc, char **argv)
}
for (arg = argv + 1; *arg; arg++) {
- 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 (!strcmp(*arg, "-t") || !strcmp(*arg, "--tty")) {
+ if (!isatty(STDIN_FILENO)) {
+ tty = open("/dev/tty", O_RDONLY);
+ if (tty < 0) err(1, "open /dev/tty");
+
+ if (tcgetattr(tty, &termios))
+ err(1, "tcgetattr tty");
+
+ if (ioctl(tty, TIOCGWINSZ, &winsize))
+ err(1, "tcgetwinsize tty");
+
+ close(tty);
}
- if (tcgetattr(fd, &termios))
- err(1, "tcgetattr tty");
} else if (!strcmp(*arg, "-c") || !strcmp(*arg, "--cols")) {
winsize.ws_col = atoi(*++arg);
} else if (!strcmp(*arg, "-r") || !strcmp(*arg, "--rows")) {
winsize.ws_row = atoi(*++arg);
} else if (!strcmp(*arg, "-h") || !strcmp(*arg, "--help")) {
- fprintf(stderr, "Usage: apty [-r ROWS] [-c COLS] [-h] [--] CMD\n");
- return 0;
+ usage(0);
} else if (!strcmp(*arg, "--")) {
arg++;
break;
@@ -141,7 +150,7 @@ main(int argc, char **argv)
execvp(*cmd, cmd);
err(1, "execvp %s", *cmd);
} else {
- mitm(master);
+ mitm(master);
waitpid(child, &exitcode, 0);
close(master);