getch.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* Extremely inefficient but portable POSIX getch() */
  2. #ifndef WIN32
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <termios.h> /* for tcgetattr() and tcsetattr() */
  6. #include <unistd.h> /* for read() */
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. #include <sys/time.h>
  10. #include "getch.h"
  11. #ifndef STDIN_FILENO
  12. # define STDIN_FILENO 0
  13. #endif
  14. int
  15. getch (void)
  16. {
  17. struct termios save_termios;
  18. struct termios ios;
  19. int c = 0;
  20. if (!isatty (STDIN_FILENO))
  21. return EOF;
  22. if (tcgetattr (STDIN_FILENO, &save_termios) < 0)
  23. return EOF;
  24. ios = save_termios;
  25. ios.c_lflag &= ~(ICANON | ECHO | ISIG);
  26. ios.c_cc[VMIN] = 1; /* read() will return with one char */
  27. ios.c_cc[VTIME] = 0; /* read() blocks forever */
  28. if (tcsetattr (STDIN_FILENO, TCSANOW, &ios) < 0)
  29. return EOF;
  30. if (read (STDIN_FILENO, &c, 1) != 1)
  31. c = EOF;
  32. tcsetattr (STDIN_FILENO, TCSANOW, &save_termios);
  33. return c;
  34. }
  35. int
  36. kbhit (void)
  37. {
  38. struct termios save_termios;
  39. struct termios ios;
  40. fd_set inp;
  41. struct timeval timeout = {0, 0};
  42. int result;
  43. if (!isatty (STDIN_FILENO))
  44. return 0;
  45. if (tcgetattr (STDIN_FILENO, &save_termios) < 0)
  46. return 0;
  47. ios = save_termios;
  48. ios.c_lflag &= ~(ICANON | ECHO | ISIG);
  49. ios.c_cc[VMIN] = 1; /* read() will return with one char */
  50. ios.c_cc[VTIME] = 0; /* read() blocks forever */
  51. if (tcsetattr (STDIN_FILENO, TCSANOW, &ios) < 0)
  52. return 0;
  53. /* set up select() args */
  54. FD_ZERO(&inp);
  55. FD_SET(STDIN_FILENO, &inp);
  56. result = select (STDIN_FILENO+1, &inp, NULL, NULL, &timeout) == 1;
  57. tcsetattr (STDIN_FILENO, TCSANOW, &save_termios);
  58. return result;
  59. }
  60. #endif