#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#ifndef TIOCGWINSIZ
#include <sys/ioctl.h>
#endif

#ifdef SYSV
     int zc_terminal(rows,columns)
#else
     int zc_terminal_(rows,columns)
#endif
/*
C----------------------------------------------------------------------
C
C.IDENTIFICATION: INTEGER Function ZC_TERMINAL
C.LIBRARY:        VOS (experimental version)
C.AUTHOR:         L.Chiappetti - IFCTR Milano
C.VERSIONS:       0.0 - 17 Mar 94 - Test version (Ultrix)
C                 0.1 - 04 Jun 94 - New version using termios (DDF)
C
C.PURPOSE:        Fortran-C-interface to get terminal size (row, columns)
C.METHOD:         issue appropriate ioctl
C
C.SYNTAX:         IERR=ZC_TERMINAL(ROWS,COLUMNS)
C.PARAMETERS:     INTEGER   rows   : number of rows
C                 INTEGER   columns: number of columns
C                 returns the error code of the ioctl
C
C.RESTRICTIONS:
C.NOTES:
C.FILES:          none
C.REFERENCES:
C
C----------------------------------------------------------------------
*/
   int *rows,*columns;
{
/* the structure returns the number of rows, columns, and pixels in a window */
   struct winsize window;
   int i,fd;

   if ( ! isatty ( STDIN_FILENO ) )
   {
/*  No rows and columns! */
     *rows = 0;
     *columns = 0;
     return -1 ;
   }
   i=ioctl(STDIN_FILENO,TIOCGWINSZ,&window);
   *rows = (int) window.ws_row;
   *columns = (int) window.ws_col;
/* window.ws_xpixel,window.ws_ypixel additionally give window sizes in pixel */
/* however since no VMS equivalent exist, their return is not implemented    */
   return i;
}
