ts.ts_lines = rows; ts.ts_cols = cols; ioctl(tty, TIOCSSIZE, &ts); } #elif defined(TIOCSWINSZ)
TTYSIZE_ROWS(ts) = rows; TTYSIZE_COLS(ts) = cols; SET_TTYSIZE(tty, ts); } #elif defined(USE_STRUCT_WINSIZE)
main(int argc, char **argv ENVP_ARG){    register char *ptr, *env;    register int emu = VT100;    char *shell;    struct passwd *pw;    int i;    int rows, cols;#ifdef USE_ANY_SYSV_TERMIO    struct termio tio;#elif defined(USE_TERMIOS)    struct termios tio;#else    struct sgttyb sg;#endif /* USE_ANY_SYSV_TERMIO/USE_TERMIOS */#ifdef USE_TERMCAP    int ok_tcap = 1;    char termcap[TERMCAP_SIZE];    char newtc[TERMCAP_SIZE];#endif /* USE_TERMCAP */    char buf[BUFSIZ];#if defined(TIOCSSIZE) && (defined(sun) && !defined(SVR4))    struct ttysize ts;#elif defined(TIOCSWINSZ)    struct winsize ws;#endif /* sun vs TIOCSWINSZ */    char *name_of_tty;#ifdef CANT_OPEN_DEV_TTY    extern char *ttyname();#endif    myname = x_basename(argv[0]);    if (strcmp(myname, sunname) == 0)	emu = SUN;    for (argv++, argc--; argc > 0 && **argv == '-'; argv++, argc--) {	switch ((*argv)[1]) {	case 's':		/* Sun emulation */	    if (emu == SUN)		Usage();	/* Never returns */	    emu = SUN;	    break;	case 'u':		/* Bourne (Unix) shell */	    shell_type = SHELL_BOURNE;	    break;	case 'c':		/* C shell */	    shell_type = SHELL_C;	    break;	default:	    Usage();		/* Never returns */	}    }    if (SHELL_UNKNOWN == shell_type) {	/* Find out what kind of shell this user is running.	 * This is the same algorithm that xterm uses.	 */	if (((ptr = getenv("SHELL")) == NULL || *ptr == 0) &&	    (((pw = getpwuid(getuid())) == NULL) ||	     *(ptr = pw->pw_shell) == 0))	    /* this is the same default that xterm uses */	    ptr = "/bin/sh";	shell = x_basename(ptr);	/* now that we know, what kind is it? */	for (i = 0; shell_list[i].name; i++)	    if (!strcmp(shell_list[i].name, shell))		break;	shell_type = shell_list[i].type;    }    if (argc == 2) {	if (!setsize[emu]) {	    fprintf(stderr,		    "%s: Can't set window size under %s emulation\n",		    myname, emuname[emu]);	    exit(1);	}	if (!checkdigits(argv[0]) || !checkdigits(argv[1]))	    Usage();		/* Never returns */    } else if (argc != 0)	Usage();		/* Never returns */#ifdef CANT_OPEN_DEV_TTY    if ((name_of_tty = ttyname(fileno(stderr))) == NULL)#endif	name_of_tty = "/dev/tty";    if ((ttyfp = fopen(name_of_tty, "r+")) == NULL) {	fprintf(stderr, "%s:  can't open terminal %s\n",		myname, name_of_tty);	exit(1);    }    tty = fileno(ttyfp);#ifdef USE_TERMCAP    if (!(env = getenv("TERM")) || !*env) {	env = DFT_TERMTYPE;	if (SHELL_BOURNE == shell_type)	    setname = "TERM=xterm;\nexport TERM;\n";	else	    setname = "setenv TERM xterm;\n";    }    termcap[0] = 0;		/* ...just in case we've accidentally gotten terminfo */    if (tgetent(termcap, env) <= 0 || termcap[0] == 0)	ok_tcap = 0;#endif /* USE_TERMCAP */#ifdef USE_TERMINFO    if (!(env = getenv("TERM")) || !*env) {	env = DFT_TERMTYPE;	if (SHELL_BOURNE == shell_type)	    setname = "TERM=xterm;\nexport TERM;\n";	else	    setname = "setenv TERM xterm;\n";    }#endif /* USE_TERMINFO */#ifdef USE_ANY_SYSV_TERMIO    ioctl(tty, TCGETA, &tioorig);    tio = tioorig;    tio.c_iflag &= ~(ICRNL | IUCLC);    tio.c_lflag &= ~(ICANON | ECHO);    tio.c_cflag |= CS8;    tio.c_cc[VMIN] = 6;    tio.c_cc[VTIME] = 1;#elif defined(USE_TERMIOS)    tcgetattr(tty, &tioorig);    tio = tioorig;    tio.c_iflag &= ~ICRNL;    tio.c_lflag &= ~(ICANON | ECHO);    tio.c_cflag |= CS8;    tio.c_cc[VMIN] = 6;    tio.c_cc[VTIME] = 1;#else /* not USE_TERMIOS */    ioctl(tty, TIOCGETP, &sgorig);    sg = sgorig;    sg.sg_flags |= RAW;    sg.sg_flags &= ~ECHO;#endif /* USE_ANY_SYSV_TERMIO/USE_TERMIOS */    signal(SIGINT, onintr);    signal(SIGQUIT, onintr);    signal(SIGTERM, onintr);#ifdef USE_ANY_SYSV_TERMIO    ioctl(tty, TCSETAW, &tio);#elif defined(USE_TERMIOS)    tcsetattr(tty, TCSADRAIN, &tio);#else /* not USE_TERMIOS */    ioctl(tty, TIOCSETP, &sg);#endif /* USE_ANY_SYSV_TERMIO/USE_TERMIOS */    if (argc == 2) {	sprintf(buf, setsize[emu], argv[0], argv[1]);	write(tty, buf, strlen(buf));    }    write(tty, getsize[emu], strlen(getsize[emu]));    readstring(ttyfp, buf, size[emu]);    if (sscanf(buf, size[emu], &rows, &cols) != 2) {	fprintf(stderr, "%s: Can't get rows and columns\r\n", myname);	onintr(0);    }    if (restore[emu])	write(tty, restore[emu], strlen(restore[emu]));#if defined(TIOCSSIZE) && (defined(sun) && !defined(SVR4))    /* finally, set the tty's window size */    if (ioctl(tty, TIOCGSIZE, &ts) != -1) {	ts.ts_lines = rows;	ts.ts_cols = cols;	ioctl(tty, TIOCSSIZE, &ts);    }#elif defined(TIOCSWINSZ)    /* finally, set the tty's window size */    if (getwsize[emu]) {	/* get the window size in pixels */	write(tty, getwsize[emu], strlen(getwsize[emu]));	readstring(ttyfp, buf, wsize[emu]);	if (sscanf(buf, wsize[emu], &ws.ws_xpixel, &ws.ws_ypixel) != 2) {	    fprintf(stderr, "%s: Can't get window size\r\n", myname);	    onintr(0);	}	ws.ws_row = rows;	ws.ws_col = cols;	ioctl(tty, TIOCSWINSZ, &ws);    } else if (ioctl(tty, TIOCGWINSZ, &ws) != -1) {	/* we don't have any way of directly finding out	   the current height & width of the window in pixels.  We try	   our best by computing the font height and width from the "old"	   struct winsize values, and multiplying by these ratios... */	if (ws.ws_col != 0)	    ws.ws_xpixel = cols * (ws.ws_xpixel / ws.ws_col);	if (ws.ws_row != 0)	    ws.ws_ypixel = rows * (ws.ws_ypixel / ws.ws_row);	ws.ws_row = rows;	ws.ws_col = cols;	ioctl(tty, TIOCSWINSZ, &ws);    }#endif /* sun vs TIOCSWINSZ */#ifdef USE_ANY_SYSV_TERMIO    ioctl(tty, TCSETAW, &tioorig);#elif defined(USE_TERMIOS)    tcsetattr(tty, TCSADRAIN, &tioorig);#else /* not USE_TERMIOS */    ioctl(tty, TIOCSETP, &sgorig);#endif /* USE_ANY_SYSV_TERMIO/USE_TERMIOS */    signal(SIGINT, SIG_DFL);    signal(SIGQUIT, SIG_DFL);    signal(SIGTERM, SIG_DFL);#ifdef USE_TERMCAP    if (ok_tcap) {	/* update termcap string */	/* first do columns */	if ((ptr = x_strindex(termcap, "co#")) == NULL) {	    fprintf(stderr, "%s: No `co#'\n", myname);	    exit(1);	}	i = ptr - termcap + 3;	strncpy(newtc, termcap, i);	sprintf(newtc + i, "%d", cols);	ptr = strchr(ptr, ':');	strcat(newtc, ptr);	/* now do lines */	if ((ptr = x_strindex(newtc, "li#")) == NULL) {	    fprintf(stderr, "%s: No `li#'\n", myname);	    exit(1);	}	i = ptr - newtc + 3;	strncpy(termcap, newtc, i);	sprintf(termcap + i, "%d", rows);	ptr = strchr(ptr, ':');	strcat(termcap, ptr);    }#endif /* USE_TERMCAP */    if (SHELL_BOURNE == shell_type) {#ifdef USE_TERMCAP	if (ok_tcap) {	    printf("%sTERMCAP=", setname);	    print_termcap(termcap);	    printf(";\nexport TERMCAP;\n");	}#endif /* USE_TERMCAP */#ifdef USE_TERMINFO	printf("%sCOLUMNS=%d;\nLINES=%d;\nexport COLUMNS LINES;\n",	       setname, cols, rows);#endif /* USE_TERMINFO */    } else {			/* not Bourne shell */#ifdef USE_TERMCAP	if (ok_tcap) {	    printf("set noglob;\n%ssetenv TERMCAP ", setname);	    print_termcap(termcap);	    printf(";\nunset noglob;\n");	}#endif /* USE_TERMCAP */#ifdef USE_TERMINFO	printf("set noglob;\n%ssetenv COLUMNS '%d';\nsetenv LINES '%d';\nunset noglob;\n",	       setname, cols, rows);#endif /* USE_TERMINFO */    }    exit(0);}