Tobiahao

SO1_TERMINAL_04

Jan 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. /*
  2. Napisz program, po którego uruchomieniu terminal będzie wypisywał wszystkie informacje dużymi literami (niezależnie od ustawienia CapsLock). Efekt ten powinno usuwać ponowne uruchomienie programu.
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <termios.h>
  9. #include <string.h>
  10.  
  11. #define BUFFER_SIZE 128
  12.  
  13. void restore_settings(struct termios default_settings)
  14. {
  15.     if(tcsetattr(STDIN_FILENO, TCSANOW, &default_settings) == -1) {
  16.         perror("tcsetattr restore_settings");
  17.         exit(EXIT_FAILURE);
  18.     }
  19.  
  20. }
  21.  
  22. void set_terminal_attr()
  23. {
  24.     struct termios attr;
  25.  
  26.     if(tcgetattr(STDIN_FILENO, &attr) == -1) {
  27.         perror("tcgetattr set_terminal_attr");
  28.         exit(EXIT_FAILURE);
  29.     }
  30.  
  31.     attr.c_oflag |= OLCUC;
  32.  
  33.     if(tcsetattr(STDIN_FILENO, TCSANOW, &attr) == -1) {
  34.         perror("tcsetattr set_terminal_attr");
  35.         exit(EXIT_FAILURE);
  36.     }
  37. }
  38.  
  39. int main(void)
  40. {
  41.     struct termios default_settings;
  42.     const char *write_something = "Napisz cos: ";
  43.     char write_buffer[BUFFER_SIZE];
  44.  
  45.     if(!isatty(STDIN_FILENO)) {
  46.         perror("isatty");
  47.         return EXIT_FAILURE;
  48.     }
  49.  
  50.     if(tcgetattr(STDIN_FILENO, &default_settings) == -1) {
  51.         perror("tcgetattr");
  52.         return EXIT_FAILURE;
  53.     }
  54.  
  55.     write(STDIN_FILENO, write_something, strlen(write_something));
  56.     set_terminal_attr();
  57.    
  58.     read(STDIN_FILENO, write_buffer, BUFFER_SIZE);
  59.    
  60.     restore_settings(default_settings);
  61.  
  62.     return EXIT_SUCCESS;
  63. }
Add Comment
Please, Sign In to add comment