-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyfiles.c
More file actions
117 lines (103 loc) · 2.59 KB
/
Copy pathmyfiles.c
File metadata and controls
117 lines (103 loc) · 2.59 KB
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
//checks if file exists
#include "stdio.h"
#include "stdlib.h"
#include "limits.h"
#include "unistd.h"
#include "fcntl.h"
#include "myfiles.h"
#include "string.h"
#include "unistd.h"
#include "mywebserver.h"
#include "mylog.h"
//myconfload.c for CSP2308 Unix and C
//William Termini 10344438
//10 April 2015
//Contains functions to load and print files
//Takes files and puts them into memmory
FILE *loadFile(const char filename[4096],const char mode[CHAR_MAX])
{
FILE *file = NULL;
//Check to see if file exists this is checked twice when loading pages unless we are going to write
if( checkFile(filename)!=404)
{
//check permissions
if(checkPrivilege(filename,mode)==200)
{
//GOOD NEWS WE CAN OPEN THE FILE ((>'-')> <('-'<) ^(' - ')^ <('-'<) (>'-')>)
printf("\nOpening File %s!\n\n",filename);
file = fopen(filename,mode);
return file;
}
else
{
//Return Empty File
return file;
}
}
else if(!strcmp(mode,"a"))
{
printf("\nmakeing new file\n");
creat(filename,0666);
file = fopen(filename,"w");
return file;
}
else
{
//Also Returns and Empty File
return file;
}
//Yet again, another empty file (programer had to much monster and its 4:51 AM)
return file;
}
//opens and prints a file to stdout
//check exist
int checkFile(const char *filename)
{
//ONLY CHECK IF FILE EXISTS
if( (access(filename, F_OK)) != -1)
{
printf("\nFile %s exists!\n",filename);
return 1;
}
else
{
printf("File %s can not be found\n",filename);
return 404;
}
};
//check your privilege
int checkPrivilege(const char *filename,const char *mode)
{
char modelong[8];
int result;
if (strcmp(mode,"r")==0)
{
strcpy(modelong,"READ");
result = (access(filename,R_OK));
}
else if ((strcmp(mode,"w")==0)||(strcmp(mode,"a")==0))
{
result = (access(filename,W_OK));
strcpy(modelong,"WRITE");
}
else
{
//user should never, ever ever ever ever see this, if this happens the code is wrong
printf("ERROR: This should never happen!\n");
return -1;
}
//Check the results
if (result!=-1)
{
//File can be read
printf("Sufficent privileges to %s file %s\n",modelong,filename);
return 200;
}
else
{
printf("Insufficent privilege to %s file %s\n",modelong,filename);
return 403;
}
}
//Takes the contents of one file and
int fileSock();