fix(utils): silence terminal-size ioctl when stdout isn't a tty
- SHA
46a7e54908b31f489056837feca716fdd3b9dcb7- Parents
-
b9c10a4 - Tree
aa91041
46a7e54
46a7e54908b31f489056837feca716fdd3b9dcb7b9c10a4
aa91041| Status | File | + | - |
|---|---|---|---|
| M |
src/utils.c
|
11 | 4 |
src/utils.cmodified@@ -859,20 +859,27 @@ bool is_terminal(int fd) { | ||
| 859 | 859 | |
| 860 | 860 | int get_terminal_size(int *width, int *height) { |
| 861 | 861 | struct winsize ws; |
| 862 | - | |
| 862 | + | |
| 863 | 863 | if (!width || !height) { |
| 864 | 864 | set_error(ERR_INVALID_ARGS, "NULL arguments to get_terminal_size"); |
| 865 | 865 | return -1; |
| 866 | 866 | } |
| 867 | - | |
| 867 | + | |
| 868 | + /* Skip the ioctl when stdout isn't a terminal (piped, redirected, | |
| 869 | + * command-substituted). Return failure silently so callers fall back to | |
| 870 | + * their default width without spamming stderr on every invocation. */ | |
| 871 | + if (!isatty(STDOUT_FILENO)) { | |
| 872 | + return -1; | |
| 873 | + } | |
| 874 | + | |
| 868 | 875 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1) { |
| 869 | 876 | set_system_error(ERR_SYSTEM_CALL, "Failed to get terminal size"); |
| 870 | 877 | return -1; |
| 871 | 878 | } |
| 872 | - | |
| 879 | + | |
| 873 | 880 | *width = ws.ws_col; |
| 874 | 881 | *height = ws.ws_row; |
| 875 | - | |
| 882 | + | |
| 876 | 883 | return 0; |
| 877 | 884 | } |
| 878 | 885 | |