From 0c42082067500c0dae3237f918e8c80c0971f766 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Tue, 24 May 2022 12:05:56 +0200 Subject: Use /dev/tty instead of stdout to get terminal size --- center.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'center.c') diff --git a/center.c b/center.c index 1b03084..a08e9e7 100644 --- a/center.c +++ b/center.c @@ -1,12 +1,15 @@ #define _POSIX_C_SOURCE 200809L #define _XOPEN_SOURCE +#include +#include #include -#include #include +#include +#include +#include #include #include -#include -#include +#include #define ERR(str) { perror(str); err = EXIT_FAILURE; goto end; } @@ -22,9 +25,15 @@ int main() ssize_t len; int err = EXIT_SUCCESS; + int tty_fd = open("/dev/tty", O_RDWR); + if (tty_fd < 0) { + perror("open"); + return EXIT_FAILURE; + } + while ((len = getline(&buf, &siz, stdin)) > 0) { struct winsize ws; - if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) ERR("ioctl") + if (ioctl(tty_fd, TIOCGWINSZ, &ws) < 0) ERR("ioctl") int term_width = ws.ws_col; @@ -61,6 +70,8 @@ int main() } end: + close(tty_fd); + if (buf) free(buf); -- cgit v1.2.3