/
fileIO.c
278 lines (232 loc) · 7.08 KB
/
fileIO.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//function prototypes
int countlines (char*);
void writeToFile (char*, char*);
void newUser(char*, char*);
int fileExists(char* filename);
int searchFile (char*, int);
void loginFunc(char*, char*, char*, int*, char*);
void HUD(int, char*);
int hashification(char*);
char* itoa(int, char*, int);
//ident 1 = username, ident 2 = password
//return 1 if string found, 0 if not
//made an int to accomodate future possibility of >2 search identifiers
int searchFile (char* string, int ident) {
FILE* fp;
fp = fopen("USERS.txt", "r");
char temp[512];
//username handling
if (ident == 1) {
while(fgets(temp, 512, fp) != NULL) {
if(strstr(temp, string)) {
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
//password handling
else if (ident == 2) {
if (strstr(temp,string) != NULL) {
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
//counts the number of lines or "records" in 'filename'
int countlines (char* filename){
FILE* fp;
fp = fopen(filename, "r");
int nlines = 0;
char c;
c = fgetc(fp);
//checks for new line character to determine number of entries
while (c != EOF){
c = fgetc(fp);
if(c == '\n') nlines++;
}
return nlines;
}
//appends 'string' to 'filename'
void writeToFile (char* filename, char* string){
FILE* fp;
fp = fopen(filename, "a");
fputs(string, fp);
fputs("\n", fp);
fclose(fp);
}
char* itoa(int value, char* result, int base) {
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while(ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
//adds a new user record to 'filename'
void newUser(char* filename, char* exitString){
char input1[50];
char input2[50];
char finalWrite[100] = "";
do {
printf("Enter Username:\n");
scanf("%s", input1);
if (searchFile(input1, 1) == 1) {
printf("\nUsername arleady exists, try again? y/n\n");
scanf("%s", input1);
}
else {
strcat(finalWrite, input1);
do{
printf("Enter Password:\n");
scanf("%s", input1);
printf("Verify Password:\n");
scanf("%s", input2);
} while(strcmp(input1, input2) != 0);
strcat(finalWrite, ",");
itoa(hashification(input1), input1, 10);
strcat(finalWrite, input1);
//count amount of current records in file and increments by 1 for new record
int numRecords = countlines(filename) + 1;
//convert username, user count, password to USERS.txt format
sprintf(input1, "%i,", numRecords);
strcat(input1, finalWrite);
writeToFile(filename, input1);
}
} while(strcmp(input1, exitString) == 0);
}
//checks if file exists
int fileExists(char* filename) {
FILE* fp;
if (fp == fopen(filename, "r")) {
fclose(fp);
return 1;
}
else return 0;
}
void loginFunc(char* userInput1, char* userInput2, char* exitString, int* loginStatus, char* username) {
printf("Enter Username:\n");
scanf("%s", userInput1);
strcpy(username, userInput1);
// encapsulate in commas to prevent password AND user# false positives
strcat(userInput1, ",");
char newString[5] = ",";
strcat(newString,userInput1);
//copy username in separate variable for repeated password attempts to not fuck it up
char newStringCopy[20];
strncpy(newStringCopy, newString, 20);
//search file with 1 as string type identifier = ,username,
if (searchFile(newString, 1) == 1) {
do{
//reset exitString comparator
strncpy(userInput2, exitString, 2);
//reset newStringCopy to original string for failed attempts
strncpy(newStringCopy, newString, 20);
printf("Username Found. Enter Password:\n");
scanf("%s", userInput1);
itoa(hashification(userInput1), userInput1, 10);
//append password to login string for search
strcat(newStringCopy,userInput1);
//search file with 2 as string type identifier = ,username,password
if (searchFile(newStringCopy, 2) == 0) {
printf("Invalid Username/Password Combo\nTry again? y/n\n");
scanf("%s", userInput2);
}
else {
printf("Login Successful af\n");
*loginStatus += 1;
//break the loop
strcat(userInput2, "hi");
}
} while(strcmp(userInput2, exitString) == 0);
}
else if (searchFile(newString, 1) == 0) {
printf("Invalid Username. Returning to Menu\n");
}
}
//create function prototype
void HUD (int loginStatus, char* username){
if (loginStatus == 0) {
printf("\n<Not Logged In>\n(1) Create File\n(2) Create User\n(3) Login\n(9) Debug\n(0) Exit\n");
}
else {
printf("\n<%s> Logged In\n(1) Create File\n(2) Create User\n(3) Log Off\n(4) Messages\n(9) Debug\n(0) Exit\n", username);
}
}
int hashification(char* input1){
int hashAddress = 0;
int counter;
for (counter = 0; input1[counter]!='\0'; counter++){
hashAddress = input1[counter] + (hashAddress << 6) + (hashAddress << 16) - hashAddress;
}
return hashAddress;
}
int main(){
int running = 1;
int loginStatus = 0;
char userInput1[25];
char userInput2[25];
char exitString[5] = "y";
char username[25];
char c;
FILE* fp;
while(running) {
HUD(loginStatus, username);
scanf("%s", userInput1);
//create file
if (userInput1[0] == '1') {
printf("Enter New File Name\n");
scanf("%s", userInput1);
if (fileExists(userInput1) == 0) {
printf("Creating New File. Enter file content: %s\n", userInput2);
scanf("%s", userInput2);
writeToFile(userInput1, userInput2);
}
else {
printf("File already Exists. Returning to Main Menu\n");
}
}
//create user
else if (userInput1[0] == '2') newUser("USERS.txt", exitString);
//login or logoff
else if (userInput1[0] == '3') {
if (loginStatus == 0) {
loginFunc(userInput1, userInput2, exitString, &loginStatus, username);
}
else if (loginStatus == 1) {
loginStatus = 0;
printf("\n%s Successfully Logged off\n", username);
}
}
//messages (if logged in only)
else if (userInput1[0] == '4' & loginStatus ==1) {
printf("this be placeholder\n");
}
//debug
else if (userInput1[0] == '9') {
scanf("%s",userInput1);
printf("%d", hashification(userInput1));
}
//exit
else if (userInput1[0] == '0') running = 0;
}
return 0;
}