From 32f8569a99b948e810266608dd6e3bf9de611df5 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 3 Apr 2018 11:13:10 +0100 Subject: win32: improvements to get_terminal_width_height - move winansi_get_terminal_width_height from winansi.c to ioctl.c, the only caller; - check both stdout and stderr for a connection to a console; - omit unnecessary code in get_terminal_width_height (because the WIN32 implementation ignores the file descriptor). --- win32/ioctl.c | 24 +++++++++++++++++++++++- win32/winansi.c | 17 ----------------- 2 files changed, 23 insertions(+), 18 deletions(-) (limited to 'win32') diff --git a/win32/ioctl.c b/win32/ioctl.c index 73ceeedec..93f9f504d 100644 --- a/win32/ioctl.c +++ b/win32/ioctl.c @@ -1,5 +1,27 @@ #include "libbb.h" +static int mingw_get_terminal_width_height(struct winsize *win) +{ + int fd; + HANDLE handle; + CONSOLE_SCREEN_BUFFER_INFO sbi; + + win->ws_row = 0; + win->ws_col = 0; + + for (fd=STDOUT_FILENO; fd<=STDERR_FILENO; ++fd) { + handle = (HANDLE)_get_osfhandle(fd); + if (handle != INVALID_HANDLE_VALUE && + GetConsoleScreenBufferInfo(handle, &sbi) != 0) { + win->ws_row = sbi.srWindow.Bottom - sbi.srWindow.Top + 1; + win->ws_col = sbi.srWindow.Right - sbi.srWindow.Left + 1; + return 0; + } + } + + return -1; +} + int ioctl(int fd UNUSED_PARAM, int code, ...) { va_list ap; @@ -11,7 +33,7 @@ int ioctl(int fd UNUSED_PARAM, int code, ...) switch (code) { case TIOCGWINSZ: arg = va_arg(ap, void *); - ret = winansi_get_terminal_width_height((struct winsize *)arg); + ret = mingw_get_terminal_width_height((struct winsize *)arg); break; default: ret = -1; diff --git a/win32/winansi.c b/win32/winansi.c index ad6016ddc..a8f5dba79 100644 --- a/win32/winansi.c +++ b/win32/winansi.c @@ -596,23 +596,6 @@ int winansi_printf(const char *format, ...) return rv; } -int winansi_get_terminal_width_height(struct winsize *win) -{ - BOOL ret; - CONSOLE_SCREEN_BUFFER_INFO sbi; - - init(); - - win->ws_row = 0; - win->ws_col = 0; - if ((ret=GetConsoleScreenBufferInfo(console, &sbi)) != 0) { - win->ws_row = sbi.srWindow.Bottom - sbi.srWindow.Top + 1; - win->ws_col = sbi.srWindow.Right - sbi.srWindow.Left + 1; - } - - return ret ? 0 : -1; -} - static int ansi_emulate_write(int fd, const void *buf, size_t count) { int rv = 0, i; -- cgit v1.2.3-55-g6feb