#include #include #include #include #include #include #include void IOHandler (int); int main () { int ct, nch; struct termios ttios, ttsave; int flags; fd_set readfds; char ch; struct sigaction newio; // Set the terminal to operate in non-canonical mode. tcgetattr (0, &ttios); ttsave = ttios; ttios . c_oflag &= ~OPOST; ttios . c_iflag &= ~(IXON | BRKINT | INLCR | ICRNL | IUCLC); ttios . c_lflag &= ~(ICANON | ECHO); ttios . c_cc [VMIN] = 0; ttios . c_cc [VTIME] = 0; tcsetattr (0, TCSAFLUSH, &ttios); /* * Initialize the terminal to operate in interrupt driven mode. */ fcntl (0, F_SETOWN, getpid ()); flags = fcntl (0, F_GETFL); if (fcntl (0, F_SETFL, flags | O_ASYNC) < 0) { perror ("fcntl on stdin"); exit (0); } /* * Set up something other than the default signal handler. */ /* newio.sa_handler = IOHandler; sigfillset (&(newio.sa_mask)); newio.sa_flags = (int) 0; sigaction (SIGIO, &newio, NULL); */ /* * Read and write some typed keys. Note, the mode allows * the normal Linux handler to continue to process the data * and handle special characters like CTRL-C and CTRL-Z, but * most key presses are simply passed through. This includes * non-special Control keys and Enter. */ while (1) { /* * Wait for something to happen. This is really simple because * we are only interested in stdin. */ sigsuspend (0); FD_ZERO (&readfds); FD_SET (0, &readfds); select (1, &readfds, 0, 0, 0); /* * If something has been typed, get it. This is incomplete, * because it could be that there is more than one character * in the queue, so you really should read until there is * nothing left. */ if (FD_ISSET (0, &readfds)) if ((nch = read (0, (void *) &ch, 1)) == 1) { printf ("Read the character %x \r\n", ch); if (ch == 'E') break; } } /* * Restore everything and quit */ newio.sa_handler = SIG_DFL; sigaction (SIGIO, &newio, NULL); fcntl(0, F_SETFL, flags); tcsetattr (0, TCSAFLUSH, &ttsave); } /* * The signal handler does nothing. However, the default handler has to * be replaced so we need this. Everything that is done above below * the sigpause in the loop could be done here, but it is much less likely * to create problems if you use the sigpause method as you don't have to * worry about coincident signals. */ void IOHandler (int s) { return; }