tenseleyflow/gitswitch / 46a7e54

Browse files

fix(utils): silence terminal-size ioctl when stdout isn't a tty

Authored by espadonne
SHA
46a7e54908b31f489056837feca716fdd3b9dcb7
Parents
b9c10a4
Tree
aa91041

1 changed file

StatusFile+-
M src/utils.c 11 4
src/utils.cmodified
@@ -859,20 +859,27 @@ bool is_terminal(int fd) {
859
 
859
 
860
 int get_terminal_size(int *width, int *height) {
860
 int get_terminal_size(int *width, int *height) {
861
     struct winsize ws;
861
     struct winsize ws;
862
-    
862
+
863
     if (!width || !height) {
863
     if (!width || !height) {
864
         set_error(ERR_INVALID_ARGS, "NULL arguments to get_terminal_size");
864
         set_error(ERR_INVALID_ARGS, "NULL arguments to get_terminal_size");
865
         return -1;
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
     if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1) {
875
     if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1) {
869
         set_system_error(ERR_SYSTEM_CALL, "Failed to get terminal size");
876
         set_system_error(ERR_SYSTEM_CALL, "Failed to get terminal size");
870
         return -1;
877
         return -1;
871
     }
878
     }
872
-    
879
+
873
     *width = ws.ws_col;
880
     *width = ws.ws_col;
874
     *height = ws.ws_row;
881
     *height = ws.ws_row;
875
-    
882
+
876
     return 0;
883
     return 0;
877
 }
884
 }
878
 
885