-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilereadwrite.c
289 lines (214 loc) · 8.01 KB
/
filereadwrite.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <stdio.h>
#include <string.h>
#define _GNU_SOURCE
#include <stdlib.h>
char **split(char *string, char *seperators, int *count);
char* replaceWord(const char* s, const char* oldW,
const char* newW)
{
char* result;
int i, cnt = 0;
int newWlen = strlen(newW);
int oldWlen = strlen(oldW);
// Counting the number of times old word
// occur in the string
for (i = 0; s[i] != '\0'; i++) {
if (strstr(&s[i], oldW) == &s[i]) {
cnt++;
// Jumping to index after the old word.
i += oldWlen - 1;
}
}
// Making new string of enough length
result = (char*)malloc(i + cnt * (newWlen - oldWlen) + 1);
i = 0;
while (*s) {
// compare the substring with the result
if (strstr(s, oldW) == s) {
strcpy(&result[i], newW);
i += newWlen;
s += oldWlen;
}
else
result[i++] = *s++;
}
result[i] = '\0';
return result;
}
int main( int argc, char *argv[] ){
char *filename = "auth.txt";
int display;
if(argc == 4){
if (strcmp(argv[1],"register")==0){
FILE *fp = fopen(filename, "a");
if (fp == NULL)
{
printf("err 500 %s", filename);
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");
return 0;
}
else{
char string[1000];
if(strcmp(argv[1],"login")==0)
{
char *arg1 = argv[1];
char *email = argv[2];
char *password = argv[3];
FILE *fp = fopen(filename, "r");
char singleLine[150];
char ch;
char* checkthething;
char* checkthethingpassword;
int emailexists = 0;
int passwordexists = 0;
do {
ch = fgetc(fp);
char mystring[100] = "";
char str1[300] = {ch , '\0'};
char s[500] = "";
strcpy(s,str1);
int count_strings = 0;
char **split_strings = split(s, "🇹🇷", &count_strings);
// print out the substrings, which should be each word of the sentence above
for (int i = 0; i < count_strings; i++)
if (strcmp(split_strings[i],email)==0){
emailexists = 1;
}
else{
if (strcmp(split_strings[i],password)==0){
passwordexists = 1;
}
else{
NULL;
}
}
// free the dynamically allocated space for each string
for (int i = 0; i < count_strings; i++)
free(split_strings[i]);
// free the dynamically allocated space for the array of pointers to strings
free(split_strings);
} while (ch != EOF);
if (emailexists == 1)
{
if(passwordexists ==1){
printf("200\n");
}
else{
printf("403p\n");
}
}
if (emailexists==0){
printf("403e\n");
}
fclose(fp);
}
else
{
printf("Error");
}
}
}
// open the file for writing
}
char **split(char *string, char *seperators, int *count)
{
// get the length of the string
int len = strlen(string);
// use count to keep a count of the number of substrings
*count = 0;
// We make one pass of the string to first determine how many substrings
// we'll need to create, so we can allocate space for a large enough array
// of pointer to strings. The variable i will keep track of our current
// index in the string
int i = 0;
while (i < len)
{
// skip over the next group of separator characters
while (i < len)
{
// keep incrementing i until the character at index i is NOT found in the
// separators array, indicating we've reached the next substring to create
if (strchr(seperators, string[i]) == NULL)
break;
i++;
}
// skip over the next group of substring (i.e. non-separator characters),
// we'll use old_i to verify that we actually did detect non-separator
// characters (perhaps we're at the end of the string)
int old_i = i;
while (i < len)
{
// increment i until the character at index i IS found in the separators
// array, indicating we've reached the next group of separator
// character(s)
if (strchr(seperators, string[i]) != NULL)
break;
i++;
}
// if we did encounter non-seperator characters, increase the count of
// substrings that will need to be created
if (i > old_i) *count = *count + 1;
}
// allocate space for a dynamically allocated array of *count* number of
// pointers to strings
char **strings = malloc(sizeof(char *) * *count);
// we'll make another pass of the string using more or less the same logic as
// above, but this time we'll dynamically allocate space for each substring
// and store the substring into this space
i = 0;
// buffer will temporarily store each substring, string_index will keep track
// of the current index we are storing the next substring into using the
// dynamically allocated array above
char buffer[16384];
int string_index = 0;
while (i < len)
{
// skip through the next group of separators, exactly the same as above
while (i < len)
{
if (strchr(seperators, string[i]) == NULL)
break;
i++;
}
// store the next substring into the buffer char array, use j to keep
// track of the index in the buffer array to store the next char
int j = 0;
while (i < len)
{
if (strchr(seperators, string[i]) != NULL)
break;
buffer[j] = string[i];
i++;
j++;
}
// only copy the substring into the array of substrings if we actually
// read in characters with the above loop... it's possible we won't if
// the string ends with a group of separator characters!
if (j > 0)
{
// add a null terminator on to the end of buffer to terminate the string
buffer[j] = '\0';
// calculate how much space to allocate... we need to be able to store
// the length of buffer (including a null terminator) number of characters
int to_allocate = sizeof(char) *
(strlen(buffer) + 1);
// allocate enough space using malloc, store the pointer into the strings
// array of pointers at hte current string_index
strings[string_index] = malloc(to_allocate);
// copy the buffer into this dynamically allocated space
strcpy(strings[string_index], buffer);
// advance string_index so we store the next string at the next index in
// the strings array
string_index++;
}
}
// return our array of strings
return strings;
}