#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>

/*
   empty_keyboard_buffer -- clears the keyboard buffer
   Abhijit Menon-Sen  <ams@toroid.org>   2008-03-17
*/



int main()
{
    char c;
    int flags;

    if ( tcdrain( 0 ) < 0 || tcflush( 0, TCIOFLUSH ) < 0 )
        return -1;

    flags = fcntl( 0, F_GETFL, 0 );
    if ( flags < 0 )
        return -1;
    if ( fcntl( 0, F_SETFL, flags|O_NDELAY ) < 0 )
        return -1;

    while ( read( 0, &c, 1 ) > 0 )
        ;

    return 0;
}
