aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2018-04-03 11:13:10 +0100
committerRon Yorston <rmy@pobox.com>2018-04-03 12:29:01 +0100
commit32f8569a99b948e810266608dd6e3bf9de611df5 (patch)
treeec891c62ed84879a99de960365cc4ba935e02c90
parent4d8668e4b6bac0d7902dd677d540c700817b094b (diff)
downloadbusybox-w32-32f8569a99b948e810266608dd6e3bf9de611df5.tar.gz
busybox-w32-32f8569a99b948e810266608dd6e3bf9de611df5.tar.bz2
busybox-w32-32f8569a99b948e810266608dd6e3bf9de611df5.zip
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).
-rw-r--r--include/mingw.h2
-rw-r--r--libbb/xfuncs.c2
-rw-r--r--win32/ioctl.c24
-rw-r--r--win32/winansi.c17
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);
147#define read winansi_read 147#define read winansi_read
148#define getc winansi_getc 148#define getc winansi_getc
149 149
150int winansi_get_terminal_width_height(struct winsize *win);
151
152/* 150/*
153 * stdlib.h 151 * stdlib.h
154 */ 152 */
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
268 int err; 268 int err;
269 int close_me = -1; 269 int close_me = -1;
270 270
271#if !ENABLE_PLATFORM_MINGW32
271 if (fd == -1) { 272 if (fd == -1) {
272 if (isatty(STDOUT_FILENO)) 273 if (isatty(STDOUT_FILENO))
273 fd = STDOUT_FILENO; 274 fd = STDOUT_FILENO;
@@ -280,6 +281,7 @@ int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *heigh
280 else 281 else
281 close_me = fd = open("/dev/tty", O_RDONLY); 282 close_me = fd = open("/dev/tty", O_RDONLY);
282 } 283 }
284#endif
283 285
284 win.ws_row = 0; 286 win.ws_row = 0;
285 win.ws_col = 0; 287 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 @@
1#include "libbb.h" 1#include "libbb.h"
2 2
3static int mingw_get_terminal_width_height(struct winsize *win)
4{
5 int fd;
6 HANDLE handle;
7 CONSOLE_SCREEN_BUFFER_INFO sbi;
8
9 win->ws_row = 0;
10 win->ws_col = 0;
11
12 for (fd=STDOUT_FILENO; fd<=STDERR_FILENO; ++fd) {
13 handle = (HANDLE)_get_osfhandle(fd);
14 if (handle != INVALID_HANDLE_VALUE &&
15 GetConsoleScreenBufferInfo(handle, &sbi) != 0) {
16 win->ws_row = sbi.srWindow.Bottom - sbi.srWindow.Top + 1;
17 win->ws_col = sbi.srWindow.Right - sbi.srWindow.Left + 1;
18 return 0;
19 }
20 }
21
22 return -1;
23}
24
3int ioctl(int fd UNUSED_PARAM, int code, ...) 25int ioctl(int fd UNUSED_PARAM, int code, ...)
4{ 26{
5 va_list ap; 27 va_list ap;
@@ -11,7 +33,7 @@ int ioctl(int fd UNUSED_PARAM, int code, ...)
11 switch (code) { 33 switch (code) {
12 case TIOCGWINSZ: 34 case TIOCGWINSZ:
13 arg = va_arg(ap, void *); 35 arg = va_arg(ap, void *);
14 ret = winansi_get_terminal_width_height((struct winsize *)arg); 36 ret = mingw_get_terminal_width_height((struct winsize *)arg);
15 break; 37 break;
16 default: 38 default:
17 ret = -1; 39 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, ...)
596 return rv; 596 return rv;
597} 597}
598 598
599int winansi_get_terminal_width_height(struct winsize *win)
600{
601 BOOL ret;
602 CONSOLE_SCREEN_BUFFER_INFO sbi;
603
604 init();
605
606 win->ws_row = 0;
607 win->ws_col = 0;
608 if ((ret=GetConsoleScreenBufferInfo(console, &sbi)) != 0) {
609 win->ws_row = sbi.srWindow.Bottom - sbi.srWindow.Top + 1;
610 win->ws_col = sbi.srWindow.Right - sbi.srWindow.Left + 1;
611 }
612
613 return ret ? 0 : -1;
614}
615
616static int ansi_emulate_write(int fd, const void *buf, size_t count) 599static int ansi_emulate_write(int fd, const void *buf, size_t count)
617{ 600{
618 int rv = 0, i; 601 int rv = 0, i;