ncurses gets the window size by three methods:
- in terminals that support the TIOCGWINSZ
ioctl(2)
, by a call to get the window dimensions at initscr(3)
call.
- through the info provided by the terminfo database. Many terminal types have a fixed size dimensions and the appropiate values for lines and columns is obtained from this database.
- by getting the value of the LINES and COLUMNS environment variables.
Well, bash(1)
normally adjusts those environment variable values to correct ones when a program exits and prior to calling the next, if this is available to bash(1)
, but if your program doesn't support the SIGWINCH
signal (or does not handle it), most probably the ncurses library won't know that the windows size has changed during program run (by default I have not seen ncurses detecting this, anyway). Even in the case you run on a system that supports it, if you don't install a signal handler for that signal, ncurses library cannot afford by itself the window dimensions change, as it has to resize the stdscr
contents and several criteria can be used to deal with old contents and new free space.
My observed criterion is that ncurses does not handle a window change by itself (at least by default, you have to consider also this is the same thing as what happens to ncurses that doesn't detect if another process has written to your tty, and the screen gets garbled). To handle this, you have to install a signal handler for the SIGWINCH
signal (this signal is sent to the whole process group when a change in terminal dimensions has occurred, normally from the pty master side), and make the appropiate ioctl(2)
to the tty device to get the new window dimensions and call the appropiate low level ncurses routines to change the new geometry of stdsrc
and stdwin
buffers, as that will need to reallocate memory for them. This means you are free to decide what happens to window contents on a terminal resize, but makes you to do all the work to deal with that. I have a program that, on a SIGWINCH
just exec(2)
s to itself and relaunchs itself, so it gets the new screen dimensions on the initscr(3)
call. If you cannot auto-exec(2)
your program, just dig in the documentation about how to reset the stdscr
dimensions and to change the LINS
and COLS
global variables correctly. (If I find doc about how to do it, I'll edit this answer with the appropiate info.)
NOTE
I've run your program and it works as expected... filling the fist line and the last with asterisks. Just put a call to getch()
before endwin()
to be able to see the screen drawn.