Advertisement
Guest User

OS1 - Cv 1

a guest
Oct 13th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.71 KB | None | 0 0
  1. // Operating Systems: sample code
  2. // Signals
  3. #include <stdio.h>  // standard I/O functions
  4. #include <unistd.h> // standard Unix functions, like getpid()
  5. #include <signal.h> // signal name macros, and the signal() prototype
  6. #include <stdlib.h> // system()
  7. #include <string.h> // strcmp()
  8.  
  9. #define GOOD_PASSWORD   "marvelous" // password we're expecting to get
  10.  
  11. // define values for 'YES' and 'NO'
  12. #ifndef YES
  13. #  define YES   1
  14. #  define NO    0
  15. #endif
  16.  
  17.  
  18. // This function switches echo mode on or off. In off mode, nothing
  19. // the user types is echoed on the screen (e.g. when typing a password).
  20. // Note: We're using the 'stty' command to switch the mode. There are
  21. //       cleaner ways, but we don't want to complicate things now.
  22. void echo_on(int is_it)
  23. {
  24.     if (is_it == YES)
  25.         system("stty echo");
  26.     else
  27.         system("stty -echo");
  28. }
  29.  
  30. // Here is the signal handler. It switches the input mode to echo-on,
  31. // then suspends the process using a STOP signal. Once the process'
  32. // operation is resumed, the function continues right after the call to
  33. // the kill() function, sets echo mode off, and reproduces the password
  34. // prompt, so the user will know that we're still expecting a password.
  35. void catch_suspend(int sig_num)
  36. {
  37.     signal(sig_num, catch_suspend); // re-set the signal handler
  38.  
  39.     printf("\nSuspending execution...\n");
  40.     fflush(stdout);
  41.  
  42.     echo_on(YES);           // re-enable echo mode
  43.  
  44.     kill(getpid(), SIGSTOP);    // stop self
  45.  
  46.     // we'll get back here when the process is resumed
  47.     printf("Resuming execution...\n");
  48.  
  49.     echo_on(NO);            // disable echo mode again
  50.  
  51.     printf("Password: ");       // reproduce the prompt
  52.     fflush(stdout);
  53. }
  54.  
  55.  
  56. // -------- and the main function goes here ---------
  57.  
  58. int main(int argc, char *argv[])
  59. {
  60. #define MAX_SIZE 30
  61.     char user[MAX_SIZE];    // user name supplied by the user
  62.     char passwd[MAX_SIZE];  // password supplied by the user
  63.  
  64.     printf("Username: ");       // prompt the user for a user name
  65.     fflush(stdout);
  66.  
  67.     fgets(user, MAX_SIZE, stdin);   // wait for input
  68.  
  69.     signal(SIGTSTP, catch_suspend); // set the TSTP (Ctrl-Z) signal handler
  70.  
  71.     printf("Password: ");       // prompt the user for a password
  72.     fflush(stdout);
  73.  
  74.     echo_on(NO);            // set input to no-echo mode
  75.     fgets(passwd, MAX_SIZE, stdin); // get the user input
  76.     echo_on(YES);           // re-enable echo on input
  77.  
  78.     printf("\n");           // the Enter pressed by the user was not echoed
  79.     fflush(stdout);
  80.  
  81.     signal(SIGTSTP, SIG_DFL);   // switch the TSTP signal handler to its default behaviour
  82.  
  83.     // verify the password (\n is stored, don't compare it)
  84.     if (strlen(passwd) - 1 == strlen(GOOD_PASSWORD) &&
  85.         strncmp(passwd, GOOD_PASSWORD, strlen(passwd) - 1) == 0)
  86.         printf("Access granted.\n");
  87.     else
  88.         printf("Access denied.\n");
  89.  
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement