SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <termios.h>
  5. #include <unistd.h>
  6.  
  7. struct termios orig_termios;
  8.  
  9. void disableRawMode() {
  10.     tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
  11.     printf("Term attributes restoredn");
  12. }
  13.  
  14. void enableRawMode() {
  15.     tcgetattr(STDIN_FILENO, &orig_termios);
  16.  
  17.     struct termios new_termios;
  18.  
  19.     memcpy(&new_termios, &orig_termios, sizeof(new_termios));
  20.     new_termios.c_lflag &= ~ECHO;
  21.     tcsetattr(STDIN_FILENO, TCSAFLUSH, &new_termios);
  22.  
  23. //
  24.     if (new_termios.c_iflag & BRKINT)
  25.       puts("BRKINT is set");
  26.     else
  27.       puts("BRKINT is not set");
  28.     if (new_termios.c_cflag & PARODD)
  29.       puts("Odd parity is used");
  30.     else
  31.       puts("Even parity is used");
  32.     if (new_termios.c_lflag & ECHO)
  33.       puts("ECHO is set");
  34.     else
  35.       puts("ECHO is not set");
  36. //
  37. }
  38.  
  39. int main(void)
  40. {
  41.     enableRawMode();
  42.     if(atexit(disableRawMode)) printf("Error in atexit().n");
  43.     return 0;
  44. }
  45.    
  46. $ ./atexit_test
  47. BRKINT is set
  48. Even parity is used
  49. ECHO is not set
  50. Term attributes restored
  51.  
  52. $ timeout 3s ./atexit_test
  53. BRKINT is set
  54. Even parity is used
  55. ECHO is not set
  56. Term attributes restored
  57.  
  58. $ sudo su -p - root bash -c "/home/programer/media/atexit_test/atexit_test"
  59. BRKINT is set
  60. Even parity is used
  61. ECHO is not set
  62. Term attributes restored
  63.  
  64. $ sudo timeout 3s ./atexit_test
  65. $
  66. $ sudo timeout 3s ./atexit_test; echo $?
  67. 124
  68.    
  69. tcsetattr(STDIN_FILENO, TCSAFLUSH, &new_termios);
  70.    
  71. if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &new_termios) == -1) {
  72.         int errsv = errno;
  73.         printf("%d",errsv);
  74.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top