Skip to content

Commit c1d7a16

Browse files
committed
fix(stdlib_system): add check for non-terminal stdout in get_terminal_size
1 parent 35ada19 commit c1d7a16

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/stdlib_system_get_terminal_size.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
#include <errno.h>
99
#endif
1010

11+
12+
// Get terminal size
13+
// @param[out] columns Pointer to store terminal width
14+
// @param[out] lines Pointer to store terminal height
15+
// @param[out] stat Pointer to store error code
16+
// (0: Success, otherwise: Error, Windows: GetLastError(), Unix: ENOTTY/errno)
1117
void get_terminal_size(int *columns, int *lines, int *stat)
1218
{
1319
/* Initialize outputs to error state */
@@ -37,6 +43,12 @@ void get_terminal_size(int *columns, int *lines, int *stat)
3743

3844
#else
3945
/* Unix implementation using termios ioctl */
46+
if (!isatty(STDOUT_FILENO))
47+
{
48+
*stat = ENOTTY;
49+
return;
50+
}
51+
4052
struct winsize w;
4153
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1)
4254
{

0 commit comments

Comments
 (0)