From c6ff39ccc4e52fb860f2bfe436c5ac1c114aadd7 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 20 Feb 2012 10:41:31 +0000 Subject: Add code to find console dimensions in WIN32 --- include/mingw.h | 2 ++ libbb/xfuncs.c | 4 ++++ win32/winansi.c | 17 +++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/include/mingw.h b/include/mingw.h index 6fe5d4017..49de6cb5e 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -152,6 +152,8 @@ int winansi_fprintf(FILE *stream, const char *format, ...) __attribute__((format #define printf(...) winansi_printf(__VA_ARGS__) #define fprintf(...) winansi_fprintf(__VA_ARGS__) +int winansi_get_terminal_width_height(struct winsize *win); + /* * stdlib.h */ diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index a02a504b0..53c48557d 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c @@ -256,7 +256,11 @@ int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *heigh win.ws_col = 0; /* I've seen ioctl returning 0, but row/col is (still?) 0. * We treat that as an error too. */ +#if !ENABLE_PLATFORM_MINGW32 err = ioctl(fd, TIOCGWINSZ, &win) != 0 || win.ws_row == 0; +#else + err = winansi_get_terminal_width_height(&win) == 0; +#endif if (height) *height = wh_helper(win.ws_row, 24, "LINES", &err); if (width) diff --git a/win32/winansi.c b/win32/winansi.c index 3276913da..7730fd006 100644 --- a/win32/winansi.c +++ b/win32/winansi.c @@ -417,3 +417,20 @@ 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; +} -- cgit v1.2.3-55-g6feb