From 7780379b27b868fe240d7ec1ceb6902280029731 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Sun, 28 Jul 2024 11:01:58 -0400 Subject: bar: Use tcgetwinsize() from POSIX 2024 if available --- build/has/tcgetwinsize.c | 9 +++++++++ build/header.mk | 1 + src/bar.c | 13 ++++++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 build/has/tcgetwinsize.c diff --git a/build/has/tcgetwinsize.c b/build/has/tcgetwinsize.c new file mode 100644 index 0000000..d25d12b --- /dev/null +++ b/build/has/tcgetwinsize.c @@ -0,0 +1,9 @@ +// Copyright © Tavian Barnes +// SPDX-License-Identifier: 0BSD + +#include + +int main(void) { + struct winsize ws; + return tcgetwinsize(0, &ws); +} diff --git a/build/header.mk b/build/header.mk index a4f392e..8dc7a56 100644 --- a/build/header.mk +++ b/build/header.mk @@ -51,6 +51,7 @@ HEADERS := \ gen/has/strerror-r-posix.h \ gen/has/string-to-flags.h \ gen/has/strtofflags.h \ + gen/has/tcgetwinsize.h \ gen/has/timegm.h \ gen/has/tm-gmtoff.h \ gen/has/uselocale.h diff --git a/src/bar.c b/src/bar.c index 64706a8..d1fd6c1 100644 --- a/src/bar.c +++ b/src/bar.c @@ -16,6 +16,7 @@ #include #include #include +#include struct bfs_bar { int fd; @@ -28,10 +29,16 @@ struct bfs_bar { /** Get the terminal size, if possible. */ static int bfs_bar_getsize(struct bfs_bar *bar) { -#ifdef TIOCGWINSZ +#if BFS_HAS_TCGETWINSIZE || defined(TIOCGWINSZ) struct winsize ws; - if (ioctl(bar->fd, TIOCGWINSZ, &ws) != 0) { - return -1; + +# if BFS_HAS_TCGETWINSIZE + int ret = tcgetwinsize(bar->fd, &ws); +# else + int ret = ioctl(bar->fd, TIOCGWINSZ, &ws); +# endif + if (ret != 0) { + return ret; } store(&bar->width, ws.ws_col, relaxed); -- cgit v1.2.3