Advertisement
Guest User

Simple print format

a guest
Dec 8th, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. /*
  2. compile with 'gcc source.c -o q1 -Wall -m32'
  3. */
  4.  
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #define PASS_SIZE 12
  11. #define ATTEMPTS 3
  12. #define NO 0
  13. #define YEP 1
  14.  
  15. void checkadmin(void);
  16. void get_guess(char *str, char *guess);
  17. void get_password(char *password);
  18.  
  19. int main(int argc, char *argv[]){
  20.  
  21. fflush(stdout);
  22. checkadmin();
  23. return EXIT_SUCCESS;
  24. }
  25.  
  26. void checkadmin(void){
  27. char guess[PASS_SIZE + 1];
  28. char password[PASS_SIZE];
  29. char response[100];
  30. get_password(password);
  31.  
  32. printf("What's the password?\n");
  33. printf(password);
  34.  
  35. int i = 0;
  36. int correct = NO;
  37. for (i = 0; i < ATTEMPTS; ++i) {
  38. get_guess("Password:", guess);
  39. sprintf(response, "You guessed: %s\n", guess);
  40. fprintf(stdout, response);
  41. if (strncmp(guess, password, PASS_SIZE) == 0){
  42. correct = YEP;
  43. break;
  44. }else{
  45. printf("Incorrect! Atempts remaining: %d\n", ATTEMPTS - i -1);
  46. }
  47. }
  48.  
  49. if (correct == YEP){
  50. printf("WIN\n");
  51. }else{
  52. printf(":-(\n");
  53. }
  54. fflush(stdout);
  55. }
  56.  
  57.  
  58. void get_password(char *password){
  59. int i = 0;
  60. char c;
  61. FILE *rand = fopen("/dev/urandom", "r");
  62. while (i < PASS_SIZE){
  63. c = getc(rand);
  64. if (((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) && !( c == 'n' || c == 'N' || c == 's' || c == 'S' )){
  65. password[i++] = c;
  66. }
  67. }
  68. }
  69.  
  70. /*
  71. Get a guess from a prompt
  72. */
  73. void get_guess(char *str, char *guess){
  74. int i;
  75. /* Prompt user for input */
  76. printf("%s", str);
  77. fflush(stdout);
  78.  
  79. /* Read user input */
  80. fgets(guess, PASS_SIZE + 1, stdin);
  81.  
  82. /* Ensure that input is valid */
  83. for(i = 0; i < PASS_SIZE; i++){ // skip the possible -ve sign
  84. if( guess[i] == 'n' || guess[i] == 'N' || guess[i] == 's' || guess[i] == 'S' ){
  85. printf("Invalid guess:%s\n", guess);
  86. return get_guess(str, guess);
  87. }
  88. }
  89. guess[PASS_SIZE] = '\0';
  90. return;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement