Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- compile with 'gcc source.c -o q1 -Wall -m32'
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define PASS_SIZE 12
- #define ATTEMPTS 3
- #define NO 0
- #define YEP 1
- void checkadmin(void);
- void get_guess(char *str, char *guess);
- void get_password(char *password);
- int main(int argc, char *argv[]){
- fflush(stdout);
- checkadmin();
- return EXIT_SUCCESS;
- }
- void checkadmin(void){
- char guess[PASS_SIZE + 1];
- char password[PASS_SIZE];
- char response[100];
- get_password(password);
- printf("What's the password?\n");
- printf(password);
- int i = 0;
- int correct = NO;
- for (i = 0; i < ATTEMPTS; ++i) {
- get_guess("Password:", guess);
- sprintf(response, "You guessed: %s\n", guess);
- fprintf(stdout, response);
- if (strncmp(guess, password, PASS_SIZE) == 0){
- correct = YEP;
- break;
- }else{
- printf("Incorrect! Atempts remaining: %d\n", ATTEMPTS - i -1);
- }
- }
- if (correct == YEP){
- printf("WIN\n");
- }else{
- printf(":-(\n");
- }
- fflush(stdout);
- }
- void get_password(char *password){
- int i = 0;
- char c;
- FILE *rand = fopen("/dev/urandom", "r");
- while (i < PASS_SIZE){
- c = getc(rand);
- if (((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) && !( c == 'n' || c == 'N' || c == 's' || c == 'S' )){
- password[i++] = c;
- }
- }
- }
- /*
- Get a guess from a prompt
- */
- void get_guess(char *str, char *guess){
- int i;
- /* Prompt user for input */
- printf("%s", str);
- fflush(stdout);
- /* Read user input */
- fgets(guess, PASS_SIZE + 1, stdin);
- /* Ensure that input is valid */
- for(i = 0; i < PASS_SIZE; i++){ // skip the possible -ve sign
- if( guess[i] == 'n' || guess[i] == 'N' || guess[i] == 's' || guess[i] == 'S' ){
- printf("Invalid guess:%s\n", guess);
- return get_guess(str, guess);
- }
- }
- guess[PASS_SIZE] = '\0';
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement