-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
226 lines (195 loc) · 4.76 KB
/
main.cpp
File metadata and controls
226 lines (195 loc) · 4.76 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
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
#include "cvfs.h"
int main()
{
char str[80] = {'\0'};
char Command[5][20] = {{'\0'}};
char InputBuffer[MAXFILESIZE] = {'\0'};
char *EmptyBuffer = NULL;
int iCount = 0;
int iRet = 0;
StartAuxillaryDataInitialization();
printf("---------------------------------------------------------\n");
printf("---------------CVFS started successfully!----------------\n");
printf("---------------------------------------------------------\n");
// infinite listening shell
while(1)
{
fflush(stdin);
strcpy(str,"");
printf("\n[CVFS]$ ");
fgets(str, sizeof(str), stdin);
iCount = sscanf(str, "%s %s %s %s %s", Command[0], Command[1], Command[2], Command[3], Command[4]);
fflush(stdin);
if(iCount == 1)
{
// [CVFS]$ exit
if(strcmp("exit", Command[0]) == 0)
{
printf("Exiting application...\n");
printf("Deallocating all resources\n"); // todo
break;
}
// [CVFS]$ ls
else if(strcmp("ls", Command[0]) == 0)
{
lsFile();
}
// [CVFS]$ help
else if(strcmp("help", Command[0]) == 0)
{
DisplayHelp();
}
// [CVFS]$ clear
else if(strcmp("clear", Command[0]) == 0)
{
// conditional preprocessing
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
} // end of if 1
else if(iCount == 2)
{
// [CVFS]$ man ls
if(strcmp("man", Command[0]) == 0)
{
DisplayManPage(Command[1]);
}
// [CVFS]$ unlink demo.txt
else if(strcmp("unlink", Command[0]) == 0)
{
iRet = UnlinkFile(Command[1]);
if(iRet == ERR_INVALID_PARAMETER)
{
printf("Error: Invalid parameter!\n");
}
if(iRet == ERR_FILE_NOT_EXIST)
{
printf("Error: Unable to delete. No such file!\n");
}
if(iRet == EXECUTE_SUCCESS)
{
printf("File successfully deleted.\n");
}
}
// [CVFS]$ write 2
else if(strcmp("write", Command[0]) == 0)
{
printf("Enter the data that you want to write: ");
fgets(InputBuffer, MAXFILESIZE, stdin);
// -1 to remove \0
iRet = WriteFile(atoi(Command[1]), InputBuffer, strlen(InputBuffer)-1);
if(iRet == ERR_INVALID_PARAMETER)
{
printf("Error: Invalid parameters.\n");
}
else if(iRet == ERR_FILE_NOT_EXIST)
{
printf("Error: There is no such file.\n");
}
else if(iRet == ERR_PERMISSION_DENIED)
{
printf("Error: Unable to write. No priveleges.\n");
}
else if(iRet == ERR_INSUFFICIENT_SPACE)
{
printf("Error: Unable to write. No space.\n");
}
else
{
printf("%d bytes gets successfully written.\n", iRet);
}
}
else if(strcmp("stat", Command[0]) == 0)
{
iRet = StatFile(Command[1]);
if(iRet == ERR_INVALID_PARAMETER)
{
printf("Error: Invalid parameter\n");
}
else if(iRet == ERR_FILE_NOT_EXIST)
{
printf("Error: File not found\n");
}
}
else
{
printf("There is no such command.\n");
}
} // end of else if 2
else if(iCount == 3)
{
// [CVFS]$ creat Ganesh.txt 3
if(strcmp("creat", Command[0]) == 0)
{
iRet = CreateFile(Command[1], atoi(Command[2]));
if(iRet == ERR_INVALID_PARAMETER)
{
printf("Error: Unable to create the file as parameters are invalid!\n");
printf("Please refer man page.\n");
}
if(iRet == ERR_NO_INODES)
{
printf("Error: Unable to create file as there is no Inode.\n");
}
if(iRet == ERR_FILE_ALREADY_EXIST)
{
printf("Error: Unable to create file. File is already present!\n");
}
if(iRet == ERR_MAX_FILES_OPEN)
{
printf("Error: Unable to create file.\n");
printf("Max opened files limit reached.\n");
}
printf("File gets successfully created with FD %d\n", iRet);
}
// [CVFS]$ read 3 10
else if(strcmp("read", Command[0]) == 0)
{
EmptyBuffer = (char *)malloc(sizeof(atoi(Command[2]))); // might need for \0
iRet = ReadFile(atoi(Command[1]), EmptyBuffer, atoi(Command[2]));
if(iRet == ERR_INVALID_PARAMETER)
{
printf("Error: Invalid parameter.\n");
}
else if(iRet == ERR_FILE_NOT_EXIST)
{
printf("Error: No such file exists.\n");
}
else if(iRet == ERR_PERMISSION_DENIED)
{
printf("Error: Not enough privileges.\n");
}
else if(iRet == ERR_INSUFFICIENT_DATA)
{
printf("Error: Insufficient data.\n");
}
else
{
printf("Read operation successful.\n");
printf("Data from file is\n%s\n", EmptyBuffer);
free(EmptyBuffer);
}
}
else
{
printf("No such command.\n");
}
} // end of else if 3
else if(iCount == 4)
{
} // end of else if 4
else if(strcmp(str, "\n") == 0)
{
continue;
}
else
{
printf("Command not found\n");
printf("Please refer help command for more information\n");
} // end of else
} // end of while
return 0;
}