From 32f8569a99b948e810266608dd6e3bf9de611df5 Mon Sep 17 00:00:00 2001 From: Ron Yorston <rmy@pobox.com> 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). --- include/mingw.h | 2 -- libbb/xfuncs.c | 2 ++ win32/ioctl.c | 24 +++++++++++++++++++++++- win32/winansi.c | 17 ----------------- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/include/mingw.h b/include/mingw.h index 2dba6c402..652652c3d 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -147,8 +147,6 @@ int winansi_getc(FILE *stream); #define read winansi_read #define getc winansi_getc -int winansi_get_terminal_width_height(struct winsize *win); - /* * stdlib.h */ diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index e8c027f17..f2112aec9 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -268,6 +268,7 @@ int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *heigh int err; int close_me = -1; +#if !ENABLE_PLATFORM_MINGW32 if (fd == -1) { if (isatty(STDOUT_FILENO)) fd = STDOUT_FILENO; @@ -280,6 +281,7 @@ int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *heigh else close_me = fd = open("/dev/tty", O_RDONLY); } +#endif win.ws_row = 0; win.ws_col = 0; 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