doc2.txt~ 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <termios.h>
  5. /* Use this variable to remember original terminal attributes. */
  6. struct termios saved_attributes;
  7. void
  8. reset_input_mode (void)
  9. {
  10. tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes);
  11. }
  12. void
  13. set_input_mode (void)
  14. {
  15. struct termios tattr;
  16. char *name;
  17. /* Make sure stdin is a terminal. */
  18. if (!isatty (STDIN_FILENO))
  19. {
  20. fprintf (stderr, "Not a terminal.\n");
  21. exit (EXIT_FAILURE);
  22. }
  23. /* Save the terminal attributes so we can restore them later. */
  24. tcgetattr (STDIN_FILENO, &saved_attributes);
  25. atexit (reset_input_mode);
  26. /* Set the funny terminal modes. */
  27. tcgetattr (STDIN_FILENO, &tattr);
  28. tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
  29. tattr.c_cc[VMIN] = 1;
  30. tattr.c_cc[VTIME] = 0;
  31. tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
  32. }
  33. int
  34. main (void)
  35. {
  36. char c;
  37. set_input_mode ();
  38. while (1)
  39. {
  40. read (STDIN_FILENO, &c, 1);
  41. if (c == '\004') /* C-d */
  42. break;
  43. else
  44. putchar (c);
  45. }
  46. return EXIT_SUCCESS;
  47. }