-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPLFS.c
140 lines (129 loc) · 3.49 KB
/
PLFS.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
#include "PLFS.h"
// DEBUG : gcc -fstack-protector-all -ggdb3 PLFS.c -o PLFS
// PROD : gcc -fstack-protector-all -s PLFS.c -o PLFS && patchelf --set-rpath ./libs --set-interpreter ./libs/ld-linux-x86-64.so.2 PLFS
char *filenames[NB_FILES_MAX] = {0};
char *contents[NB_FILES_DEFAULT] = {CONTENT0, CONTENT1, CONTENT2, CONTENT3};
int main(int argc, char **argv)
{
setbuf(stdin, NULL);
setbuf(stdout, NULL);
initfs();
puts(HELLO);
puts(HELPTEXT);
char input[INPUT_MAX_LEN];
char *in2;
while (1)
{
in2 = input;
printf("\n> ");
if (fgets(input, INPUT_MAX_LEN, stdin) == NULL)
{
perror("fgets");
exit(1);
}
input[strcspn(input, "\n")] = 0;
char *command = strsep(&in2, " ");
if (strcmp(command, EXIT) == 0)
{
break;
}
parse(command, in2);
}
return 0;
}
void initfs()
{
filenames[0] = FILE0;
filenames[1] = FILE1;
filenames[2] = FILE2;
filenames[3] = FILE3;
}
__always_inline void parse(char *command, char *argument)
{
if (strcmp(command, HELP) == 0)
{
puts(HELPTEXT);
return;
}
if (strcmp(command, LS) == 0 && argument == NULL)
{
for (int i = 0; i < NB_FILES_MAX; i++)
{
if (filenames[i])
{
puts(filenames[i]);
}
}
return;
}
if (argument != NULL)
{
if (strcmp(command, PUT) == 0)
{
int i = 0;
while (i < NB_FILES_MAX && filenames[i])
{
i++;
}
if (i < NB_FILES_MAX)
{
filenames[i] = (char *)malloc(strlen(argument) + 1);
strcpy(filenames[i], argument);
return;
}
else
{
puts("Le serveur est plein. Opération annulée.");
return;
}
}
if (strcmp(command, GET) == 0)
{
for (int i = 0; i < NB_FILES_DEFAULT; i++)
{
if (strcmp(filenames[i], argument) == 0)
{
printf("Contenu du fichier : ");
printf(contents[i]);
return;
}
}
puts("Le fichier spécifié n'a pas été trouvé ou n'a pas de contenu.");
return;
}
if (strcmp(command, LS) == 0)
{
int found = 0;
for (int i = 0; i < NB_FILES_MAX; i++)
{
if (filenames[i] && *argument && strstr(filenames[i], argument) == filenames[i])
{
printf(filenames[i]);
printf("\n");
found = 1;
}
}
if (!found)
puts("Le fichier spécifié n'a pas été trouvé.");
return;
}
if (strcmp(command, DEL) == 0)
{
for (int i = NB_FILES_DEFAULT; i < NB_FILES_MAX; i++)
{
if (filenames[i] && (strcmp(filenames[i], argument) == 0))
{
free(filenames[i]);
filenames[i] = NULL;
puts("Fichier supprimé.");
return;
}
}
puts("Le fichier spécifié n'a pas pu être supprimé.");
return;
}
}
puts(ERROR);
puts(HELPTEXT);
explicit_bzero(command, INPUT_MAX_LEN);
}