#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <time.h>

int main()
{
	clock_t start, end;
	double cpu_time_used;

	// Set start to current CPU time
	start = clock();
	
	int c;
	int flag = 0;
	int laps = 0;
	
	/*
	  THIS IS A REPLACEMENT FOR THE _KBHIT FUNCTION
	*/	
	// These structs are for saving tty parameters
	static struct termios old_term, new_term;

	// Get parameters associated with the tty
	// Set the new tty parameters to the old one
	tcgetattr(STDIN_FILENO, &old_term);
	new_term = old_term;

	// Set the new tty parameters to canonical mode
	new_term.c_lflag &= ~(ICANON);

	// Set tty attributes
	tcsetattr(STDIN_FILENO, TCSANOW, &new_term);

	// Get character input by user
	while ((c = getchar()) != EOF && laps < 10)
	{
		// Flip the flag
		flag = !flag;
		laps++;
		
		printf("\n");
		printf("Flag: %d\n", flag);
	}

	// Done retriving input
	// Set tty parameters back to old tty parameters
	tcsetattr(STDIN_FILENO, TCSANOW, &old_term);

	end = clock();
	cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

	printf("CPU Time: %f\n", cpu_time_used);
	
	return 0;
}
