How to get terminal window width?

后端 未结 3 742
猫巷女王i
猫巷女王i 2020-12-11 23:10

I am working on embedded system using C and linux. User can connect to the device via SSH or a console serial cable. He can do this through PuTTY o

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-11 23:47

    A serial line does not have a way to negotiate the size of the terminal. Running a real xterm et.al. over a TTY/PTY device pair uses the TIOCSWINSZ/TIOCGWINSZ ioctl() pair to pass this information.

    Traditionally, this information was kept in termcap or terminfo about the real physical piece-of-glass-with-phosphor-coating terminals such as a DEC VT220. This is a static fixed database of values known ahead of time, which was correct around the time that a "terminal" literally was this fixed piece of hardware, but doesn't work so well in modern times of terminals being just some piece of program that draws to a framebuffer or similar.

    I'd suggest the best method would be to try TIOCGWINSZ, if that fails use terminfo's tgetnum("co"), and if that fails just presume 80x24.

提交回复
热议问题