-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.c
More file actions
660 lines (620 loc) · 17.7 KB
/
Code.c
File metadata and controls
660 lines (620 loc) · 17.7 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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
// This is a basic file system with a smooth code flow, does have basic file operations, but is only bound for a single run
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct docs
{
FILE *address;
char file_name[30];
long int size;
int blocks;
char owner[6];
int permission;
};
int perm_num = 1; // to represent who is logged on.
struct docs doc[10];
int permission[8][3] = {{0, 0, 0},
{0, 0, 1},
{0, 1, 0},
{0, 1, 1},
{1, 0, 0},
{1, 0, 1},
{1, 1, 0},
{1, 1, 1}};
void modify_file(char *, int);
void login()
{
char name[10], password[10];
int auth;
printf("\nHit 1 to login or 2 for a guest session: \n");
scanf("%d", &auth);
switch (auth)
{
case 1:
printf("ID: ");
scanf("%s", name);
printf("Password: ");
scanf("%s", password);
if (!((strcmp(name, "user") == 0 || strcmp(name, "group") == 0) && (strcmp(password, "123") == 0)))
{
printf("Invalid login details");
exit(0);
}
else
{
printf("Login Successful");
if (strcmp(name, "group") == 0)
{
perm_num = 2;
}
else
{
perm_num = 1;
}
}
break;
case 2:
printf("\nGuest Session\n____________________________");
perm_num = 3;
break;
default:
printf("\nInvalid");
exit(0);
break;
}
return;
}
int validate(int num)
{
if (num == 0)
{
return 0;
}
int temp = num, len = 0, flag = 1;
while (num > 0)
{
if (num % 10 > 8)
{
flag = 0;
break;
}
num = num / 10;
len++;
}
if (len == 3 && flag == 1)
{
return 0;
}
return -1;
}
//returns row number for permission matrix
int pos_of_perm(int loc)
{
int digit;
if (perm_num == 1)
{
digit = (doc[loc].permission) / 100;
}
else
{
if (perm_num == 2)
{
digit = ((doc[loc].permission) / 10) % 10;
}
else
{
digit = (doc[loc].permission) % 10;
}
}
return digit;
}
int used_blocks()
{
int i = 0, res = 0;
while (doc[i].address != NULL)
{
res += doc[i].blocks;
i++;
}
return res;
}
void trimLeading(char *str)
{
int index = 0, i;
while (str[index] == ' ' || str[index] == '\t' || str[index] == '\n')
{
index++;
}
if (index != 0)
{
i = 0;
while (str[i + index] != '\0')
{
str[i] = str[i + index];
i++;
}
str[i] = '\0';
}
}
int file_number(char *name)
{
int index = -1, i = 0;
while (strcmp(doc[i].file_name, "") != 0)
{
if (strcmp(doc[i].file_name, name) == 0)
{
index = i;
break;
}
i += 1;
}
free(name);
return index;
}
void create_file()
{
if (perm_num == 3)
{
printf("\nYou cannot create a file in guest session.\n");
return;
}
char name[30];
int choice;
printf("Enter the name of the file to be created along with its extension:");
scanf("%s", name);
int loc = 0;
if (file_number(name) == -1)
{
while (doc[loc].address != NULL)
loc += 1;
if (used_blocks() <= 400)
{
FILE *fp = fopen(name, "w");
if (fp != NULL)
{
printf("\nFile created successfully.");
doc[loc].address = fp;
strcpy(doc[loc].owner, "user");
doc[loc].permission = 764;
strcpy(doc[loc].file_name, name);
printf("\nWould you like to add text to the file right away(Enter 1 to answer in the affirmative): ");
scanf("%d", &choice);
if (choice == 1)
{
fclose(fp);
modify_file(name, loc);
}
}
else
{
printf("Could not create file.");
}
fclose(fp);
}
else
{
printf("Memory Insufficient, for file creation.");
return;
}
}
else
{
printf("File with that name already exists. Procedure called once again\n");
create_file();
}
}
void read_file()
{
char name[30];
char ch;
printf("Enter the complete name of the file you want to read.");
scanf("%s", name);
int loc = file_number(name);
if (loc == -1)
{
int option;
printf("File does not exist. Would you like to create it now(Enter 1 to answer in the affirmative): ");
scanf("%d", &option);
if (option == 1)
{
create_file();
}
else
{
return;
}
}
else
{
int row = pos_of_perm(loc);
if (permission[row][0] == 0)
{
printf("You dont have the permission to read this file.\n");
return;
}
else
{
FILE *fp = fopen(name, "r");
ch = fgetc(fp);
while (ch != EOF)
{
printf("%c", ch);
ch = fgetc(fp);
}
fclose(fp);
return;
}
}
}
void modify_file(char *name, int loc)
{
if (name != NULL && loc != -1)
{
int row = pos_of_perm(loc);
if (permission[row][2] == 0)
{
printf("You don't have the permission to modify this file.\n");
return;
}
FILE *fp = fopen(name, "w");
long int size = 0;
int blocks = 0, i = 0;
char *string = (char *)malloc(409600);
printf("Enter the text to be inserted.(Tab, then return to stop reading): ");
scanf("%[^\t]s", string);
trimLeading(string);
if (used_blocks() * 1024 + strlen(string) >= 409600)
{
printf("\nInsufficient memory to insert the given string.");
return;
}
fputs(string, fp);
fseek(fp, 0, SEEK_END);
size = ftell(fp);
doc[loc].size = size;
while (size > 0)
{
blocks++;
size -= 1024;
}
doc[loc].blocks = blocks;
fclose(fp);
free(string);
printf("\nText inserted successfully.");
}
else
{
char name[30];
printf("Enter the full name of the file to be modified.");
scanf("%s", name);
loc = file_number(name);
if (loc == -1)
{
printf("No such file exists.");
return;
}
else
{
int row = pos_of_perm(loc);
if (permission[row][2] == 0)
{
printf("You don't have the permission to modify this file\n");
return;
}
int choice = 1;
printf("Enter 1 for overwriting the file or 2 for appending into it: ");
scanf("%d", &choice);
int blocks = 0, i = 0;
FILE *fp;
if (choice == 1)
{
fp = fopen(name, "w");
}
else
{
fp = fopen(name, "a");
}
char *string = (char *)malloc(409600);
printf("Enter the text to be inserted.(Tab, then return to stop reading):");
scanf("%[^\t]s", string);
fseek(fp, 0, SEEK_SET);
trimLeading(string);
if (used_blocks() * 1024 + strlen(string) >= 409600)
{
printf("\nInsufficient memory to insert the given string.");
return;
}
fputs(string, fp);
printf("Modification Successful\n");
fseek(fp, 0, SEEK_END);
long int size = ftell(fp);
doc[loc].size = size;
while (size > 0)
{
blocks++;
size -= 1024;
}
doc[loc].blocks = blocks;
fclose(fp);
}
}
}
void rename_file()
{
char name[30], new_name[30];
printf("Enter the complete name of the file you want to rename: ");
scanf("%s", name);
int loc = file_number(name);
if (loc != -1)
{
int row = pos_of_perm(loc);
if (permission[row][2] == 0)
{
printf("You don't have the permission to rename this file\n");
return;
}
printf("Enter the new name of the file: ");
scanf("%s", new_name);
int i = 0;
while (doc[i].address != NULL)
{
if (strcmp(doc[i].file_name, new_name) == 0)
{
printf("\nFile with this name already exists. Returning to menu....");
return;
}
i++;
}
strcpy(doc[loc].file_name, new_name);
int res = rename(name, new_name);
if (res == 0)
{
printf("\nRename Successful");
}
else
{
printf("\nSorry, the file could not be renamed.");
}
return;
}
else
{
printf("No file with the given name was found (Did you enter the name along with it's extension?)");
return;
}
}
void delete_file()
{
char name[30];
printf("\nEnter the complete name of the file you want to delete: ");
scanf("%s", name);
int loc = file_number(name);
if (loc != -1)
{
int row = pos_of_perm(loc);
if (permission[row][2] == 0)
{
printf("You don't have the permission to delete this file\n");
return;
}
int status = remove(name);
if (status == 0)
{
while (strcmp(doc[loc].file_name, "") != 0)
{
doc[loc].address = doc[loc + 1].address;
strcpy(doc[loc].file_name, doc[loc + 1].file_name);
doc[loc].size = doc[loc + 1].size;
doc[loc].blocks = doc[loc + 1].blocks;
loc++;
}
printf("\nFile Deleted Successfully");
}
else
{
printf("\nSorry, some error occurred. Could not delete file.");
return;
}
}
else
{
printf("No file with the given name was found (Did you enter the name along with it's extension?)");
return;
}
}
void view_FAT()
{
int limit = 0;
while (strcmp(doc[limit].file_name, "") != 0)
{
limit++;
}
printf("--------------------------------------------------------------------------------------------------------------\n");
printf("File_no\tFile_name\tFile_Address\tFile_Size\tNumber_of_blocks\tOwnership\tPermissions\n");
printf("______________________________________________________________________________________________________________\n");
for (int i = 0; i < limit; i++)
{
printf(" %d\t%s\t%p\t\t%ld\t\t%d\t\t%s\t\t%d\n", i + 1, doc[i].file_name, doc[i].address, doc[i].size, doc[i].blocks, doc[i].owner, doc[i].permission);
}
printf("\n--------------------------------------------------------------------------------------------------------------\n");
}
void file_owner()
{
char name[30];
printf("Enter the name of the file, for which ownership is to be changed: ");
scanf("%s", name);
int loc = file_number(name);
if (loc != -1)
{
printf("The current owner is: %s\n", doc[loc].owner);
printf("Enter the new owner (Options: user, group, all) :- ");
scanf("%s", name);
int i = 0;
while (name[i] != '\0')
{
name[i] = tolower(name[i]);
i++;
}
if (strcmp(name, "user") == 0 || strcmp(name, "group") == 0 || strcmp(name, "all") == 0)
{
strcpy(doc[loc].owner, name);
printf("Ownership changed.");
return;
}
else
{
printf("No such owner value defined.");
return;
}
}
else
{
printf("File not found.");
return;
}
}
void file_rights()
{
printf("\nA file has 3 types of permissions: read(r), write(w), and modify(m)\n");
printf("We represent permissions for user, group, and all in the same order, folowing a absolute(numeric) mode of representation:\n");
printf("------------------------------------------------\n");
printf("Number Permission Type Symbol\n");
printf("------------------------------------------------\n");
printf("0 No Permission ---\n");
printf("1 Modify --m\n");
printf("2 Write -w-\n");
printf("3 Modify + Write -wm\n");
printf("4 Read r--\n");
printf("5 Read + Modify r-m\n");
printf("6 Read + Write rw-\n");
printf("7 Read + Write + Modify rwm\n");
printf("------------------------------------------------\n");
printf("The permission granted for a newly created file, is 764, which represents rwmrw-r--\n");
char name[30];
printf("\nEnter the full name of the file whose permissions are to be modified: ");
scanf("%s", name);
int loc = file_number(name);
if (loc != -1)
{
int new_per;
printf("\nEnter the new permissions for the file, in absolute(numeric) representation: ");
scanf("%d", &new_per);
if (validate(new_per) == 0)
{
doc[loc].permission = new_per;
printf("Permissions changed");
return;
}
else
{
printf("Invalid permission given.");
return;
}
}
else
{
printf("\nFile not found.");
return;
}
return;
}
int main()
{
for (int i = 0; i < 30; i++)
{
doc[i].address = NULL;
doc[i].blocks = 0;
doc[i].size = 0;
strcpy(doc[i].file_name, "");
}
int run = 1;
printf("**************WELCOME**************\n");
printf("This File System assumes a storage space of about 400KB, and is paritioned into 400 blocks of 1KB each.");
login();
printf("\n\nOperations\n___________________________________________________\n");
int choice;
while (run == 1)
{
printf("\nHit 1 to create a new file");
printf("\nHit 2 to read (an existing) file");
printf("\nHit 3 to modify (an existing) file");
printf("\nHit 4 to rename an existing file");
printf("\nHit 5 to delete an existing file");
printf("\nHit 6 to enter priviledged mode");
printf("\nHit 7 to login with different credentials.");
printf("\nHit 8 to exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
create_file();
break;
case 2:
read_file();
break;
case 3:
modify_file(NULL, -1);
break;
case 4:
rename_file();
break;
case 5:
delete_file();
break;
case 6:
printf("**************PRIVILEGED MODE**************\n");
char global_user[30], global_password[30];
printf("Enter the global user credentials");
printf("\nUsername: ");
scanf("%s", global_user);
printf("Password: ");
scanf("%s", global_password);
if (strcmp(global_user, "global_user") == 0 && strcmp(global_password, "12345") == 0)
{
int choice;
printf("Hello, Global user. You have the privilege to access the FAT Table, and modify file permissions and change file ownership.");
while (choice != 4)
{
printf("\nHit 1 to view the FAT Table, 2 for changing file ownership, 3 to modify file permissions, or 4 to exit: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
view_FAT();
break;
case 2:
file_owner();
break;
case 3:
file_rights();
break;
case 4:
break;
default:
printf("Invalid option.");
break;
}
}
}
else
{
printf("Invalid Global user credentials.");
break;
}
break;
case 7:
login();
break;
case 8:
printf("**************THANK YOU**************\n");
run = 2;
break;
default:
printf("Invalid Option");
break;
}
}
return 0;
}