-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.c
469 lines (437 loc) · 11.6 KB
/
functions.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header_task6_es.h"
int str_n_len_es(const char* string)
{
///Initialize the length
int length = 0;
///Create a while loop to check if the first char is not a zero char
while (*string != '\0') {
string++;
length++;
}
///Return the length from the function
return length;
}
char* str_n_str_es(const char* string, const char* sub_string)
{
///Create a while loop to check when the first char is not equal to a zero char
while (*string != '\0'){
///Create an if statement to check if pointer to string is equal to pointer
///to sub_string and string compare return value
if ((*string == *sub_string) && str_cmp_es(string, sub_string)) {
return (char*)string;
}
string++;
}
return NULL;
}
int str_cmp_es(const char* string1, const char* string2)
{
///Check while there is a pointer to char in string1 and string2
while (*string1 && *string2) {
///Check if pointer to string1 char is not equal to pointer to string2 char
if (*string1 != *string2) {
return 0;
}
string1++;
string2++;
}
return *string2 == '\0';
}
char* str_n_chr_es(const char* string, int character)
{
///Initialize a constant char pointer to NULL
const char* ptr = NULL;
size_t i = 0;
///Create a for loop until a break
for (i = 0; ;i++) {
///Check if the string is equal the character
if ((unsigned char)string[i] == character) {
ptr = &string[i];
break;
}
if (string[i] == '\0') {
return NULL;
}
}
///Retrun a pointer to the character
return (char*)ptr;
}
char* read_stdin(FILE* stream)
{
///Create an initial max size for the buffer to hold
unsigned int BUFF_SIZE = 100;
///Create memory on the heap for the buffer
char* buffer = calloc(BUFF_SIZE + 1, sizeof(char));
char c = '\0';
int i = 0;
///Get the input from stdin
while ((c = fgetc(stream)) != '\0' && !feof(stream)) {
///Increase size if the buffer if full
if (i == (BUFF_SIZE - 1)) {
BUFF_SIZE += BUFF_SIZE;
///Reallocte memory for the buffer if needed
buffer = realloc(buffer, (BUFF_SIZE + 1) * sizeof(char));
}
///Copy the actual read character
*(buffer + (i++)) = c;
}
///Reallocate the memory to the correct size
buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char));
///Ensures that the buffer is null terminated
*(buffer + i) = '\0';
return buffer;
}
char** get_sub_strings(int* count, char* text_line, const char STRING_START[], char** links)
{
const size_t len_link_begin = strlen(STRING_START);
int index = *count - 1;
text_line = strstr(text_line, STRING_START);
while (index >= 0 && text_line) {
///Get the size of the string inside quotes
size_t link_len = next_quote(text_line + len_link_begin + 1) - (text_line + len_link_begin + 1);
///Check if the length is not zero
if (link_len) {
///return a pointer to a new string which is a duplicate of the string
char* link = strndup(text_line + len_link_begin + 1, link_len);
links[index] = link;
index--;
///Find the first occurence of STRING_START
text_line = strstr(text_line + len_link_begin + strlen(link) + 2, STRING_START);
}
else {
printf("Empty link.\n");
text_line = strstr(text_line + len_link_begin, STRING_START);
}
}
return links;
}
unsigned int count_sub_strings(const char* text_line, const char STRING_START[])
{
///Initialize an usigned int (it can never be zero)
unsigned int count = 0;
///Check if not text_line or not String_Start
if (!text_line || !STRING_START) {
return 0;
}
else {
///Find the first occurence of STRING_START in the text_line
text_line = strstr(text_line, STRING_START);
///Get the length of STRING_START
size_t string_start_len = strlen(STRING_START);
/// While there is a pointer to the the text_line
while (text_line && *text_line) {
count++;
///Find the first occurence of STRING_START
text_line = strstr(text_line + string_start_len, STRING_START);
}
}
return count;
}
const char *next_quote(const char text_line[])
{
///Create a loop While the first char in text_line is not equal to a ' " ', a '\'' and '\0'
while(*text_line != '"' && *text_line != '\'' && *text_line != '\0') {
text_line++;
}
///Return the text_line from the function
return text_line;
}
char *read_formatted_input(FILE *stream)
{
/// Initialize a max size for the buffer to hold
unsigned int BUFF_SIZE = 100;
///Create an Initial allocation of the buffer with BUFF_SIZE
char *buffer = malloc( sizeof(char)* BUFF_SIZE + 1);
char c = '\0';
int i = 0;
///Read stdin
while ((c = fgetc(stream)) != EOF && c != '\n') {
///Increase the buffer size if the buffer is full
if (i == (BUFF_SIZE - 1)) {
BUFF_SIZE += BUFF_SIZE;
///Reallocate the memory with a new size tp preserve the data
buffer = realloc(buffer, (BUFF_SIZE + 1) * sizeof(char));
}
///Copy the actual read character
*(buffer + (i++)) = c;
}
///Reallocate the memory to the actual size
buffer = realloc(buffer, (str_n_len_es(buffer) + 1) * sizeof(char));
///Ensures the buffer is null terminated
*(buffer + i) = '\0';
///Check if i is qual to zero before returning the buffer
if(i == 0) {
free(buffer);
buffer = NULL;
}
return buffer;
}
void server_response(char *url)
{
///Start a libcurl easy session
CURL *curl = curl_easy_init();
///Check if curl is not null
if(curl) {
CURLcode res;
///Set the options for the curl easy handle
curl_easy_setopt(curl, CURLOPT_URL, url);
///Url is redirected, so we tell libcurl to follow redirection
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
///Perform the download request without getting the body content
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
///Use curl_easy_perform to perform the entire request
res = curl_easy_perform(curl);
///Check if there is no errors
if(res == CURLE_OK) {
///Create a variable to hold the response code
long response_code;
///Use curl handle to extract information
res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
///Print the url and the resulting response
printf("%s %ld %s\n", url, response_code, response_message(response_code));
}
///Terminate the libcurl easy handle
curl_easy_cleanup(curl);
} else {
printf("An error occured");
exit(EXIT_FAILURE);
}
}
void server_response(char *url)
{
///Start a libcurl easy session
CURL *curl = curl_easy_init();
///Check if curl is not null
if(curl) {
CURLcode res;
///Set the options for the curl easy handle
curl_easy_setopt(curl, CURLOPT_URL, url);
///Url is redirected, so we tell libcurl to follow redirection
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
///Perform the download request without getting the body content
curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
///Use curl_easy_perform to perform the entire request
res = curl_easy_perform(curl);
///Check if there is no errors
if(res == CURLE_OK) {
///Create a variable to hold the response code
long response_code;
///Use curl handle to extract information
res = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
///Print the url and the resulting response
printf("%s %ld %s\n", url, response_code, response_message(response_code));
}
///Terminate the libcurl easy handle
curl_easy_cleanup(curl);
} else {
printf("An error occured");
exit(EXIT_FAILURE);
}
}
char *response_message(long response_code)
{
char *message = NULL;
switch(response_code)
{
///Create information responses
case 100:
message = "Continue";
break;
case 101:
message = "Switching";
break;
case 102:
message = "Procssing";
break;
///Create successful responses
case 200:
message = "OK";
break;
case 201:
message = "Created";
break;
case 202:
message = "Accepted";
case 203:
message = "Non-Authoritative Information";
break;
case 204:
message = "No Content";
break;
case 205:
message = "Reset Content";
break;
case 206:
message = "Partial Content";
break;
case 207:
message = "Multi-Status";
break;
case 208:
message = "Already Reported";
break;
case 226:
message = "IM Used";
break;
///Create redirection responses
case 300:
message = "Multiple Choices";
break;
case 301:
message = "Moved Permanently";
break;
case 302:
message = "Found";
break;
case 303:
message = "See Other";
break;
case 304:
message = "Not Modified";
break;
case 305:
message = "Use Proxy";
break;
case 306:
message = "(Unused)";
break;
case 307:
message = "Temporary Redirect";
break;
case 308:
message = "Permanent Redirect";
break;
///Create client error responses
case 400:
message = "Bad Request";
break;
case 401:
message = "Unauthorized";
break;
case 402:
message = "Payment Required";
break;
case 403:
message = "Forbidden";
break;
case 404:
message = "Not Found";
break;
case 405:
message = "Method Not Allowed";
break;
case 406:
message = "Not Acceptable";
break;
case 407:
message = "Proxy Authentication Required";
break;
case 408:
message = "Request Timeout";
break;
case 409:
message = "Conflict";
break;
case 410:
message = "Gone";
break;
case 411:
message = "Length Required";
break;
case 412:
message = "Precondition Failed";
break;
case 413:
message = "Payload Too Large";
break;
case 414:
message = "URI Too Long";
break;
case 415:
message = "Unsupported Media Type";
break;
case 416:
message = "Range Not Satisfiable";
break;
case 417:
message = "Expectation Failed";
break;
case 421:
message = "Misdirected Request";
break;
case 422:
message = "Unprocessable Entity";
break;
case 423:
message = "Locked";
break;
case 424:
message = "Failed Dependency";
break;
case 425:
message = "Too Early";
break;
case 426:
message = "Upgrade Required";
break;
case 427:
message = "Unassigned";
break;
case 428:
message = "Precondition Required";
break;
case 429:
message = "Too Many Requests";
break;
case 430:
message = "Unassigned";
break;
case 431:
message = "Request Header Fields Too Large";
break;
case 451:
message = "Unavailable For Legal Reasons";
break;
///Create server error responses
case 500:
message = "Internal Server Error";
break;
case 501:
message = "Not Implemented";
break;
case 502:
message = "Bad Gateway";
break;
case 503:
message = "Service Unavailable";
break;
case 504:
message = "Gateway Timeout";
break;
case 505:
message = "HTTP Version Not Supported";
break;
case 506:
message = "Variant Also Negotiates";
break;
case 507:
message = "Insufficient Storage";
break;
case 508:
message = "Loop Detected";
break;
case 509:
message = "Unassigned";
break;
case 510:
message = "Not Extended";
break;
case 511:
message = "Network Authentication Required";
break;
}
return message;
}