/*
	file: lico.c
	author: ikeda@misystems.co.jp
		check terminals lines/columns
	modify: kmiya
*/

#define USE_IOCTL
#define USE_TERMIO
#define	USE_TTY	/* */
/* #define	USE_SGTTY */

#define USE_TERMCAP

#include <stdio.h>

#ifdef USE_IOCTL
#include <sys/ioctl.h>
#ifdef USE_SGTTY
#include <sgtty.h>
#endif
#ifdef USE_TTY
#include <sys/types.h>
#include <sys/tty.h>
#endif
#ifdef USE_TERMIO
#include <termio.h>
#endif
#endif /* USE_IOCTL */

char *getenv();

void	cols_lines_get(int *cols, int *lines)
{
#ifdef	TIOCGWINSZ
    struct winsize winsize;
#endif
#ifdef USE_TERMCAP
    int  n;
    char *name;
    char bp[1024];
#endif
    char *p;

#ifdef	TIOCGWINSZ
    if( ioctl(1, TIOCGWINSZ, &winsize) != -1 ) {
	*lines = winsize.ws_row;
	*cols = winsize.ws_col;
	return;
    }
#endif	/* TIOCGWINSZ */

#ifdef USE_TERMCAP
    if( (name = getenv("TERM")) != NULL ) {
	if( tgetent(bp, name) == 1 ) {
	    if( (n = tgetnum("li")) != -1 )
		*lines = n;
	    if ( (n = tgetnum("co")) != -1 )
		*cols = n;
	    return;
	}
    }
#endif /* USE_TERMCAP */

    if ( (p = getenv("LINES")) != NULL )
	*lines = atoi(p);
    if ( (p = getenv("COLUMNS")) != NULL )
	*cols = atoi(p);
    if ( (p = getenv("COLS")) != NULL )
	*cols = atoi(p);
}
