home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / UNXGETCH.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  816b  |  44 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  UNXGETCH.C - Non-blocking console input function for Unix/Posix.
  5. **
  6. **  public domain by Bob Stout using Steve Poole's term_option().
  7. */
  8.  
  9. #include "unxconio.h"
  10.  
  11. int getch()
  12. {
  13.       int istat, key;
  14.       char buf[2];
  15.  
  16.       if (0 > term_option(1))
  17.             return EOF;
  18.       if (0 > term_option(2))
  19.             return EOF;
  20.       istat = read(STDIN_FILENO,&buf,1);
  21.       if (istat < 0)
  22.             key = EOF;
  23.       else  key = (int)buf[0];
  24.       term_option(0);
  25.       return key;
  26. }
  27.  
  28. #ifdef TEST
  29.  
  30. #include <ctype.h>
  31.  
  32. main()
  33. {
  34.       int key;
  35.  
  36.       printf("Press any key to continue...");
  37.       fflush(stdout);
  38.       key = getch();
  39.       printf("\n\nYou pressed \"%c\"\n", toupper(key));
  40.       return 0;
  41. }
  42.  
  43. #endif /* TEST */
  44.