#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check_authentication(char* password) {
int auth_flag = 0;
char* password_buffer;
char* dept;
password_buffer = (char*)malloc(16);
dept = (char*)malloc(10);
printf("Your department?");
fgets(dept, 10, stdin); //line 11
strcpy_s(password_buffer, 16, password); //line 12
if (strcmp(password_buffer, "AsiaPacificInst") == 0) {
if (strcmp(dept, "NSF") == 0) {
auth_flag = 1;
}
}
if (strcmp(password_buffer, "AsiaPacificUni") == 0) {
if (strcmp(dept, "TM") == 0) {
auth_flag = 1;
}
}
return auth_flag;
}
int main(int argc, char* argv[]) {
char errmsg[512];
char outbuf[512];
char user[20];
printf("Username: ");
fgets(user, 20, stdin); //line 32
if (strcmp(user, "Adm1n") == 0) {
printf("Authorised User\n"); sprintf_s(errmsg, "Authorised User %400s", user); sprintf_s(outbuf, errmsg); //line 34
if (argc < 2)
{
printf("Usage: %s <password>\n", argv[0]); exit(0);
}
if (check_authentication(argv[1]))
{
printf("\n-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
printf(" Access Granted.\n");
printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
}
else {
printf("\n-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
printf("\nAccess Denied.\n");
printf("\n-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
}
}
else { printf("Unauthorised User!!\n"); exit(0); }
}
I need help on to check whether the set of codes below has been written in the correct way as I'm not familiar with C++.
- fgets (line 11 and 32)
- strcpy_s (line 12)
- sprintf_s (line 34)
Because these line of codes had errors earlier when I got them from another source. However, I fixed those error yet on runtime the program did not work properly. The program actually should request for a username and password and verify whether the user is authorized using the username and verify the user's department using their password. However, I could only enter the username when I run the program. It did not request me a password. Also in overall is there any other issue that may cause the program to not run properly.