

/*
 *
 * This file contains some of the functions which simulate conio.h in windows.
 *
 */
      

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


/*
 *
 * This function sets the cursor at j'th column and i'th row of window.
 *
 * The column and row numbers start form 0.
 *
 * 0,0 represents the left upper part of the window.
 *
 */
      


 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]);
 }


 /* Get a character, without the user having to press enter after the input. */
 /* This does not echo the input character typed */
 int getch(void)
 {
  return getch_funcs(0);
 }


 /* Get a character, without the user having to press enter after the input. */
 /* This echos the input character typed */
 int getche(void)
 {
  return getch_funcs(1);
 }
