Skip to content

Commit

Permalink
bar: Use tcgetwinsize() from POSIX 2024 if available
Browse files Browse the repository at this point in the history
  • Loading branch information
tavianator committed Jul 28, 2024
1 parent 26f3c37 commit 7780379
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
9 changes: 9 additions & 0 deletions build/has/tcgetwinsize.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright © Tavian Barnes <[email protected]>
// SPDX-License-Identifier: 0BSD

#include <termios.h>

int main(void) {
struct winsize ws;
return tcgetwinsize(0, &ws);
}
1 change: 1 addition & 0 deletions build/header.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions src/bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>

struct bfs_bar {
int fd;
Expand All @@ -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);

Check failure on line 38 in src/bar.c

View workflow job for this annotation

GitHub Actions / OmniOS

Compiler error

implicit declaration of function 'ioctl' [-Werror=implicit-function-declaration]
# endif
if (ret != 0) {
return ret;
}

store(&bar->width, ws.ws_col, relaxed);
Expand Down

0 comments on commit 7780379

Please sign in to comment.