#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>



 int clrscr(void)
 {
  system("clear");
  return(0);
 }


 int gotoxy(int j,int i)
 {
  char str[50];
  if(j>80) {
	printf("\n\nThe no of cols is greater than 80:%d:\n\n",j);
	exit(0);
  }
  sprintf(str,"tput cup %d %d",i,j);
  system(str); 
  return(0);
 }
 


 int getch_funcs(int echo_mode)
 {
  int fd, res;
  struct termios oldtio,newtio;
  char buf[2];
  fd = 0;

  tcgetattr(fd,&oldtio); /* save current port settings */

  bzero(&newtio, sizeof(newtio));
  newtio.c_cflag =  CRTSCTS | CS8 | CLOCAL | CREAD;
  newtio.c_iflag = IGNPAR;
  newtio.c_oflag = 0;
  newtio.c_lflag = echo_mode; /* set input mode (non-canonical, no echo) */
  newtio.c_cc[VTIME]    = 0;  /* inter-character timer unused */
  newtio.c_cc[VMIN]     = 1;  /* blocking read until 1 chars received */
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd,TCSANOW,&newtio);
  res = read(fd,buf,1);   /* returns after 1 chars have been input */
  buf[res]=0;            
  tcsetattr(fd,TCSANOW,&oldtio);
  return((int)buf[0]);
 }


 int getch(void)
 {
  return getch_funcs(0);
 }


 int getche(void)
 {
  return getch_funcs(1);
 }
