cmkl_assignments/fall-2024/sys-arch/sys-102/00040/source/polling.c~

41 lines
804 B
C
Raw Permalink Normal View History

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
int main()
{
int c;
int flag = 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)
{
flag = !flag;
printf("Flag: %d\n", flag);
}
// Done retriving input
// Set tty parameters back to old tty parameters
tcsetattr(STDIN_FILENO, TCSANOW, &old_term);
return 0;
}