#include <config.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>
#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <termios.h>
#include "missing.h"
#if !defined(TIOCGWINSZ) && defined(TIOCGSIZE)
# define TIOCGWINSZ TIOCGSIZE
# define winsize ttysize
# define ws_col ts_cols
# define ws_row ts_lines
#endif
#ifdef TIOCGWINSZ
static int
get_ttysize_ioctl(rowp, colp)
int *rowp;
int *colp;
{
struct winsize wsize;
if (ioctl(STDERR_FILENO, TIOCGWINSZ, &wsize) == 0 &&
wsize.ws_row != 0 && wsize.ws_col != 0) {
*rowp = wsize.ws_row;
*colp = wsize.ws_col;
return 0;
}
return -1;
}
#else
static int
get_ttysize_ioctl(rowp, colp)
int *rowp;
int *colp;
{
return -1;
}
#endif
void
get_ttysize(rowp, colp)
int *rowp;
int *colp;
{
if (get_ttysize_ioctl(rowp, colp) == -1) {
char *p;
if ((p = getenv("LINES")) == NULL || (*rowp = atoi(p)) <= 0)
*rowp = 24;
if ((p = getenv("COLUMNS")) == NULL || (*colp = atoi(p)) <= 0)
*colp = 80;
}
return;
}