Determining screen width

Roger Droz roger at .uucp
Thu Nov 8 11:09:55 AEST 1990


Using termcap/terminfo routines seems like a very expensive way of
determining the screen width, if that's the only terminal capability one
is interested in.

I have seen several programs (such as less) use ioctl calls, but I know
that not all of the Un*x systems in house have these.  Code that uses
ioctl invariably also uses tgetnum() if all else fails.  That big
termcap/terminfo library still has to be linked in only to be used
once.

#ifdef TIOCGWINSZ
 	if (ioctl(2, TIOCGWINSZ, &w) == 0 && w.ws_col)
		sc_width = w.ws_col;
	else
#ifdef WIOCGETD
	if (ioctl(2, WIOCGETD, &w) == 0 && w.uw_width)
		sc_width = w.uw_width/w.uw_hs;
	else
#endif
#endif
 		sc_width = tgetnum("co");
 	if (dumb || sc_width < 0)
  		sc_width = 80;

What do you net people this of the following as a terse, portable way of
determining screen width? 

FILE *tputfile;
int stdoutwidth;

if ((tputfile = popen("tput cols", "r")) != NULL) {
	if (fscanf(tputfile,"%d", &stdoutwidth) < 1)
		stdoutwidth = 80;
	(void) pclose(tputfile);
}
____________
               Roger Droz       UUCP: uw-beaver!gtisqr!roger
()       ()    Maverick MICRoSystems / Global Technology International
 (_______)     Mukilteo, WA 
  (     )      
   |   |       Disclaimer: "We're all mavericks here: 
   |   |                    Each of us has our own opinions,
   (___)                    and the company has yet different ones!"

	



More information about the Comp.unix.questions mailing list