| 1 | #include <sys/ioctl.h> |
| 2 | #include <unistd.h> |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | // C wrapper for getting terminal size |
| 6 | // Returns 0 on success, -1 on failure |
| 7 | int get_term_size_c(int *rows, int *cols) { |
| 8 | struct winsize ws; |
| 9 | int ret; |
| 10 | |
| 11 | // Try stdin first (most reliable for interactive programs) |
| 12 | ret = ioctl(STDIN_FILENO, TIOCGWINSZ, &ws); |
| 13 | |
| 14 | if (ret == 0 && ws.ws_row > 0 && ws.ws_col > 0) { |
| 15 | *rows = ws.ws_row; |
| 16 | *cols = ws.ws_col; |
| 17 | return 0; |
| 18 | } |
| 19 | |
| 20 | // Fallback |
| 21 | *rows = 24; |
| 22 | *cols = 80; |
| 23 | return -1; |
| 24 | } |