Reputation: 4200
I've been trying to write a program and for said program I need the size of the terminal window; however I am having some trouble, here is what is happening. I'm using a function to get the size of the terminal with ioctl
and it returns a winsize struct but the returned struct looks like:
(ws_row = 0, ws_col = 0, ws_xpixel = 0, ws_ypixel = 0)
However, when I do something like:
printf("Screen Width: %d", win.ws_col);
It does successfully print out the terminal size. Even though the values are 0s. My code for getting the struct is as follows:
/*
* Creates a winsize struct with the screen dimensions
*/
struct winsize get_screen_dimensions()
{
struct winsize wbuf;
if (ioctl(0, TIOCGWINSZ, &wbuf) != -1)
{
return wbuf;
}
return wbuf;
}
The usage looks like the following when printing the value, keep in mind printing does print the right value however in lldb, the values are all 0s:
struct winsize win = get_screen_dimensions();
printf("Screen Width: %d", win.ws_col);
Other usage, looks like the following:
struct winsize winset = get_screen_dimensions();
unsigned short scrnwdth = winset.ws_col;
In this case, in lldb, scrnwdth is 0 as well. I am truly stumped... The OS that I am running is Mac OS X El Capitan 10.11. I tried it in Debian, and get the same results.
Upvotes: 1
Views: 1203
Reputation: 54563
Whether the ioctl fails or not, your sample code returns the structure wbuf
. It will fail (for example) if your program is reading from a pipe or some other non-tty input.
Whether the wbuf
is cleared on return is implementation specific. If no specific behavior is mentioned in the documentation, when handling an error, the system is free to (a) clear the values, (b) not modify the values or (c) set them to interesting/useful values.
Upvotes: 1