/commTools/getch.c

http://github.com/Yniold/liftsrc · C · 48 lines · 25 code · 6 blank · 17 comment · 4 complexity · a23357618cf3531ccce77d4dacd022cc MD5 · raw file

  1. /*
  2. * $RCSfile: getch.c,v $ last changed on $Date: 2006/09/04 11:53:29 $ by $Author: rudolf $
  3. *
  4. * $Log: getch.c,v $
  5. * Revision 1.2 2006/09/04 11:53:29 rudolf
  6. * Fixed warnings for GCC 4.03, added newline and CVS revision info
  7. *
  8. *
  9. *
  10. */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <termios.h>
  14. #include <unistd.h>
  15. static struct termios new_io;
  16. static struct termios old_io;
  17. /* Funktion schaltet das Terminal in den cbreak-Modus: */
  18. /* Kontrollflag ECHO und ICANON auf 0 setzen */
  19. /* Steuerzeichen: Leseoperation liefert 1 Byte VMIN=1 VTIME=1 */
  20. int cbreak(int fd) {
  21. /*Sichern unseres Terminals*/
  22. if((tcgetattr(fd, &old_io)) == -1)
  23. return -1;
  24. new_io = old_io;
  25. /*Wir verändern jetzt die Flags für den cbreak-Modus*/
  26. new_io.c_lflag = new_io.c_lflag & ~(ECHO|ICANON);
  27. new_io.c_cc[VMIN] = 0;
  28. new_io.c_cc[VTIME]= 0;
  29. /*Jetzt setzen wir den cbreak-Modus*/
  30. if((tcsetattr(fd, TCSAFLUSH, &new_io)) == -1)
  31. return -1;
  32. return 1;
  33. }
  34. int restoreinput(void){
  35. /*Alten Terminal-Modus wiederherstellen*/
  36. tcsetattr(STDIN_FILENO, TCSANOW, &old_io);
  37. };
  38. int getch(void) {
  39. int c;
  40. c = getchar();
  41. return c;
  42. }