-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCGI.c
239 lines (215 loc) · 6.91 KB
/
CGI.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
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <mysqll.h>
#include <stdlib.h>
#include <errno.h>
#define CHUNK 1024
struct connection
{
char server[256];
char user[256];
char password[256];
char database[256];
};
char **linesSeperateBySemicolon(char **linesOfText, char *buffer, short *lineCount)
{
short i;
short startPoint = 0;
short count = 0;
for (i = 0; i <= strlen(buffer); i++)
{
if (buffer[i] == ';' || buffer[i] == '\0')
{
linesOfText = (char **)realloc(linesOfText, (count + 1) * sizeof(char *));
if (linesOfText == NULL)
{
perror("Memory allocation patladı 1");
exit(3);
}
linesOfText[count] = (char *)malloc((i - startPoint + 1) * sizeof(char));
if (linesOfText[count] == NULL)
{
perror("Memory allocation patladı 2");
exit(3);
}
strncpy(linesOfText[count], buffer + startPoint, i - startPoint);
linesOfText[count][i - startPoint] = '\0';
startPoint = i + 1;
count++;
}
}
*lineCount = count;
return linesOfText;
}
char **wordsSeperatedBySpace(char **words, char *buffer, short *wordCount)
{
short i;
short startPoint = 0;
short count = 0;
for (i = 0; i <= strlen(buffer); i++)
{
if ((buffer[i] == ' ' && buffer[i + 1] != ' ')|| buffer[i] == '\0')
{
words = (char **)realloc(words, (count + 1) * sizeof(char *));
if (words == NULL)
{
perror("Memory allocation failed 3");
exit(3);
}
words[count] = (char *)malloc((i - startPoint + 1) * sizeof(char));
if (words[count] == NULL)
{
perror("Memory allocation failed 4");
exit(3);
}
strncpy(words[count], buffer + startPoint, i - startPoint);
words[count][i - startPoint] = '\0';
startPoint = i + 1;
count++;
}
}
*wordCount = count;
return words;
}
int main(int argc, char **argv)
{
char buffer[CHUNK];
char logicBuffer[1024];
char exampleBuffer[] = "DENEMELİK TEXT DOLAR SIGN $MERHABA$ BAKALIM OLCAK MI??";
char insertBuffer[] = "NABER";
int insertBufferLength = strlen(insertBuffer);
int lengthExample = strlen(exampleBuffer);
short i;
short j;
int fd = open("index.html", O_RDONLY);
int fileSize = lseek(fd, (off_t)0, SEEK_END);
lseek(fd, (off_t)0, SEEK_SET);
if (read(fd, buffer, fileSize) < 0)
perror("Something happened while reading");
buffer[fileSize] = '\0';
short indexInterval;
short count = 0;
short countLogic = 0;
short startPoint = -1;
short endPoint = -1;
short startPointLogic = -1;
short endPointLogic = -1;
struct connection connection1 = {"localhost", "root", "Erayubuntu82889**",
"bruh"};
MYSQL *conn1;
MYSQL *conn2;
char *server1 = connection1.server;
char *user1 = connection1.user;
char *password1 = connection1.password;
char *database1 = connection1.database;
conn1 = mysql_init(NULL);
if (!mysql_real_connect(conn1, server1, user1, password1, database1, 0, NULL,
0))
{
printf("bruh error for conn1 %s\n", mysql_error(conn1));
exit(1);
}
for (i = 0; i < strlen(buffer); i++)
{
if (buffer[i] == '&')
{
countLogic++;
switch (countLogic)
{
case 1:
startPointLogic = i;
break;
case 2:
endPointLogic = i;
indexInterval = endPointLogic - startPointLogic - 1;
strncpy(logicBuffer, &buffer[startPointLogic + 1], indexInterval);
logicBuffer[indexInterval] = '\0';
printf("Logic buffer is %s\n", logicBuffer);
char **linesOfText = NULL;
short lineCount;
linesOfText = linesSeperateBySemicolon(linesOfText, logicBuffer, &lineCount);
printf("Here is the first line %s\n", linesOfText[0]);
for (j = 0; j < lineCount; j++)
{
char **wordsOfLine = NULL;
short wordCount;
wordsOfLine = wordsSeperatedBySpace(wordsOfLine, linesOfText[j], &wordCount);
printf("Line %d:\n", j + 1);
for (short k = 0; k < wordCount; k++)
{
printf("Word %d: %s\n", k + 1, wordsOfLine[k]);
}
if (strcmp(wordsOfLine[1], "mysql") == 0)
{
}
else if (strcmp(wordsOfLine[1], "post") == 0)
{
printf("Post activiation");
} else {
printf("bruh");
}
for (short k = 0; k < wordCount; k++)
{
free(wordsOfLine[k]);
}
free(wordsOfLine);
}
for (j = 0; j < lineCount; j++)
{
free(linesOfText[j]);
}
free(linesOfText);
countLogic = 0;
default:
break;
}
}
if (buffer[i] == '$')
{
count++;
switch (count)
{
case 1:
startPoint = i;
break;
case 2:
endPoint = i;
indexInterval = endPoint - startPoint;
for (j = 0; j < insertBufferLength; j++)
{
if (insertBuffer[j] != '\0')
{
buffer[j + startPoint] = insertBuffer[j];
}
}
memmove(&buffer[startPoint + insertBufferLength], &buffer[endPoint + 1], fileSize - endPoint);
fileSize = fileSize - indexInterval + insertBufferLength;
buffer[fileSize] = '\0';
break;
default:
break;
}
}
}
// printf("File: %s\n", buffer);
}
/*
TO DO:
-----------
-able to read LOGIC section
-----------
-seperating it by line ';'
-seperating lines by white space ' '
-deciding on command chose [0]
-deciding on flags [1] - ex:whether targeted element should be looped
-variable name decided between $$ [2]
-command itself [3]
potential commands: variable, mysql, POST, url_read
potential flags: mysql -> loop through div
-create a dynamic array that stores the variable and corresponding results
-reading through variables
-inserting values to the variables according to array created
*/