This repository has been archived by the owner on Jan 6, 2020. It is now read-only.
/
Credentials.c
111 lines (83 loc) · 1.94 KB
/
Credentials.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "Credentials.h"
char* getfield(char* line, int num)
{
char* tok;
for (tok = strtok(line, ";");
tok && *tok;
tok = strtok(NULL, ";\n"))
{
if (!--num)
return tok;
}
return NULL;
}
int read_file(char* name, char* password)
{
FILE* fp;
char* line = NULL;
char* line_tmp = NULL;
char* cred_concat = NULL;
size_t len = 0;
ssize_t read;
// test_space(name);
// test_space(password);
fp = fopen("users.csv", "r");
if (fp == NULL){
printf("exit can't open file\n");
exit(EXIT_FAILURE);
}
char* t = NULL;
while ((read = getline(&line, &len, fp)) != -1) {
//printf("Retrieved line of length %zu :\n", read);
printf("%s\n", line);
if(
(test_credential_password(extract_password(line), password) == 0) &&
(test_credential_name(extract_name(line), name)) == 0)
{
//good login and password
//printf("good %s : %s\n", name, password );
fclose(fp);
if (line)
free(line);
free(cred_concat);
return 0;
}
}
fclose(fp);
if (line)
free(line);
free(cred_concat);
return 1;
}
int test_credential_name(char* name_file, char* name_receive){
if(strcmp(name_file, name_receive) == 0){
return 0;
}
return 1;
}
int test_credential_password(char* password_file, char* password_receive){
if(strcmp(password_file, password_receive) == 0){
return 0;
}
return 1;
}
char* extract_name(char* line){
char* t = getfield(line, 1);
return t;
}
char* extract_password(char* line){
char* t = getfield(line, 2);
return t;
}
char* get_login(){
char* login = malloc(sizeof(255));
printf("login: ");
scanf("%s", login);
return login;
}
char* get_password(){
char* password = malloc(sizeof(255));
printf("password: ");
scanf("%s", password);
return password;
}