How do I get vi to know my window size?

Rich Felker dalias at aerifal.cx
Thu Sep 21 17:38:18 UTC 2006


A couple comments:

On Thu, Sep 21, 2006 at 07:13:20PM +0200, Bernhard Fischer wrote:
> /* Copyright 2006 Bernhard Fischer
>  * Licensed under GPLv2 or (at landleys or your opinion) any later version.
>  * Have fun or somesuch.
>  */
> #include <stdio.h>
> #include <unistd.h>
> #include <sys/ioctl.h>
> #include <termios.h>
> #include <stdlib.h>
> static struct termios old;
> static void reset_to_old(void) {
> 	tcsetattr(0, TCSANOW, &old);
> }
> int main(void) {
> 	/* save_cursor_pos 7
> 	 * scroll_whole_screen [r
> 	 * put_cursor_waaaay_off [$x;$yH
> 	 * get_cursor_pos [6n
> 	 * restore_cursor_pos 8
> 	 */
> 	const char req[] = "\0337\033[r\033[999;999H\033[6n";
> 	FILE *fp = fopen("/dev/stdin", "r+");
> 	int f = fileno(fp);

Umm, this code is rather silly, and depends on /proc. Why not use
/dev/tty or if you really mean stdin, use fdopen(0)...?

> 	struct winsize w = {0,0,0,0};
> 	int ret;
> 	struct termios new;
> 	tcgetattr(0, &old); /* fiddle echo */
> 	new = old;
> 	new.c_cflag |= (CLOCAL | CREAD);
> 	new.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
> #if defined(TIMEOUT) && (TIMEOUT >=1)
> 	new.c_cc[VMIN] = 0;
> 	new.c_cc[VTIME] = TIMEOUT;
> #endif
> 	tcsetattr(0, TCSANOW, &new);
> 	atexit(reset_to_old);

Using atexit() is not useful unless you call exit() from somewhere in
the program. Default fatal signal handlers cannot call atexit handlers
for you because they take place at the kernel level so you need to
handle them yourself if you want to.

Keep in mind that for static linking, atexit could add a lot of size
to the program too. So can scanf/printf. The program hardly uses stdio
as written and it should be easy to make it not depend on stdio at
all..

Rich




More information about the busybox mailing list