forked from Audakel/cs-345
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os345p6.c
executable file
·1928 lines (1694 loc) · 62.8 KB
/
os345p6.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
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
// os345p6.c - FAT File Management System
// ***********************************************************************
// ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER **
// ** **
// ** The code given here is the basis for the CS345 projects. **
// ** It comes "as is" and "unwarranted." As such, when you use part **
// ** or all of the code, it becomes "yours" and you are responsible to **
// ** understand any algorithm or method presented. Likewise, any **
// ** errors or problems become your responsibility to fix. **
// ** **
// ** NOTES: **
// ** -Comments beginning with "// ??" may require some implementation. **
// ** -Tab stops are set at every 3 spaces. **
// ** -The function API's in "OS345.h" should not be altered. **
// ** **
// ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER **
// ***********************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <setjmp.h>
#include <assert.h>
#include "os345.h"
#include "os345fat.h"
// ***********************************************************************
// ***********************************************************************
// project 6 variables
// RAM disk
extern unsigned char RAMDisk[SECTORS_PER_DISK * BYTES_PER_SECTOR];
extern unsigned char FAT1[]; // current fat table
extern unsigned char FAT2[]; // secondary fat table
extern FDEntry OFTable[]; // open files
extern char dirPath[128]; // directory path
extern TCB tcb[]; // task control block
extern int curTask; // current task #
extern bool diskMounted; // disk has been mounted
FMSERROR FMSErrors[NUM_ERRORS] = {
{ERR50, ERR50_MSG}, // Invalid File Name
{ERR51, ERR51_MSG}, // Invalid File Type
{ERR52, ERR52_MSG}, // Invalid File Descriptor
{ERR53, ERR53_MSG}, // Invalid Sector Number
{ERR54, ERR54_MSG}, // Invalid FAT Chain
{ERR55, ERR55_MSG}, // Invalid Directory
{ERR60, ERR60_MSG}, // File Already Defined
{ERR61, ERR61_MSG}, // File Not Defined
{ERR62, ERR62_MSG}, // File Already Open
{ERR63, ERR63_MSG}, // File Not Open
{ERR64, ERR64_MSG}, // File Directory Full
{ERR65, ERR65_MSG}, // File Space Full
{ERR66, ERR66_MSG}, // End-Of-File
{ERR67, ERR67_MSG}, // End-Of-Directory
{ERR68, ERR68_MSG}, // Directory Not Found
{ERR69, ERR69_MSG}, // Can Not Delete
{ERR70, ERR70_MSG}, // Too Many Files Open
{ERR71, ERR71_MSG}, // Not Enough Contiguous Space
{ERR72, ERR72_MSG}, // Disk Not Mounted
{ERR80, ERR80_MSG}, // File Seek Error
{ERR81, ERR81_MSG}, // File Locked
{ERR82, ERR82_MSG}, // File Delete Protected
{ERR83, ERR83_MSG}, // File Write Protected
{ERR84, ERR84_MSG}, // Read Only File
{ERR85, ERR85_MSG} // Illegal Access
};
int sectorReads;
int sectorWrites;
// ***********************************************************************
// project 6 functions and tasks
// ***********************************************************************
// ***********************************************************************
// ***********************************************************************
// ***********************************************************************
// p6 - runs finalTest
//
int P6_project6(int argc, char* argv[])
{
char* newArgv[] = { {"finalTest"}, {"All"} };
if (sizeof(DirEntry) != 32)
{
printf("\n***Incompatible long data type in DirEntry***");
return 0;
}
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
P6_chkdsk(0, (char**)0); // check RAM disk
P6_finalTest(2, newArgv); // all final tests
return 0;
} // end P6_project6
// ***********************************************************************
// ***********************************************************************
// cd <fileName>
int P6_cd(int argc, char* argv[]) // change directory
{
int error;
if (argc < 2)
{
printf("\n CD <fileName>");
return 0;
}
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
if (!(error = fmsChangeDir(argv[1]))) return 0;
if (error == ERR67) error = ERR68;
fmsError(error);
return 0;
} // end P6_cd
// ***********************************************************************
// ***********************************************************************
// dir <mask>
int P6_dir(int argc, char* argv[]) // list directory
{
int index = 0;
DirEntry dirEntry;
char mask[20];
int error = 0;
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
if (argc < 2) strcpy(mask, "*.*");
else strcpy(mask, argv[1]);
//dumpRAMDisk("Root Directory", 19*512, 19*512+256);
printf("\nName:ext time date cluster size");
while (1)
{
error = fmsGetNextDirEntry(&index, mask, &dirEntry, CDIR);
if (error)
{
if (error != ERR67) fmsError(error);
break;
}
printDirectoryEntry(&dirEntry);
SWAP;
}
//dumpRAMDisk("Root Directory", 19*512, 20*512);
return 0;
} // end P6_dir
// ***********************************************************************
// ***********************************************************************
// fat {<TAB#>{,<LOC#>{,<END>}}}
//
// 1 fat output fat 1 table
// 2 fat <1 to 2> output fat <#> table
// 2 fat <# gt 2> output fat 1 table entry <#>
// 3 fat <#>,<s> output fat table <#> starting with entry <s>
// 4 fat <#>,<s>,<e> output fat table <#> from <s> to <e>
//
void outFatEntry(int index)
{
int fatvalue;
printf("\n FAT1[%d] = ", index);
fatvalue = getFatEntry(index, FAT1);
// Special formatting cases...
if (index < 2) printf("RSRV"); // A reserved cluster
else if (fatvalue == FAT_EOC) printf("EOC"); // The EOC marker
else if (fatvalue == FAT_BAD) printf("BAD"); // The BAD cluster marker
else printf("%d", fatvalue);
return;
}
int P6_dfat(int argc, char* argv[]) // list FAT table
{
int index;
int start = 0;
int end = 64;
// 1 fat output fat 1 table
// 2 fat <1 to 2> output fat <#> table
// 2 fat <# gt 2> output fat 1 table entry <#>
// 3 fat <#>,<s> output fat table <#> starting with entry <s>
// 4 fat <#>,<s>,<e> output fat table <#> from <s> to <e>
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
switch (argc)
{
case 1: // 1 fat output fat 1 table
{
printFatEntries("FAT1", start, end);
break;
}
case 2: // 2 fat <1 to 2> output fat <#> table
{
char buf[32];
index = INTEGER(argv[1]);
if ((index == 1) || (index ==2))
{
sprintf(buf, "Disk Image: FAT %d", index);
dumpRAMDisk(buf, (1+index*8)*512, (1+index*8)*512+64);
}
else
{
outFatEntry(index);
}
break;
}
case 3: // 3 fat <#>,<s> output fat table <#> starting with entry <s>
{
char* FATtb = (INTEGER(argv[1]) == 2) ? FAT2 : FAT1;
start = INTEGER(argv[2]);
end = start + 1;
for (index=start; index<end; index++) outFatEntry(index);
break;
}
case 4: // 4 fat <#>,<s>,<e> output fat table <#> from <s> to <e>
{
char* FATtb = (INTEGER(argv[1]) == 2) ? FAT2 : FAT1;
start = INTEGER(argv[2]);
end = INTEGER(argv[3]);
for (index=start; start<end; index++) outFatEntry(index);
break;
}
}
return 0;
} // end P6_dfat
// ***********************************************************************
// ***********************************************************************
// mount <disk name>
//
int P6_mount(int argc, char* argv[]) // mount RAM disk
{
int error;
BSStruct bootSector;
char temp[128] = {""};
assert("64-bit" && (sizeof(DirEntry) == 32));
if (argc < 2) strcat(temp, "c:/lcc/projects/disk4");
else strcat(temp, argv[1]);
printf("\nMount Disk \"%s\"", temp);
error = fmsMount(temp, &RAMDisk);
if (error)
{
printf("\nDisk Mount Error %d", error);
return 0;
}
//dumpRAMDisk("Boot Sector", 0, 512-450);
//printf("\nBoot size = %d", sizeof(BSStruct));
fmsReadSector(&bootSector, 0);
strncpy(temp, (char*)&bootSector.BS_OEMName, 8);
temp[8] = 0;
printf("\n System: %s", temp);
printf("\n Bytes/Sector: %d", bootSector.BPB_BytsPerSec);
printf("\n Sectors/Cluster: %d", bootSector.BPB_SecPerClus);
printf("\n Reserved sectors: %d", bootSector.BPB_RsvdSecCnt);
printf("\n FAT tables: %d", bootSector.BPB_NumFATs);
printf("\n Max root dir entries: %d", bootSector.BPB_RootEntCnt);
printf("\n FAT-12 sectors: %d", bootSector.BPB_TotSec16);
printf("\n FAT sectors: %d", bootSector.BPB_FATSz16); // FAT sectors (should be 9)
printf("\n Sectors/track: %d", bootSector.BPB_SecPerTrk); // Sectors per cylindrical track
printf("\n Heads/volume: %d", bootSector.BPB_NumHeads); // Heads per volume (2 for 1.4Mb 3.5" floppy)
printf("\n FAT-32 sectors: %ld", bootSector.BPB_HiddSec); // Hidden sectors (0 for non-partitioned media)
return 0;
} // end P6_mount
// ***********************************************************************
// ***********************************************************************
// run <fileName>
int P6_run(int argc, char* argv[]) // run lc3 program from RAM disk
{
char fileName[128];
char* myArgv[] = {"1", ""};
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
if (argc < 2)
{
printf("\nRUN <fileName>");
return 0;
}
strcpy(fileName, argv[1]);
if (!strstr(fileName, ".hex")) strcat(fileName, ".hex");
myArgv[1] = (char*)&fileName;
createTask( myArgv[0], // task name
lc3Task, // task
MED_PRIORITY, // task priority
2, // task argc
myArgv); // task arguments
return 0;
} // end P6_run
// ***********************************************************************
// ***********************************************************************
// space
int P6_space(int argc, char* argv[])
{
DiskSize dskSize;
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
fmsDiskStats(&dskSize);
printf("\nFree: %d", dskSize.free);
printf("\nUsed: %d", dskSize.used);
printf("\n Bad: %d", dskSize.bad);
printf("\nSize: %d", dskSize.size);
return 0;
} // end P6_space
// ***********************************************************************
// ***********************************************************************
// type <fileName>
int P6_type(int argc, char* argv[]) // display file
{
int error, nBytes, FDs;
char buffer[4];
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
if (argc < 2)
{
printf("\n type <file>");
return 0;
}
printf("\nType File \"%s\"\n", argv[1]); // ?? debug
// open source file
if ((FDs = fmsOpenFile(argv[1], 0)) < 0)
{
fmsError(FDs);
return 0;
}
printf("\n FDs = %d\n", FDs); // ?? debug
while ((nBytes = fmsReadFile(FDs, buffer, 1)) == 1)
{
putchar(buffer[0]);
SWAP;
}
if (nBytes != ERR66) fmsError(nBytes);
if (error = fmsCloseFile(FDs)) fmsError(error);
return 0;
} // end P6_type
// ***********************************************************************
// ***********************************************************************
// copy <file1>,<file2>
int P6_copy(int argc, char* argv[]) // copy file
{
int error, nBytes, FDs, FDd;
char buffer[BYTES_PER_SECTOR + 1];
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
if (argc < 3)
{
printf("\n copy <file1> <file2>");
return 0;
}
// open source file
if ((FDs = fmsOpenFile(argv[1], 0)) < 0)
{ fmsError(FDs);
return 0;
}
// open destination file
if ((FDd = fmsOpenFile(argv[2], 1)) < 0)
{ fmsCloseFile(FDs);
fmsError(FDd);
return 0;
}
// printf("\n FDs = %d\n FDd = %d\n", FDs, FDd);
nBytes = 1;
while (nBytes > 0)
{
error = 0;
nBytes = fmsReadFile(FDs, buffer, BYTES_PER_SECTOR);
SWAP;
if (nBytes < 0) break;
error = fmsWriteFile(FDd, buffer, nBytes);
if (error < 0) break;
//for (error=0; error<nBytes; error++) putchar(buffer[error]);
}
if (nBytes != ERR66) fmsError(nBytes);
if (error) fmsError(error);
error = fmsCloseFile(FDs);
if (error) fmsError(error);
error = fmsCloseFile(FDd);
if (error) fmsError(error);
return 0;
} // end P6_copy
// ***********************************************************************
// ***********************************************************************
// df <fileName>
int P6_define(int argc, char* argv[]) // define file
{
int error;
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
if (argc < 2)
{
printf("\n DF <fileName>");
return 0;
}
if ((error = fmsDefineFile(argv[1], ARCHIVE)) < 0)
{
fmsError(error);
}
return 0;
} // end P6_define
// ***********************************************************************
// ***********************************************************************
// del <fileName>
int P6_del(int argc, char* argv[]) // delete file
{
int error;
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
if (argc < 2)
{
printf("\n DL <fileName>");
return 0;
}
if ((error = fmsDeleteFile(argv[1])) < 0)
{
fmsError(error);
}
return 0;
} // end P6_del
// ***********************************************************************
// ***********************************************************************
// mkdir <fileName>
int P6_mkdir(int argc, char* argv[]) // create directory file
{
int error;
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
if (argc < 2)
{
printf("\n MK <fileName>");
return 0;
}
if ((error = fmsDefineFile(argv[1], DIRECTORY)) < 0)
{
fmsError(error);
}
return 0;
} // P6_mkdir
// ***********************************************************************
// ***********************************************************************
// unmount <diskname>
int P6_unmount(int argc, char* argv[]) // save ram disk
{
int error;
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
if (argc < 2)
{
printf("\n UM <fileName>");
return 0;
}
if ((error = fmsUnMount(argv[1], RAMDisk)) < 0)
{
fmsError(error);
}
return 0;
} // end P6_unmount
// ***********************************************************************
// ***********************************************************************
// ds <sector>
int P6_dumpSector(int argc, char* argv[]) // dump RAM disk sector
{
char temp[32];
int sector;
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
printf("\nValidate arguments..."); // ?? validate arguments
sector = INTEGER(argv[1]);
sprintf(temp, "Sector %d:", sector);
dumpRAMDisk(temp, sector*512, sector*512 + 512);
return 0;
} // end P6_dumpSector
// ***********************************************************************
// ***********************************************************************
// fs
int P6_fileSlots(int argc, char* argv[]) // list open file slots
{
int i, fd;
FDEntry* fdEntry;
printf("\nSlot Name Ext Atr Size Strt Curr cDir cPID Mode Flag Indx");
for (fd = 0; fd < NFILES; fd++)
{
fdEntry = &OFTable[fd];
if (fdEntry->name[0] == 0) continue; // open slot
printf("\n %2u ", fd);
for (i=0; i<12; i++) printf("%c", fdEntry->name[i]);
printf(" %02x%6u%6u%6u%6u%6u%6u%6u%6u",
fdEntry->attributes,
fdEntry->fileSize,
fdEntry->startCluster,
fdEntry->currentCluster,
fdEntry->directoryCluster,
fdEntry->pid,
fdEntry->mode,
fdEntry->flags,
fdEntry->fileIndex);
}
return 0;
} // end P6_fileSlots
// ***********************************************************************
// ***********************************************************************
// * Support functions
// ***********************************************************************
// Print directory entry
void printDirectoryEntry(DirEntry* dirent)
{
int i = 7;
char p_string[64] = " ------ 00:00:00 03/01/2004";
FATDate date; // The Date bit field structure
FATTime time; // The Time bit field structure
strncpy(p_string, (char*)&(dirent->name), 8); // Copies 8 bytes from the name
while (p_string[i] == ' ') i--;
p_string[i+1] = '.'; // Add extension
strncpy(&p_string[i+2], (char*)&(dirent->extension), 3);
while (p_string[i+4] == ' ') i--;
if (p_string[i+4] == '.') p_string[i+4] = ' ';
// Generate the attributes
if(dirent->attributes & 0x01) p_string[14] = 'R';
if(dirent->attributes & 0x02) p_string[15] = 'H';
if(dirent->attributes & 0x04) p_string[16] = 'S';
if(dirent->attributes & 0x08) p_string[17] = 'V';
if(dirent->attributes & 0x10) p_string[18] = 'D';
if(dirent->attributes & 0x20) p_string[19] = 'A';
// Extract the time and format it into the string...
memcpy(&time, &(dirent->time), sizeof(FATTime));
sprintf(&p_string[22], "%02u:%02u:%02u ", time.hour, time.min, time.sec*2);
// Extract the date and format it as well, inserting it into the string...
memcpy(&date, &(dirent->date), sizeof(FATDate));
sprintf(&p_string[31], "%02u/%02u/%04u %5u %5u",
date.month+1, date.day, date.year+1980,
dirent->startCluster, dirent->fileSize);
// p_string is now ready!
printf("\n%s", p_string);
return;
} // end PrintDirectoryEntry
// ***********************************************************************
// print FAT table
void printFatEntries(unsigned char* FAT, int start, int end)
{
char tbuf[16];
char fbuf[100];
unsigned short fatvalue;
int i, nn, counter = 0;;
nn = (512 * 9) / 1.5; // The number of fat entries in the FAT table
if (end < nn) nn = end;
sprintf(fbuf, "\n %6u:", start);
for (i=start; i<nn; i++)
{
if (!(counter%10) && counter)
{
printf("%s", fbuf);
sprintf(fbuf, "\n %6u:", i);
}
fatvalue = getFatEntry(i, FAT);
// Special formatting cases...
if (i < 2) sprintf(tbuf, " RSRV"); // A reserved cluster
else if (fatvalue == FAT_EOC) sprintf(tbuf, " EOC"); // The EOC marker
else if (fatvalue == FAT_BAD) sprintf(tbuf, " BAD"); // The BAD cluster marker
else sprintf(tbuf, "%5u", fatvalue);
strcat(fbuf, tbuf);
counter++;
}
if (counter%10) printf("%s", fbuf);
return;
} // End PrintFatTable
// ***********************************************************************
// dm <sa> <ea> - dump dumpRAMDisk memory
void dumpRAMDisk(char *s, int sa, int ea)
{
int i, ma;
unsigned char j;
printf("\n%s", s);
for (ma = sa; ma < ea; ma+=16)
{
printf("\n0x%08x:", ma);
for (i=0; i<16; i++)
{
printf(" %02x", (unsigned char)RAMDisk[ma+i]);
}
printf(" ");
for (i=0; i<16; i++)
{
j = RAMDisk[ma+i];
if ((j<20) || (j>127)) j='.';
printf("%c", j);
}
}
return;
} // end dumpRAMDisk
// ***********************************************************************
// ***********************************************************************
// * *
// * PPPPPP AA SSSSS SSSSS OOOO FFFFFF FFFFFF *
// * PP PP AAAA SS SS SS SS OO OO FF FF *
// * PP PP AA AA SSSS SSSS OO OO FF FF *
// * PPPPPP AAAAAAAA SSS SSS OO OO FFFF FFFF *
// * PP AA AA SS SS SS SS OO OO FF FF *
// * PP AA AA SSSSS SSSSS OOOO FF FF *
// * *
// ***********************************************************************
// ***********************************************************************
// ***********************************************************************
// ***********************************************************************
// passoff functions and tasks
#define ckFAT1 FAT1
#define ckFAT2 FAT2
int isValidDirEntry(DirEntry* dirEntry);
void getFileName(char* fileName, DirEntry* dirEntry);
void checkDirectory(char* dirName, unsigned char fat[], int dir);
// ***********************************************************************
// ***********************************************************************
// check disk for errors
//
int P6_chkdsk(int argc, char* argv[]) // check RAM disk
{
int i, j;
unsigned char fat[CLUSTERS_PER_DISK];
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
printf("\nChecking disk...");
// set fat if cluster allocated
for (i=2; i<CLUSTERS_PER_DISK; i++)
{
fat[i] = getFatEntry(i,ckFAT1) ? 1 : 0;
}
// process all directory entries
checkDirectory("Root", fat, 0);
// check to see if there are any zombie clusters
j = 0;
for (i=2; i<CLUSTERS_PER_DISK; i++)
{
if (fat[i])
{
SWAP;
if (j)
{
if (++j == i) continue;
printf("-%d", --j);
}
else
{
printf("\n Orphaned Clusters: ");
}
printf(", %d", i);
j = i;
}
}
if (j) printf("-%d", j);
// reconcile fat tables
for (i=0; i<CLUSTERS_PER_DISK; i++)
{
if (getFatEntry(i,ckFAT1) != getFatEntry(i,ckFAT2))
{
printf("\n FAT1[%d] != FAT2[%d] (%d/%d)",
i, i, getFatEntry(i,ckFAT1), getFatEntry(i,ckFAT2));
}
}
return 0;
} // end P6_chkdsk
// ***********************************************************************
// chkdsk utilities
// ***********************************************************************
//
// getFileName
//
// IN: *fileName pointer to buffer of at least 16 bytes
// *dirEntry 32-byte directory entry
//
void getFileName(char* fileName, DirEntry* dirEntry)
{
int i = 7;
memset(fileName, 0, 16);
strncpy(fileName, (char*)&(dirEntry->name), 8); // Copies 8 bytes from the name
i = strlen(fileName) - 1;
while (fileName[i] == ' ') i--;
fileName[++i] = '.'; // Add extension
strncpy(&fileName[++i], (char*)&(dirEntry->extension), 3);
i = strlen(fileName) - 1;
while (fileName[i] == ' ') i--;
if (fileName[i] == '.') fileName[i] = 0;
return;
} // end getFileName
// ***********************************************************************
// check for valid directory entry
int isValidDirEntry(DirEntry* dirEntry)
{
char name[16];
char fileName[16];
int error = 0;
getFileName(fileName, dirEntry);
strncpy(name, (char*)dirEntry->name, 11);
name[11] = 0;
if (strlen(name) != 11)
{
printf("\n Illegal NULL in file name \"%s\"", fileName);
error = 1;
}
// check for invalid characters
if (strpbrk(name, ".\"\\/:*<>|?"))
{
printf("\n Illegal character in file name \"%s\"", fileName);
error = 1;
}
return error;
} // end isValidDirEntry
// ***********************************************************************
// check directory (recursively)
void checkDirectory(char* dirName, unsigned char fat[], int dir)
{
int error, index = 0;
DirEntry dirEntry;
char fileName[32];
unsigned long maxSize;
// check for dot/dotdot
if (dir)
{
index = 0;
if (fmsGetNextDirEntry(&index, ".", &dirEntry, dir) < 0)
printf("\n \".\" missing from \"%s\" directory", dirName);
index = 0;
if (fmsGetNextDirEntry(&index, "..", &dirEntry, dir) < 0)
printf("\n \"..\" missing from \"%s\" directory", dirName);
}
index = 0;
while (1)
{
if (fmsGetNextDirEntry(&index, "*.*", &dirEntry, dir)) break;
// process file name
getFileName(fileName, &dirEntry);
if (dirEntry.attributes & (VOLUME || DIRECTORY))
printf("\n Attribute 0x%02x is illegal in file \"%s\"", dirEntry.attributes, fileName);
if (dirEntry.name[0] == '.') continue;
else
{ // follow clusters and clear fat
unsigned char chain[CLUSTERS_PER_DISK];
unsigned int i = dirEntry.startCluster;
// check for valid file name
isValidDirEntry(&dirEntry);
memset(chain, 0, CLUSTERS_PER_DISK);
error = 0;
maxSize = 0;
while (i)
{
maxSize += BYTES_PER_SECTOR;
// check for illegal cluster
if ((i < 2) || (i > CLUSTERS_PER_DISK))
{
printf("\n Illegal reference to cluster %d in file \"%s\"", i, fileName);
error = 1;
break;
}
// check for loop
if (chain[i]++)
{
printf("\n Loop detected at cluster %d in file \"%s\"", i, fileName);
error = 2;
break;
}
// check for cross chains
if (!fat[i])
{
printf("\n Cross chain detected at cluster %d in file \"%s\"", i, fileName);
error = 3;
break;
}
// mark cluster as used, get next cluster
fat[i] = 0;
i = getFatEntry(i,ckFAT1);
if (i == FAT_EOC) break; // EOD
if (i == FAT_BAD) break;
}
}
// if no loop and directory, then recurse
if ((error != 2) && (dirEntry.attributes & DIRECTORY))
{
// file size of directory should be 0
if (dirEntry.fileSize)
printf("\n Non-zero file size of %u in directory \"%s\"", dirEntry.fileSize, fileName);
checkDirectory(fileName, fat, dirEntry.startCluster);
}
else
{ // check file size
if (dirEntry.fileSize > maxSize)
printf("\n File size of %u in file \"%s\" exceeds %u", dirEntry.fileSize, fileName, maxSize);
}
}
return;
} // end checkDirectory
// ***********************************************************************
// final pass-off routine
#define FERROR(s1,s2,e) { printf(s1,s2); fmsError(e); return e; }
#define numWords 16
#define numFiles 64
#define numDirs 128
#define numTests 6
#define try(s1) if((error=s1)<0){printf("\nFailed \"%s\"",#s1);fmsError(error);return error;}
int fmsTests(int test, int debug);
extern int P6_fileSlots(int, char**); // list open file slots
extern int P6_dir(int, char**); // list current directory
int P6_finalTest(int argc, char* argv[])
{
int i, flags = 0;
int finalDebug = 0;
if (!diskMounted)
{
fmsError(ERR72);
return 0;
}
if (argc < 2)
{
printf("\n final 1 Define 128 directories, 64 files");
printf("\n final 2 Open 32 files, write, rewind, print, close");
printf("\n final 3 Open and append words");
printf("\n final 4 Random access on sector boundaries");
printf("\n final 5 Creating and deleting directories");
printf("\n final 6 Delete all test 1 files/directories");
printf("\n final all Run all tests");
return 0;
}
if (toupper(argv[1][0]) == 'A') flags = 0x3f;
else
{
flags = 0;
for (i=1; i<argc; i++)
{
flags |= 1 << (INTEGER(argv[i]) - 1);
printf("\n Run test #%d", INTEGER(argv[i]));
}
printf("\n flags = %02x", flags);
}
sectorReads = 0;
sectorWrites = 0;
for (i=1; i<=numTests; i++)
{
if (flags & (1 << (i-1)))
{
if (fmsTests(i, finalDebug))
{
printf("\nFAILED TEST %d!", i);
return 0;
}
}
}
// list system
P6_fileSlots(0, 0); // list open file slots
P6_dir(0, 0); // list directory
printf("\nSector reads = %d", sectorReads);
printf("\nSector writes = %d", sectorWrites);