Can someone explain me why strcmp
returns the same value even if passwords are correct/incorrect? I define valid password just below include section and checking it with entered one at the end of my program.
Here's my code:
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>
#define TIME 10
#define MAXPASSWORD 12
#define PASSWORD "pass123"
void sigalrm_handler() {
printf("\nERR: Time is up...\n");
}
int getch() {
struct termios oldtc, newtc;
int ch;
tcgetattr(STDIN_FILENO, &oldtc);
newtc = oldtc;
newtc.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newtc);
ch=getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldtc);
return ch;
}
int main(int argc, char * argv[]) {
char password[MAXPASSWORD] = {0};
printf("Enter correct password. You have %d seconds: ", TIME);
signal(SIGALRM, sigalrm_handler);
alarm(TIME);
fflush(stdout);
for(int i=0; i<MAXPASSWORD; i++)
{
password[i] = getch();
if (password[i] == '\n')
break;
printf("*");
}
if (strcmp(password, PASSWORD) == 0) {
printf("\nValid password\n");
} else {
printf("\nInvalid password\n");
}
}