To get the size, the correct way is to call TIOCGWINSZ ioctl() . An example from my code:
struct winsize ws = { 0, 0, 0, 0 }; if(ioctl(tt->outfd, TIOCGWINSZ, &ws) == -1) return;
You will want to do this first, and then again after receiving a SIGWINCH signal that reports a WINDOW CHange.
As for getting the cursor position, this is a little trickier. Some terminals allow you to request it using DSR 6 (device status report)
$ echo -ne "\e[6n"; cat -v ^[[62;1R
The answer from DSR comes in CSI R, it tells me the (1st) 62nd row, 1st column.
However, since not all terminals support DSR 6, it may be easiest not to rely on the ability to request the cursor position and instead perform absolute addressing of the final address by placing the cursor where you want.
source share