I have a program in which I use ioctl(0, TIOCGWINSZ, (struct winsize *))
to find the size of the terminal window the program is running in. When I run it in the terminal, it works fine, but when I use LLDB, ioctl
gives a window size of 0 x 0.
Example:
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdio.h>
int main(){
struct winsize tty_window_size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &tty_window_size);
printf("Rows: %i, Cols: %i\n", tty_window_size.ws_row, tty_window_size.ws_col);
return 0;
}
Terminal transcript:
$ clang test.c
$ ./a.out
Rows: 24, Cols: 80
$ lldb ./a.out
(lldb) target create "./a.out"
Current executable set to './a.out' (x86_64).
(lldb) r
Process 32763 launched: './a.out' (x86_64)
Rows: 0, Cols: 0
Process 32763 exited with status = 0 (0x00000000)
Does anybody why this happens, or a way to fix this?
Thanks in advance.
Not sure it is useful since it is an old post. Anyway... I faced the same problem and found a workaround. if ioctl on stdout fails, then try with /dev/tty
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
void getTerminalSize(int *row, int *col) {
struct winsize ws;
*row = *col = 0; /* default value (indicates an error) */
if (!isatty(STDOUT_FILENO)) {
return;
}
ws.ws_row = ws.ws_col = 0;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_row == 0 || ws.ws_col == 0) {
int fd = open("/dev/tty", O_RDONLY);
if (fd != -1) {
ioctl(fd, TIOCGWINSZ, &ws);
close (fd);
}
}
*row = ws.ws_row;
*col = ws.ws_col;
}
int main(){
int row, col;
getTerminalSize(&row, &col);
printf("Row: %i, Col: %i\n", row, col);
return 0;
}