├── README.md ├── bash └── main.sh └── c └── main.c /README.md: -------------------------------------------------------------------------------- 1 | # check_SIGINT 2 | Programs that check accidental SIGINT command. 3 | 4 | There might have a time when you had to copy a data from terminal but you forgot to press shift key 5 | while copying that caused an accidental SIGINT signal which generally causes lots of data loss and time loss 6 | 7 | To prevent this you can add these codes in your program as a verification 8 | -------------------------------------------------------------------------------- /bash/main.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # main 3 | trap 'myExit;' SIGINT 4 | count=0 5 | myExit() 6 | { 7 | echo "Do you really want to exit? press y to confirm ...." 8 | read answer 9 | if [[ "$answer" == "y" ]]; 10 | then 11 | echo "program is exiting...." 12 | sleep 1 13 | exit 14 | else 15 | echo "program will continue" 16 | fi 17 | } 18 | while : 19 | do 20 | sleep 1 21 | count=$(expr $count + 1) 22 | echo $count 23 | done -------------------------------------------------------------------------------- /c/main.c: -------------------------------------------------------------------------------- 1 | // A C program that check when Ctrl+C is pressed 2 | #include 3 | #include 4 | #include 5 | int check = 0; 6 | // Signal Handler for SIGINT 7 | void sigintHandler(int sig_num) 8 | { 9 | // Reset handler to catch SIGINT next time 10 | signal(SIGINT, sigintHandler); 11 | int char_check = 0; 12 | while(!char_check) 13 | { 14 | printf("\n Do you want to quit? press y/n \n"); 15 | char a; 16 | scanf(" %c", &a); 17 | if(a=='y') 18 | { 19 | printf("\n Sayonara \n"); 20 | char_check = 1; 21 | exit(0); 22 | } 23 | else if(a=='n') 24 | { 25 | printf("\n OK you ma continue\n"); 26 | char_check = 1; 27 | } 28 | else 29 | { 30 | printf("\n Invalid response please retype \n"); 31 | } 32 | } 33 | fflush(stdout); 34 | } 35 | 36 | int main () 37 | { 38 | // Set the SIGINT (Ctrl-C) signal handler to sigintHandler 39 | 40 | 41 | signal(SIGINT, sigintHandler); 42 | 43 | // Infinite loop 44 | while(1) 45 | { 46 | } 47 | return 0; 48 | } --------------------------------------------------------------------------------