-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject3.c
More file actions
1073 lines (858 loc) · 23.2 KB
/
Copy pathproject3.c
File metadata and controls
1073 lines (858 loc) · 23.2 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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
typedef struct{
char** tokens;
int numTokens;
} instruction;
pid_t child_pids[1000];
int child_nb = 0;
void addToken(instruction* instr_ptr, char* tok);
void clearInstruction(instruction* instr_ptr);
void addNull(instruction* instr_ptr);
void printTokens(instruction* instr_ptr);
char userinput[100];
char inputitems[10][10];
void parse(instruction* instr);
int getOffset(int currentDir);
void listContents(char* directory);
void size(char* file);
void makeDir(char directory[]);
void creatFile(char file[]);
void writeFile(char* file, int offset, int size, char* buffer);
void changeDir(instruction* instr);
void readFile(instruction* instr);
void mv(instruction* instr);
void cp(instruction* instr);
unsigned int BPB_BytesPerSec;
unsigned int BPB_SecPerClus;
unsigned int BPB_RsvdSecCnt;
unsigned int BPB_NumFATs;
unsigned int BPB_FATSz32;
unsigned int BPB_RootEntCnt;
unsigned int BPB_RootClus;
unsigned int BPB_TotSec32;
char BS_OEMName[8];
unsigned int curDir;
unsigned int FirstDataSector;
unsigned int FirstSectorofCluster;
unsigned int offset;
unsigned int curDirClusterAddr;
unsigned int rootDirClusterAddr;
FILE *imagefile;
FILE *openfile;
struct __attribute__((__packed__)) DirectoryEntry{
unsigned char DIR_Name[11];
unsigned char DIR_Attr;
unsigned char DIR_NTRes;
unsigned char DIR_CrtTimeTenth;
unsigned char DIR_CrtTime[2];
unsigned char DIR_CrtDate[2];
unsigned char DIR_LstAccDate[2];
unsigned char DIR_FstClusHI[2];
unsigned char DIR_WrtTime[2];
unsigned char DIR_WrtDate[2];
unsigned char DIR_FstClusLO[2];
unsigned char DIR_FileSize[4];
};
struct DirectoryEntry dir[16];
char openFileList[100];
int main(){
int filelistspot = 0;
imagefile = fopen("fat32.img", "r");
char* command = "start";
instruction instr;
instr.tokens = NULL;
instr.numTokens = 0;
fseek(imagefile, 3, SEEK_SET);
fread(&BS_OEMName, 8, 1, imagefile);
fseek(imagefile, 11, SEEK_SET);
fread(&BPB_BytesPerSec, 2, 1, imagefile);
fread(&BPB_SecPerClus, 1, 1, imagefile);
fread(&BPB_RsvdSecCnt, 2, 1, imagefile);
fread(&BPB_NumFATs, 1, 1, imagefile);
fread(&BPB_RootEntCnt, 2, 1, imagefile);
fseek(imagefile, 36, SEEK_SET);
fread(&BPB_FATSz32, 4, 1, imagefile);
fseek(imagefile, 44, SEEK_SET);
fread(&BPB_RootClus, 4, 1, imagefile);
FirstDataSector = (BPB_NumFATs * BPB_FATSz32) + BPB_RsvdSecCnt;
FirstSectorofCluster = FirstDataSector + ((BPB_RootClus - 2) * BPB_SecPerClus);
//set current directory to root
curDir = BPB_RootClus;
offset = getOffset(curDir);
char rootDir[512];
char dirNames[16];
//read root directory
fseek(imagefile, offset, SEEK_SET);
fread(&dir[0], 32, 16, imagefile);
rootDirClusterAddr = BPB_NumFATs * BPB_FATSz32 * BPB_BytesPerSec + (BPB_RsvdSecCnt * BPB_BytesPerSec);
curDirClusterAddr = rootDirClusterAddr;
//printf("Root Cluster Check: %x\n", curDirClusterAddr);
//printf("offset: %x\n", offset);
do {
printf("$ ");
clearInstruction(&instr);
parse(&instr);
if(strcmp(instr.tokens[0], "info") == 0){ //thomas
if (instr.numTokens != 1) {
printf("Error: No arguments expected\n");
continue;
}
//parse the boot sector. print the field name and corresponding values for each entry, one per line
//bytes per sector
printf("Bytes per Sector: %d\n", BPB_BytesPerSec);
//sectors per cluster
printf("Sectors Per Cluster: %d\n", BPB_SecPerClus);
//reserved sector count
printf("Reserved Sector Count: %d\n", BPB_RsvdSecCnt);
//number of FATs
printf("Number of FATs: %d\n", BPB_NumFATs);
//total sectors
printf("Total Sectors: %d\n", BPB_TotSec32);
//FATsize
printf("FATsize: %d\n", BPB_FATSz32);
//root cluster
printf("Root Cluster: %d\n", BPB_RootClus);
}
else if(strcmp(instr.tokens[0], "size") == 0){ //thomas
if (instr.numTokens != 2) {
printf("Error: Incorrect number of arguments\n");
continue;
}
size(instr.tokens[1]);
}
//prints contents of curDir
//if a directory is listed, cd directory, print directory, then change back to previous
else if(strcmp(instr.tokens[0], "ls") == 0){ //thomas
if (instr.numTokens > 2) {
printf("Error: Incorrect number of arguments\n");
continue;
}
if(instr.tokens[1] != NULL)
listContents(instr.tokens[1]);
else
listContents(NULL);
}
else if(strcmp(instr.tokens[0], "cd") == 0){ //scott
//printf("cd selected\n");
changeDir(&instr);
}
else if(strcmp(instr.tokens[0], "creat") == 0){ //taylor
if(instr.tokens[2] != NULL){
printf("Too many arguments.\n");
continue;
}
if(instr.tokens[1] == NULL){
printf("Missing arguments.\n");
continue;
}
creatFile(instr.tokens[1]);
}
else if(strcmp(instr.tokens[0], "mkdir") == 0){ //taylor
if(instr.tokens[2] != NULL){
printf("Too many arguments.\n");
continue;
}
if(instr.tokens[1] == NULL){
printf("Missing arguments.\n");
continue;
}
makeDir(instr.tokens[1]);
}
else if(strcmp(instr.tokens[0], "mv") == 0){ //scott
//printf("mv selected\n");
mv(&instr);
}
else if(strcmp(instr.tokens[0],"open") == 0){ //taylor
if( instr.tokens[1] == NULL || instr.tokens[2] == NULL){
printf("Missing arguments.\n");
continue;
}
//print error if invalid mode is used
if(strcmp(instr.tokens[2], "r") != 0 && strcmp(instr.tokens[2],"w") != 0 && strcmp(instr.tokens[2], "rw") != 0 && strcmp(instr.tokens[2], "wr") != 0){
printf("Invalid mode.\n");
continue;
}
int filecheck = 0;
//print error if file\inputitems[1] is already opened
//print error if it is a directory
int i;
for(i = 0; i < 100; i++){
if(strcmp(instr.tokens[1], &openFileList[i]) == 0)
{
printf("File is already opened.\n");
break;
}
}
int filereal = 0;
char temp[12];
char directory[16];
int p;
int j;
int z = 0;
for(j = 1; j < 16; j=j+2){
for(p = 0; p < 12; p++){
temp[p] = 0;
if(dir[j].DIR_Name[p] == ' ')
break;
else
temp[p] = dir[j].DIR_Name[p];
}
if(strcmp(instr.tokens[1], temp) == 0){
filereal = 1;
//check if file entered and give error if not
if(dir[j].DIR_Attr == 16)
{
printf("Enter a file, not directory.\n");
break;
}
break;
}
}
//check if file exists
if(filereal == 0)
printf("File does not exist.\n");
else{
if(strcmp(instr.tokens[2],"r") == 0)
dir[filereal].DIR_Attr = 1;
}
strcpy(&openFileList[filelistspot],instr.tokens[1]);
filelistspot= filelistspot + 10;
}
else if(strcmp(instr.tokens[0], "close") == 0){ //taylor
if( instr.tokens[1] == NULL){
printf("Missing parameters.\n");
continue;
}
int opencheck = 0;
int i;
for(i = 0; i < 100; i++){
if(strcmp(instr.tokens[1], &openFileList[i]) == 0)
{
opencheck = 1;
printf("File has been successfully closed.\n");
strcpy(&openFileList[i],"0");
}
}
if(opencheck == 0){
printf("Error: File is not open.\n");
continue;
}
//set attribute
char temp[12];
int p;
int j;
for(j = 1; j < 16; j=j+2){
for(p=0; p < 12;p++){
temp[p] = 0;
if(dir[j].DIR_Name[p] == ' ')
break;
else
temp[p] = dir[j].DIR_Name[p];
}
if(strcmp(instr.tokens[1], temp) == 0){
dir[j].DIR_Attr = 32;
break;
}
}
}
else if(strcmp(instr.tokens[0], "read") == 0){ //scott
//check params are all there
if( instr.tokens[3] == NULL || instr.tokens[2] == NULL || instr.tokens[1] == NULL){
printf("Missing parameters.\n");
continue;
}
if(instr.numTokens == 4)
{
readFile(&instr);
}
else
{
printf("Not enough arguents.\n");
}
//print error if filename/inputitems[1] does not exist
//printf("File does not exist.\n");
//print error if offset\inputitems[2] is larger than the size of the file
if((inputitems[2] - '0') > (inputitems[3] - '0'))
printf("Error: Offset is larger than the size of the file.\n");
//start reading the data from the file in the current working directory with the name filename/inputitems[1]
//read from the file at offset/inputitems[2] bytes and stop after reading size/inputitems[3] bytes
//if offset + size is larger than the size of the file, just ready size - offset bytes starting at offset
}
else if(strcmp(instr.tokens[0], "write") == 0){ //thomas
if (instr.numTokens != 5) {
printf("Error: Incorrect number of arguments\n");
continue;
}
//writeFile(&instr);
//print error if filename/inputitems[1] does not exist
//printf("File does not exist.\n");
//print error if filename is a directory
//printf("Must be file to read.\n");
//print error if file is not opened for writing
//printf("File is not open.\n");
//print error if offset\inputitems[2] is larger than the size of the file
//printf("Offset is larger than the size of the file.\n");
//write to a file in current working directory with name filename/inputitems[1]
//start writing at offset/inputitems[2] bytes and stop after writing size/inputitems[3] bytes. you will write string at this position
//if offset + size is larger than the size of the file you will need to extend the length of the file to at least hold the data being written
//if string is larger than size write only the first size characters of string. if string is smaller than size write the remaining character as ascii 0(null characters)
}
else if(strcmp(instr.tokens[0], "rm") == 0){ //thomas
if (instr.numTokens != 2) {
printf("Error: Incorrect number of arguments\n");
continue;
}
//print error if filename/inputitems[1] does not exist
//printf("File does not exist.\n");
//print error if filename is a directory
//printf("Must be file to read.\n");
//delete the file named filename from the current working directory
//this means removing the entry in the directory as well as reclaiming the actual file data
}
else if(strcmp(instr.tokens[0], "cp") == 0){ //scott
//printf("cp selected\n");
cp(&instr);
}
else if(strcmp(instr.tokens[0], "exit") == 0){
//return 0;
break;
}
else{
printf("Error: Wrong Command Name\n");
}
} while (instr.tokens[0], "exit");
//exit
//free memory
fclose(imagefile);
imagefile = NULL;
clearInstruction(&instr);
return 0;
}
void makeDir(char directory[]){
//check directory does not already exist
char temp[12];
int p;
int j;
int z = 0;
for(j = 1; j < 16; j=j+2){
for(p = 0; p < 12; p++){
temp[p] = 0;
if(dir[j].DIR_Name[p] == ' ')
break;
else
temp[p] = dir[j].DIR_Name[p];
}
if(strcmp(directory, temp) == 0){
if(dir[j].DIR_Attr == 16)
printf("Directory already exists.\n");
else
printf("File already exists with same name.\n");
break;
}
}
//create a new directory in the current working directory with the name dirname/inputitems[1]
}
void creatFile(char file[]){
//check file does not already exist
char temp[12];
int p;
int j;
int z = 0;
for(j = 1; j < 16; j=j+2){
for(p = 0; p < 12; p++){
temp[p] = 0;
if(dir[j].DIR_Name[p] == ' ')
break;
else
temp[p] = dir[j].DIR_Name[p];
}
if(strcmp(file, temp) == 0){
if(dir[j].DIR_Attr == 32)
printf("File already exists.\n");
else
printf("Directory already exists with same name.\n");
break;
}
}
//creates a file in the current working directory with a size of 0 bytes and with a name of filename/inputitems[1]
//if it does exist print error
//fprint("File already exists.\n");
}
///parses user input
///places tokens inside of instruction pointer passed
void parse(instruction* instr) {
char* token = NULL;
char* temp = NULL;
// loop reads character sequences separated by whitespace
do {
//scans for next token and allocates token var to size of scanned token
scanf("%ms", &token);
temp = (char*)malloc((strlen(token) + 1) * sizeof(char));
int i;
int start = 0;
for (i = 0; i < strlen(token); i++) {
//pull out special characters and make them into a separate token in the instruction
if (token[i] == '|' || token[i] == '>' || token[i] == '<' || token[i] == '&') {
if (i-start > 0) {
memcpy(temp, token + start, i - start);
temp[i-start] = '\0';
addToken(instr, temp);
}
char specialChar[2];
specialChar[0] = token[i];
specialChar[1] = '\0';
addToken(instr, specialChar);
start = i + 1;
}
}
if (start < strlen(token)) {
memcpy(temp, token + start, strlen(token) - start);
temp[i-start] = '\0';
addToken(instr, temp);
}
//free and reset variables
free(token);
free(temp);
token = NULL;
temp = NULL;
} while ('\n' != getchar()); //until end of line is reached
}
//reallocates instruction array to hold another token
//allocates for new token within instruction array
void addToken(instruction* instr_ptr, char* tok)
{
//extend token array to accomodate an additional token
if (instr_ptr->numTokens == 0)
instr_ptr->tokens = (char**) malloc(sizeof(char*));
else
instr_ptr->tokens = (char**) realloc(instr_ptr->tokens, (instr_ptr->numTokens+1) * sizeof(char*));
//allocate char array for new token in new slot
instr_ptr->tokens[instr_ptr->numTokens] = (char *)malloc((strlen(tok)+1) * sizeof(char));
strcpy(instr_ptr->tokens[instr_ptr->numTokens], tok);
instr_ptr->numTokens++;
}
void addNull(instruction* instr_ptr)
{
//extend token array to accomodate an additional token
if (instr_ptr->numTokens == 0)
instr_ptr->tokens = (char**)malloc(sizeof(char*));
else
instr_ptr->tokens = (char**)realloc(instr_ptr->tokens, (instr_ptr->numTokens+1) * sizeof(char*));
instr_ptr->tokens[instr_ptr->numTokens] = (char*) NULL;
instr_ptr->numTokens++;
}
void clearInstruction(instruction* instr_ptr) {
int i;
for (i = 0; i < instr_ptr->numTokens; i++)
free(instr_ptr->tokens[i]);
free(instr_ptr->tokens);
instr_ptr->tokens = NULL;
instr_ptr->numTokens = 0;
}
//returns offset of current directory
int getOffset(int currentDir) {
if (currentDir == 0)
currentDir = 2;
int dirOffset = currentDir - 2;
dirOffset = dirOffset * BPB_BytesPerSec;
dirOffset += BPB_BytesPerSec * BPB_RsvdSecCnt;
dirOffset += BPB_NumFATs * BPB_FATSz32 * BPB_BytesPerSec;
return dirOffset;
}
///prints size of file in bytes
void size(char* file) {
//get offset and read from directory
offset = getOffset(curDir);
fseek(imagefile, offset, SEEK_SET);
fread(&dir[0], 32, 16, imagefile);
int dircheck = 0;
char temp[12];
int a;
int b;
//check for file
for(b = 1; b < 16; b=b+2){
for(a = 0; a < 12; a++){
temp[a] = 0;
if(dir[b].DIR_Name[a] == ' ')
break;
else
temp[a] = dir[b].DIR_Name[a];
}
//if file name matches check that it is not a directory
if(strcmp(file, temp) == 0 && dir[b].DIR_Attr != 16){
dircheck = 1;
break;
}
}
//print error if directory or not exist
if (!dircheck) {
printf("File %s does not exist\n", file);
}
printf("%d bytes\n", dir[b].DIR_FileSize);
}
///ls
///prints contents of directory
void listContents(char* directory) {
//create temp variable to revert back to if cd is called
int tempCurDir = curDir;
offset = getOffset(curDir);
fseek(imagefile, offset, SEEK_SET);
fread(&dir[0], 32, 16, imagefile);
if(directory != NULL){
//need to check if directory exists
//if not give error
int dircheck = 0;
char temp[12];
int a;
int b;
for(b = 1; b < 16; b=b+2){
for(a = 0; a < 12; a++){
temp[a] = 0;
if(dir[b].DIR_Name[a] == ' ')
break;
else
temp[a] = dir[b].DIR_Name[a];
}
if(strcmp(directory, temp) == 0 && dir[b].DIR_Attr == 16){
dircheck = 1;
break;
}
}
//do nothing for current directory
if (strcmp(directory, ".") == 0);
//read parent directory
else if (strcmp(directory, "..") == 0) {
//cd(..)
}
//directory does not exist
else if(dircheck == 0) {
printf("Directory %s does not exist.\n", directory);
return;
}
//directory found, cd
else {
//cd(dir)
}
}
int p;
int j;
int q = 0;
for(j = 1; j < 16; j=j+2){
if(dir[j].DIR_Attr == 1 || dir[j].DIR_Attr == 16 || dir[j].DIR_Attr == 32){
for(p = 0; p < 12; p++){
printf("%c", dir[j].DIR_Name[p]);
}
printf("\n");
}
}
printf(".\n");
if (curDir != BPB_RootClus)
printf("..\n");
//change back to current directory
curDir = tempCurDir;
}
void removeEntry(char* file) {
offset = getOffset(curDir);
fseek(imagefile, offset, SEEK_SET);
fread(&dir[0], 32, 16, imagefile);
int dircheck = 0;
char temp[12];
int a;
int b;
for(b = 1; b < 16; b=b+2){
for(a = 0; a < 12; a++){
temp[a] = 0;
if(dir[b].DIR_Name[a] == ' ')
break;
else
temp[a] = dir[b].DIR_Name[a];
}
if(strcmp(file, temp) == 0 && dir[b].DIR_Attr != 16){
dircheck = 1;
break;
}
}
if (!dircheck) {
printf("Error: File %s does not exist");
return;
}
}
void writeFile(char* file, int fileOffset, int size, char* buffer) {
offset = getOffset(curDir);
fseek(imagefile, offset, SEEK_SET);
fread(&dir[0], 32, 16, imagefile);
int dircheck = 0;
char temp[12];
int a;
int b;
for(b = 1; b < 16; b=b+2){
for(a = 0; a < 12; a++){
temp[a] = 0;
if(dir[b].DIR_Name[a] == ' ')
break;
else
temp[a] = dir[b].DIR_Name[a];
}
if(strcmp(file, temp) == 0 && dir[b].DIR_Attr != 16){
dircheck = 1;
break;
}
}
if (!dircheck) {
printf("Error: File %s does not exist");
return;
}
/*if (fileOffset + size > dir[b].DIR_FileSize) {
}*/
}
void changeDir(instruction* instr)
{
if(instr->numTokens == 2)
{
if(strcmp(instr->tokens[1], ".") == 0) //do nothing
return;
if(strcmp(instr->tokens[1], "..") == 0)
{
curDirClusterAddr = rootDirClusterAddr;
//curDir = rootDir;
return;
}
int dircheck = 0;
char temp[12];
int a;
int b;
for(b = 1; b < 16; b=b+2){
for(a = 0; a < 12; a++){
temp[a] = 0;
if(dir[b].DIR_Name[a] == ' ')
break;
else
temp[a] = dir[b].DIR_Name[a];
}
if(strcmp(instr->tokens[1], temp) == 0 && dir[b].DIR_Attr == 16){
dircheck = 1;
break;
}
}
if(dircheck == 1)
{
printf("addr before: %d\n", curDirClusterAddr);
//printf("curDir: %d\n", curDir);
if(dir[b].DIR_FstClusLO == 0 && (strcmp(dir[b].DIR_Name, "..") == 0))
{
curDirClusterAddr = 0x2;
//curDir = 0x2;
}
else
{
curDirClusterAddr = dir[b].DIR_FstClusLO;
//curDir = dir[b].DIR_FstClusLO;
}
//printf("It's a directory\n");
printf("addr after: %d\n", curDirClusterAddr);
//printf("curDir: %d\n", curDir);
}
else
{
printf("Directory does not exist.\n");
}
//////// ***** you'll be changing curDirClusterAddr and curDir to where the dir starts) ////////////////////////////////////
//check that dirname/inputitems[1] exists and is a directory
//change current working directory to dirname
//code will need to maintain current working directory state
//if not print error
//fprint("Directory does not exist.\n");
}
}
void readFile(instruction* instr)
{
if(instr->numTokens == 4)
{
if(strcmp(instr->tokens[1], ".") == 0) //do nothing
return;
int dircheck = 0;
char temp[12];
int a;
int b;
for(b = 1; b < 16; b=b+2){
for(a = 0; a < 12; a++){
temp[a] = 0;
if(dir[b].DIR_Name[a] == ' ')
break;
else
temp[a] = dir[b].DIR_Name[a];
}
if(strcmp(instr->tokens[1], temp) == 0 && dir[b].DIR_Attr == 16){
dircheck = 1;
break;
}
}
if(dircheck != 1) //it's not a directory, might be a file
{
/*
int clust = (int)dir[b].DIR_FstClusLO;
int offs = getOffset(clust);
char* bytes = malloc(instr->tokens[3]);
fseek(imagefile, offs + (int)instr->tokens[2], SEEK_SET);
fread(bytes, (int)instr->tokens[3], 1, dir);
printf("%s\n", bytes);
*/
}
else if(dircheck == 1)
printf("FILENAME is a Directory.\n");
}
}
void mv(instruction* instr)
{
if(instr->numTokens == 3)
{
int filereal = 0;
char temp[12];
char directory[16];
int p;
int j;
int z = 0;
for(j = 1; j < 16; j=j+2){
for(p = 0; p < 12; p++){
temp[p] = 0;
if(dir[j].DIR_Name[p] == ' ')
break;
else
temp[p] = dir[j].DIR_Name[p];
}
if(strcmp(instr->tokens[1], temp) == 0){
filereal = 1;
//check if file entered and give error if not
if(dir[j].DIR_Attr == 16)
{
printf("Enter a file, not directory.\n");
return;
}
break;
}
}
//check if token[2] is a directory
int dircheck = 0;
temp[12];
int a;
int b;
for(b = 1; b < 16; b=b+2){
for(a = 0; a < 12; a++){
temp[a] = 0;
if(dir[b].DIR_Name[a] == ' ')
break;
else
temp[a] = dir[b].DIR_Name[a];
}
if(strcmp(instr->tokens[1], temp) == 0 && dir[b].DIR_Attr == 16){
dircheck = 1;
break;
}
}
if(dircheck == 1)
{
printf("Cannot move directory: invalid destinationa argument.\n");
return;
}
//check if file exists
if(filereal == 0)
printf("File does not exist.\n");
else
{
if(strcmp(instr->tokens[1], instr->tokens[2]) == 0)
{
printf("The name is already being used by another file.\n");
}
else if(dircheck == 1)
{}
}
}
else{
printf("ERROR: incorrect number of arguments.\n");
}
//if both to/inputitems[2] and from/inputitems[1] exist and are files print error
//printf("The name is already being used by another file.\n");
//if to is a file and from is a directory print error
//printf("Cannot move directory: invalid destinationa argument.\n");
//otherwise if to exists and is a directory, the system will move the from entry to be inside the to directory
//if to does not exist the system will rename the from entry to the name specified by to
}