-
Notifications
You must be signed in to change notification settings - Fork 1
/
r.c
163 lines (144 loc) · 5.44 KB
/
r.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
#include <string.h>
#include <stdio.h>
int inFile ( char * , FILE * );
void resetToken ( char * , int );
int main ( int argc , char * argv )
{
// let browser know we will write html
printf ( "Content-Type: text/html\n\n" );
// Gets the form data from the shell var QUERY_STRING
char * data = getenv ( "QUERY_STRING" );
// so just use one string and check it against the file users.txt
// loop through form and check that current string ie token is not present in the file
// write to file
// open file
FILE * users;
FILE * toAdd; // pointer for appending
FILE * friends;
int firstToken = 0; // flag to let us know this is the username we need to add to the friends.txt file
users = fopen ( "../users.txt" , "r" ); // r for read. a for append. PARENT DIR
// need length of string to know howlong to loop for
int length = strlen ( data );
char token[50]; // will contain the string from one of the 4 fields. always terminates with '\0' because of resetToken function
resetToken ( token , 50 );
int i = 0; // counter for index in while
int isToken = 1; // flag for a token
int tIndex = 0; // counter for index in token
while ( i <= ( length + 1 )) // stops at end of string +1 because we need a final loop to check the last token. +1 includes the '\0'
{
if ( data[i] == '=' ) // reached field input
{
isToken = 0;
tIndex = 0;
}
else if (( data[i] == '&' ) || ( data [i] == '\0' )) // field entry ended at & or end of string
{
isToken = 1; // reached end of field input. by field input I mean what the user entered in the text box in the form
token[tIndex] = '\0'; // terminate token
// Now we check if the token is in the file
// if in file
if ( inFile ( token , users ) == 0 )
{
//display error
FILE * registrationError = fopen ( "../registerError.html" , "r" ); // html is in parent dir!!!
char c = getc ( registrationError );
while ( c != EOF )
{
putchar ( c ); // write each char from file to client browser
c = fgetc ( registrationError ); // get the next character
}
return 0; // break program
}
else // append to file. We can append the tokens in order because thats the order we read them in. uname|passw|fulln|jobdes
{
// append to file with puts?
toAdd = fopen ( "../users.txt" , "a" ); // PARENT DIR
// need to change '\0' to '\n' so fputs formats file correctly
token[tIndex] = '\n'; // NOTE that it was '\0' for inFile function
fputs ( token , toAdd ); // WHY DID THIS WORK?? I tried it on a hunch but don't totally understand
fclose ( toAdd ); // close file we just wrote to
// we need to update friends.txt with the new username
// there is a flag that lests us know we are at first token from the form
firstToken ++;
if ( firstToken == 1 )
{
// also we don't want to add duplicates to friends.txt if the user registers twice
friends = fopen ( "../friends.txt" , "a" ); // parent dir. a for appending
if ( inFile ( token , friends ) != 0 ) // username not in friends.txt
{
token[tIndex] = ' '; // we need to add the token to friends.txt with space so dashboard.py can display
// status correctly
fputs ( token , friends ); // appends the token to the end of friends.txt
}
fclose ( friends ); // close friends.txt
}
}
}
else if ( isToken == 0 )
{
token[tIndex] = data[i];
tIndex ++; // fill next position
}
else // reset token so that we always write a string terminating '\0'
{
resetToken( token , 50 ); // populates token array with '\0'
}
i ++; // index to loop through data
}
// if we make it through the above while that means we appended to the users.txt file
// now we display the success html page
FILE * registrationSuccess;
registrationSuccess = fopen ( "../registerSuccess.html" , "r" ); // html file is above dir where cgi runs......!!!
if ( registrationSuccess == NULL )
printf ( "<h1>ERROR</h1>" );
int c = fgetc ( registrationSuccess );
while ( c != EOF )
{
printf ( "%c" , c ); // write each char from file to client browser
c = fgetc ( registrationSuccess ); // get the next character
}
return 0;
}
// function to take an array an make every index '\0'void
void resetToken ( char * input , int size )
{
int i = 0 ;
for ( i; i < size; i ++ )
{
input[i] = '\0';
}
}
// function to check if a string is in the file
// FILE should be a column of username|password|fullname|jobdescripton where | is newline
int inFile ( char * token , FILE * file )
{
// so we will read each word into a buffer. We read into the buffer if the current char is between 33 and 122 ascii or between ! and z
// once we reach a space ie 32 or something out of our range we terminate the buffer with '\0'
// then we strcmp buffer with token and if it matches token is in file
// return 0
//else
// return 1
char c = fgetc ( file );
char buffer[50]; // holds current word
int bCounter = 0; // bufferCounter index
while ( c != EOF )
{
if (( c > 32 ) && ( c < 123 )) // read words into buffer
{
buffer[bCounter] = c;
bCounter ++;
}
else // terminate buffer and compare token to buffer
{
buffer[bCounter] = '\0'; // already at next index since bCounter ++ at last valid char
//-----------compare now that buffer full------------------//
if ( strcmp ( token , buffer ) == 0 )
{
return 0;
}
bCounter = 0; // need to reset
}
c = fgetc ( file ); // need to check first character of file
}
return 1; // token not in file
}