-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.c
100 lines (83 loc) · 2.26 KB
/
auth.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
#include <stdio.h>
#include <string.h>
void split(char *inputArr, char **outputArr, char *delim)
{
char *temp;
temp = strtok(inputArr, delim);
for (int i = 0; temp != NULL; i++)
{
outputArr[i] = temp;
temp = strtok(NULL, delim);
}
}
int main(int argc, char **argv)
{
/* check for proper arguments */
if (argc == 4)
{
if (strcmp(argv[1], "register") == 0)
{
FILE *fp = fopen("auth.txt", "a");
if (fp == NULL)
{
printf("err 500 %s", "auth.txt");
return -1;
}
char *arg1 = argv[1];
char *email = argv[2];
char *password = argv[3];
fprintf(fp, "%s🇹🇷%s\n", email, password);
fclose(fp);
printf("200\n");
return 0;
}
if (strcmp(argv[1], "login") == 0)
{
/*---------main code starts here----------*/
FILE *myScriptFile;
myScriptFile = fopen("auth.txt", "r");
/* read txt file and split into array like java split() */
int bufferLen = 100;
char buffer[bufferLen];
char *splitArr[100];
int passed = 0;
while (fgets(buffer, bufferLen, myScriptFile) != NULL)
{
split(buffer, splitArr, "🇹🇷");
if (strcmp(argv[2], strtok(splitArr[0], "\n")) == 0)
{
/* first credential passed */
if (strcmp(argv[3], strtok(splitArr[1], "\n")) == 0)
{
/* password passed */
passed = 1;
break;
}
else
{
passed = 2;
break;
}
}
}
fclose(myScriptFile);
if (passed == 1)
{
printf("200\n");
}
if (passed == 2)
{
printf("403p\n");
}
if (passed == 0)
{
printf("403e\n");
}
}
}
else
{
printf("");
}
return 0;
}