-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsupporting_functions.c
86 lines (75 loc) · 1.53 KB
/
supporting_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
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include"log.h"
#include <semaphore.h>
#include <pthread.h>
pthread_mutex_t lock_requests_mtx = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t lock_size_mtx = PTHREAD_MUTEX_INITIALIZER;
static int total_requests;
static int total_size;
void increment_request()
{
// pthread_mutex_lock(&lock_requests_mtx);
total_requests++;
// pthread_mutex_unlock(&lock_requests_mtx);
}
int show_total_requests()
{
return total_requests;
}
void add_to_total_size(int new_size)
{
// pthread_mutex_lock(&lock_size_mtx);
total_size += new_size;
// pthread_mutex_unlock(&lock_size_mtx);
}
int show_total_size()
{
return total_size;
}
int check_file_exists(const char * path)
{
FILE * fp;
if(fp=fopen(path,"r"))
{
fclose(fp);
return 1;
}
return 0;
}
int file_size(const char *path)
{
if(!check_file_exists(path)) exit(0);
struct stat st;
stat(path,&st);
return st.st_size;
}
int check_folder_exists(const char *path)
{
struct stat st;
if(lstat(path,&st)<0)
{ print_log(ERROR,"ROOT DIRECTORY DOES NOT EXISTS");
return 0;
}
if(S_ISDIR(st.st_mode))
return 1;
}
set_index(char *path)
{
struct stat st;
if(lstat(path,&st)<0)
{ perror("");
return -1;
}
if(S_ISDIR(st.st_mode)) strcat(path,"/index.html");
return 1;
}
void trim_resource(char * resource_location)
{
if(strstr(resource_location,"#")) strcpy(strpbrk(resource_location,"#"),"");
if(strstr(resource_location,"?")) strcpy(strpbrk(resource_location,"?"),"");
}