-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDos.cpp
More file actions
1315 lines (1245 loc) · 38.2 KB
/
Dos.cpp
File metadata and controls
1315 lines (1245 loc) · 38.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<time.h>
#include<ctype.h>
#include <stdbool.h>
#define BLOCKSIZE 1024 // 磁盘块大小
#define SIZE 1024000 // 1000个磁盘块构成总虚拟磁盘空间
#define END 65535 // FAT中文件结束标志
#define FREE 0 // FAT中盘块空闲标志
#define ROOTBLOCKNUM 2 //数据区前两块分配给根目录文件
#define MAXOPENFILE 10 //最多同时打开文件个数
#define ERROR -1
#define OK 1
#define MAXTEXT 2000 //text最大大小
//文件控制块(目录项)
typedef struct FCB{
char filename[8]; //文件名
char exname[3]; //文件扩展名
unsigned char attribute; //文件属性字段,0:目录文件,1:数据文件
unsigned short time; //文件创建时间
unsigned short date; //文件创建日期
unsigned short first; //文件起始盘块号
unsigned long length; //文件长度(字节数)
char free; //表示目录项是否为空,若值为0,则表示空;值为1,表示已分配。
}fcb;
typedef struct FAT{
unsigned short id; //unsigned short 占两个字节,0-65535
}fat;
typedef struct USEROPEN{
char filename[8]; //文件名
char exname[3]; //文件扩展名
unsigned char attribute; //文件属性字段,0:目录文件,1:数据文件 ???
unsigned short time; //文件创建时间
unsigned short date; //文件创建日期
unsigned short first; //文件起始盘块号
unsigned long length; //文件长度(字节数)
//以上是文件FCB的内容,下面是记录有关文件使用的动态信息。
int father; //父目录的文件描述符
char dir[80]; //打开文件所在路径,以便快速检查指定文件是否已经打开。怎么检查???
int count; //读写指针的位置
char fcbstate; //文件的FCB是否已经被修改,如果修改了,则置为1;否则,为0
char topenfile; //打开表项是否为空,若值为0,则表示已被占用
}useropen;
typedef struct BLOCK0{
char information[200]; //存储一些描述信息,如磁盘块大小,磁盘块数量等
unsigned short root; //根目录文件的起始盘块号
unsigned char* startblock; //虚拟磁盘上数据区开始位置
}block0;
//全局变量定义
unsigned char *myvhard; //指向虚拟磁盘的起始地址
useropen openfile[MAXOPENFILE]; //用户打开文件表数组,最多同时打开10个文件
int curdir; //当前目录的文件描述符fd
char currentdir[80]; //记录当前目录的目录名
unsigned char* startp; //记录虚拟磁盘上数据区开始位置
time_t c_time; //文件系统启动时间
char filesys_name[] = "FAT16"; //文件系统文件的文件名
unsigned char *buf;
void startsys(); //初始化文件系统
void my_format(); //对虚拟磁盘进行格式化
int my_cd(char *dirname); //将当前目录改为指定的目录
void my_mkdir(char *dirname); //在当前目录下创建名为dirname的子目录
void my_rmdir(char *dirname); //在当前目录下删除名为dirname的子目录
void my_ls(); //显示当前目录的内容(子目录和文件信息)
int my_create(char *filename); //创建名为filename的新文件。
void my_rm(char *filename); //删除名为filename的文件
int my_open(char *filename); //打开当前目录下名为filename的文件
void my_close(int fd); //关闭之前由my_open()打开的文件描述符为fd的文件
int my_write(int fd); //将用户通过键盘输入的内容写到fd所指定的文件中
int do_write(int fd, char *text, int len, char wstyle); //将缓冲区中的内容写到指定文件中
int myread(int fd, int len); //读出指定文件中从读写指针开始的长度为len的内容到用户空间中
int do_read(int fd, int len, char *text); //读出指定文件中从读写指针开始的长度为len的内容到用户空间的text中
void my_exitsys(); //退出文件系统
void extractDir(char *dirname, char * dir,char *filename);
int findFreeBlock();
//启动文件函数startsys()
void startsys()
{
int pos = 0; //记录写入虚拟磁盘的位置
int i,j;
FILE* fp;
fcb* root;
block0 *guide;
//读入文件系统到虚拟磁盘中
if((fp = fopen(filesys_name,"r")) == NULL)
{
printf("The file system doesn't exist, and then it will be created by myformat().\n");
my_format();
}
else
{
printf("The file system exists, and then it will be written to Myvhard.\n");
for(i = 0;i < (SIZE / BLOCKSIZE);i ++)
{
fread(buf,BLOCKSIZE,1,fp);
for(j = 0;j < BLOCKSIZE;j ++)
{
myvhard[pos + j] = buf[j];
}
pos += BLOCKSIZE;
}
fclose(fp);
}
root = (fcb*)(myvhard + 5 * BLOCKSIZE);
strcpy(openfile[0].filename, root->filename);
strcpy(openfile[0].exname, root->exname);
openfile[0].attribute = root->attribute;
openfile[0].time = root->time;
openfile[0].date = root->date;
openfile[0].first = root->first;
openfile[0].length = root->length;
openfile[0].father = 0;
strcpy(openfile[0].dir, "\\root\\");
openfile[0].count = 0;
openfile[0].fcbstate = 0;
openfile[0].topenfile = 1; //打开表项是否为空,1为非空,0为空
}
//磁盘格式化函数my_format()
void my_format()
{
//这里都只能用从虚拟磁盘空间中分配空间给引导区,Fat1,fat2,不能直接成变量
FILE *fp;
block0* guide;
fat *fat1,*fat2;
fcb *root;
int i;
char buf[SIZE];
time_t now_time;
struct tm *lc_time;
guide = (block0 *)myvhard;
fat1 = (fat*)(myvhard + BLOCKSIZE);
fat2 = (fat*)(myvhard + BLOCKSIZE * 3);
root = (fcb*)(myvhard + BLOCKSIZE * 5);
//初始化引导块
strcpy(guide->information,"Every block's size is 1024 bytes,and the number of the block is 1000.");
guide->root = 5;
guide->startblock = (unsigned char *)root; //虚拟磁盘数据区开始位置
//初始化fat1,fat2,前5个磁盘块已经分配出去了
for(i = 0;i < 5;i++)
{
fat1->id = END;
fat2->id = END;
fat1 ++;
fat2 ++;
}
fat1->id = 6;
fat2->id = 6;
fat1++;
fat2++;
fat1->id = END;
fat2->id = END;
fat1++;
fat2++;
for(i = 7;i < (SIZE / BLOCKSIZE);i++)
{
fat1->id = FREE;
fat2->id = FREE;
fat1++;
fat2++;
}
//初始化根目录文件
strcpy(root->filename, ".");
strcpy(root->exname,"");
root->attribute = 0;
time(&now_time); //时间:秒
lc_time = localtime(&now_time);
root->time = lc_time->tm_hour * 2048 + lc_time->tm_min * 32 + lc_time->tm_sec / 2;
root->date = (lc_time->tm_year - 80) * 512 + (lc_time->tm_mon + 1) * 32 + lc_time->tm_mday;
root->first = 5;
root->length = 2 * sizeof(fcb); //应该是文件体长度吧
root->free = 1; //表示目录项是否为空,0表示空,1表示已分配 ???
root++;
time(&now_time);
lc_time = localtime(&now_time);
strcpy(root->filename, "..");
strcpy(root->exname, "");
root->attribute = 0;
root->time = lc_time->tm_hour * 2048 + lc_time->tm_min * 32 + lc_time->tm_sec / 2;
root->date = (lc_time->tm_year - 80) * 512 + (lc_time->tm_mon + 1) * 32 + lc_time->tm_mday;
root->first = 5;
root->length = 2 * sizeof(fcb);
root->free = 1;
fp = fopen(filesys_name,"w");
fwrite(myvhard,SIZE,1,fp);
fclose(fp);
}
//更改当前目录函数
int my_cd(char *dirname)
{
char *dir,*next;
int fd,tmp,father;
dir = strtok(dirname,"\\");
next = strtok(NULL,"\\");
//这里!next的目的是判断后面是否还有其他目录
if(strcmp(dir,".") == 0)
{
if(!next)
{
printf("It's current dir!\n");
return OK;
}
else
{
dir = next;
next = strtok(NULL,"\\");
}
}
else if(strcmp(dir,"..") == 0)
{
if(curdir) //判断是不是根目录,根目录在用户打开表中为0
{
tmp = openfile[curdir].father; //父目录不用打开吗,是本来就在内存里了吗?
my_close(curdir);
curdir = tmp;
if(!next)
return OK;
else
{
dir = next;
next = strtok(NULL,"\\");
}
}
else
{
printf("the current dir is root dir!\n");
return ERROR;
}
}
else if(strcmp(dir,"root") == 0)
{
while(curdir) //循环直到当前目录为根目录
{
tmp = openfile[curdir].father;
my_close(curdir);
curdir = tmp;
}
dir = next;
next = strtok(NULL,"\\");
}
while(dir)
{
//获得fd,并将打开的目录定为当前目录
fd = my_open(dir);
if(openfile[fd].attribute)
{
printf("this is not a dir.\n");
return ERROR;
}
else
{
if(fd == -2)
return ERROR;
else if(fd != ERROR)
curdir = fd;
else
{
printf("Failed to open the dir!\n");
return ERROR;
}
}
dir = next;
next = strtok(NULL,"\\");
}
//printOpenFile();
return OK;
}
//创建子目录函数
void my_mkdir(char* dirname)
{
int num,blk_num,i,fd,value,father;
fcb *fcbptr, *pos;
fat *fat1, *fat2;
time_t now;
struct tm *lc_time;
char text[MAXTEXT];
char dir[80] = "",filename[20] ,dir_tmp[80];
char *fname,*exname;
//这里dir_tmp是因为不能直接把openfile[curdir].dir传进my_cd()
strcpy(dir_tmp,openfile[curdir].dir);
fat1 = (fat*)(myvhard + BLOCKSIZE);
fat2 = (fat*)(myvhard + 3 * BLOCKSIZE);
//提取出指定目录和创建的目录名,并切换到指定目录
extractDir(dirname,dir,filename);
if(!(strcmp(dir,"") == 0))
value = my_cd(dir);
if(value == ERROR)
return;
fname = strtok(filename,".");
exname = strtok(NULL,".");
if(exname != NULL)
{
printf("the dir doesn't have the exname!\n");
my_cd(dir_tmp);
return;
}
//读取当前目录的内容,判断子目录是否存在
openfile[curdir].count = 0;
num = do_read(curdir,openfile[curdir].length,text);
fcbptr = (fcb*)text;
for(i = 0; i < (num / sizeof(fcb)); i++)
{
if((!strcmp(fcbptr->filename,fname)))
{
printf("The filename to create has been exist!\n");
return;
}
fcbptr ++;
}
//寻找空闲目录项的位置
fcbptr = (fcb *)text;
for(i = 0; i < (num / sizeof(fcb)); i++)
{
if(fcbptr->free == 0)
break;
fcbptr++;
}
//寻找空闲磁盘块
blk_num = findFreeBlock();
if(blk_num == ERROR)
return;
(fat1 + blk_num)->id = END;
(fat2 + blk_num)->id = END;
//初始化目录文件的fcb
strcpy(fcbptr->filename, fname);
strcpy(fcbptr->exname, "");
fcbptr->attribute = 0;
time(&now);
lc_time = localtime(&now);
fcbptr->time = lc_time->tm_hour * 2048 + lc_time->tm_min * 32 + lc_time->tm_sec / 2;
fcbptr->date = (lc_time->tm_year - 80) * 512 + (lc_time->tm_mon + 1) * 32 + lc_time->tm_mday;
fcbptr->first = blk_num;
fcbptr->length = 2 * sizeof(fcb);
fcbptr->free = 1;
//父目录指针定位到空闲目录项
openfile[curdir].count = i * sizeof(fcb);
do_write(curdir, (char *)fcbptr, sizeof(fcb), 2);
//更新当前目录中第一个fcb的内容
fcbptr = (fcb*)text;
fcbptr->length = openfile[curdir].length;
openfile[curdir].count = 0;
do_write(curdir, (char *)fcbptr, sizeof(fcb), 2);
//打开子目录文件,创建"."和".."两个目录项
fd = my_open(fname);
if(fd == ERROR)
return;
fcbptr = (fcb*)malloc(sizeof(fcb));
strcpy(fcbptr->filename, ".");
strcpy(fcbptr->exname, "");
fcbptr->attribute = 0;
time(&now);
lc_time = localtime(&now);
fcbptr->time = lc_time->tm_hour * 2048 + lc_time->tm_min * 32 + lc_time->tm_sec / 2;
fcbptr->date = (lc_time->tm_year - 80) * 512 + (lc_time->tm_mon + 1) * 32 + lc_time->tm_mday;
fcbptr->first = blk_num;
fcbptr->length = 2 * sizeof(fcb);
fcbptr->free = 1;
do_write(fd, (char *)fcbptr, sizeof(fcb),2);
pos = (fcb*)text;
strcpy(fcbptr->filename, "..");
strcpy(fcbptr->exname, "");
fcbptr->attribute = pos->attribute;
fcbptr->time = pos->time;
fcbptr->date = pos->date;
fcbptr->first = pos->first;
fcbptr->length = pos->length;
fcbptr->free = 1;
do_write(fd, (char *)fcbptr, sizeof(fcb), 2);
my_close(fd);
//更新当前目录的父目录中该项的长度
father = openfile[curdir].father;
openfile[father].count = father;
num = do_read(father,openfile[father].length,text);
fcbptr = (fcb*)text;
//printFcb(father);
for(i = 0; i < (num / sizeof(fcb)); i++)
{
if((!strcmp(fcbptr->filename,openfile[curdir].filename)) && (!strcmp(fcbptr->exname,openfile[curdir].exname)))
{
fcbptr->length = openfile[curdir].length;
break;
}
fcbptr ++;
}
openfile[father].count = i * sizeof(fcb);
do_write(father,(char *)fcbptr, sizeof(fcb), 2);
openfile[curdir].fcbstate = 1;
//printFcb(father);
//printOpenFile();
my_cd(dir_tmp);
}
//删除子目录函数
void my_rmdir(char *dirname)
{
int num1,num2,blk_num,i,fd,j;
fcb *fcbptr,*fcbptr2;
fat *fat1,*fat2,*fatptr1,*fatptr2;
char text[MAXTEXT],text2[MAXTEXT];
char dir[80] = "",filename[8] = "",dir_tmp[80] = "";
//这里dir_tmp是因为不能直接把openfile[curdir].dir传进去
strcpy(dir_tmp,openfile[curdir].dir);
//提取路径和目录名
extractDir(dirname,dir,filename);
if(!(strcmp(dir,"") == 0))
my_cd(dir);
fat1 = (fat*)(myvhard + BLOCKSIZE);
fat2 = (fat*)(myvhard + 3 * BLOCKSIZE);
//判断要删除的是不是当前目录或者父目录
if(strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
{
printf("Can't remove the current dir and the father dir!\n");
return;
}
//读取父目录文件内容
openfile[curdir].count = 0;
num1 = do_read(curdir, openfile[curdir].length, text);
fcbptr = (fcb*)text;
for(i = 0; i < (num1 / sizeof(fcb)); i ++)
{
if(strcmp(fcbptr->filename, filename) == 0 && strcmp(fcbptr->exname, "") == 0)
break;
fcbptr++;
}
if( i == (num1 / sizeof(fcb)))
{
printf("The dir to delete doesn't exist!\n");
return;
}
//打开要删除的文件
fd = my_open(filename);
num2 = do_read(fd, openfile[fd].length, text2);
fcbptr2 = (fcb*)text2;
//判断指定文件能不能删除
for(j = 0; j < (num2 / sizeof(fcb)); j++)
{
if(strcmp(fcbptr2->filename, ".") && strcmp(fcbptr2->filename, "..") && strcmp(fcbptr2->filename, ""))
{
my_close(fd);
printf("Failed to remove this dir.Directory not empty!\n");
return;
}
fcbptr2++;
}
//回收指定目录占据的磁盘块
blk_num = openfile[fd].first;
while(blk_num != END)
{
fatptr1 = fat1 + blk_num;
fatptr2 = fat2 + blk_num;
blk_num = fatptr1->id;
fatptr1->id = FREE;
fatptr2->id = FREE;
}
my_close(fd);
strcpy(fcbptr->filename, "");
fcbptr->free = 0;
openfile[curdir].count = i * sizeof(fcb);
do_write(curdir, (char *)fcbptr, sizeof(fcb), 2);
openfile[curdir].fcbstate = 1;
my_cd(dir_tmp);
}
//显示目录函数
void my_ls()
{
fcb *fcbptr;
char text[MAXTEXT];
int num, i;
openfile[curdir].count = 0;
num = do_read(curdir, openfile[curdir].length, text);
fcbptr = (fcb *)text;
for(i = 0; i < num / sizeof(fcb); i++)
{
if(fcbptr->free)
{
if(!fcbptr->attribute)
printf("%s\\\t\t<DIR>\t\t%d/%d/%d\t%02d:%02d:%02d\n", fcbptr->filename, (fcbptr->date >> 9) + 1980, (fcbptr->date >> 5) & 0x000f, fcbptr->date & 0x001f, fcbptr->time >> 11, (fcbptr->time >> 5) & 0x003f, fcbptr->time & 0x001f * 2);
else
{
printf("%s.%s\t\t%dB\t\t%d/%d/%d\t%02d:%02d:%02d\t\n", fcbptr->filename, fcbptr->exname, (int)(fcbptr->length), (fcbptr->date >> 9) + 1980, (fcbptr->date >> 5) & 0x000f, fcbptr->date & 0x001f, fcbptr->time >> 11, (fcbptr->time >> 5) & 0x3f, fcbptr->time & 0x1f * 2);
}
}
fcbptr++;
}
}
//创建文件函数
int my_create(char *dirname)
{
int num,blk_num,i,father;
time_t now;
struct tm *lc_time;
fat *fat1,*fat2;
fcb *pos; //pos用来定位搜索到了什么目录项
char *fname,*exname;
char dest_dir[80] = "",filename[20] = "",text[MAXTEXT] = "",dir_tmp[80] = ""; //要创建文件的目录
strcpy(dir_tmp,openfile[curdir].dir);
fat1 = (fat*)(myvhard + BLOCKSIZE);
fat2 = (fat*)(myvhard + 3 * BLOCKSIZE);
extractDir(dirname, dest_dir, filename);
if(!(strcmp(dest_dir,"") == 0))
my_cd(dest_dir);
fname = strtok(filename,".");
exname = strtok(NULL,".");
if(strcmp(fname,"") == 0)
{
printf("the filename is NULL!\n");
return ERROR;
}
if(!exname)
{
printf("the extern name is NULL!\n");
return ERROR;
}
//要检索父目录文件中是否已经有同名文件,父目录文件中存储了子文件的FCB
openfile[curdir].count = 0;
num = do_read(curdir,openfile[curdir].length,text);
if(num == ERROR)
{
printf("Read father file failed!\n");
return ERROR;
}
pos = (fcb*)text;
for(i = 0; i< (num / sizeof(fcb)); i++)
{
//查找是否有同名文件
if((!strcmp(pos->filename,fname)) && (!(strcmp(pos->exname,exname))))
{
printf("The filename to create has been exist!\n");
return ERROR;
}
pos ++;
}
//确定了没有重名文件,然后开始创建文件。
//要在父目录中添加子文件的目录项
pos = (fcb *)text;
for(i = 0; i < num / sizeof(fcb); i++)
{
if(pos->free == 0)
break;
pos++;
}
blk_num = findFreeBlock();
if(blk_num == ERROR)
return ERROR;
(fat1 + blk_num)->id = END;
(fat2 + blk_num)->id = END;
//初始化子文件的fcb
strcpy(pos->filename, fname);
strcpy(pos->exname, exname);
pos->attribute = 1;
time(&now);
lc_time = localtime(&now);
pos->time = lc_time->tm_hour * 2048 + lc_time->tm_min * 32 + lc_time->tm_sec / 2;
pos->date = (lc_time->tm_year - 80) * 512 + (lc_time->tm_mon + 1) * 32 + lc_time->tm_mday;
pos->first = blk_num;
pos->length = 0; //应该是文件体长度吧
pos->free = 1; //表示目录项是否为空,0表示空,1表示已分配
openfile[curdir].count = i * sizeof(fcb);
do_write(curdir, (char *)pos, sizeof(fcb), 2);
//更新当前目录的第一个目录项
pos = (fcb *)text;
pos->length = openfile[curdir].length;
openfile[curdir].count = 0;
do_write(curdir, (char *)pos, sizeof(fcb), 2);
openfile[curdir].fcbstate = 1;
//更新父目录中的当前目录的目录项
father = openfile[curdir].father;
openfile[father].count = father;
num = do_read(father,openfile[father].length,text);
pos = (fcb*)text;
//printFcb(father);
for(i = 0; i < (num / sizeof(fcb)); i++)
{
if((!strcmp(pos->filename,openfile[curdir].filename)) && (!strcmp(pos->exname,openfile[curdir].exname)))
{
pos->length = openfile[curdir].length;
break;
}
pos ++;
}
openfile[father].count = i * sizeof(fcb);
do_write(father,(char *)pos, sizeof(fcb), 2);
my_cd(dir_tmp);
}
//删除文件函数
void my_rm(char *dirname)
{
fcb *pos;
fat *fat1, *fat2, *fatptr1, *fatptr2;
char *fname, *exname, text[MAXTEXT];
int num,i,blk_num;
char dir[80] = "",filename[20] = "",dir_tmp[80] = "";
strcpy(dir_tmp,openfile[curdir].dir);
//提取路径和目录名
extractDir(dirname,dir,filename);
if(!(strcmp(dir,"") == 0))
my_cd(dir);
fat1 = (fat *)(myvhard + BLOCKSIZE);
fat2 = (fat *)(myvhard + 3 * BLOCKSIZE);
fname = strtok(filename, ".");
exname = strtok(NULL, ".");
if(strcmp(fname,"") == 0)
{
printf("the filename is NULL!\n");
return;
}
if(!exname)
{
printf("the extern name is NULL!\n");
return;
}
openfile[curdir].count = 0;
num = do_read(curdir, openfile[curdir].length, text);
pos = (fcb *)text;
for(i = 0; i < (num / sizeof(fcb)); i++)
{
if(strcmp(pos->filename, fname) == 0 && strcmp(pos->exname, exname) == 0)
break;
pos++;
}
if(i == num / sizeof(fcb))
{
printf("Error,the file is not exist.\n");
return;
}
blk_num = pos->first;
while(blk_num != END)
{
fatptr1 = fat1 + blk_num;
fatptr2 = fat2 + blk_num;
blk_num = fatptr1->id;
fatptr1->id = FREE;
fatptr2->id = FREE;
}
strcpy(pos->filename, "");
pos->free = 0;
openfile[curdir].count = i * sizeof(fcb);
do_write(curdir, (char *)pos, sizeof(fcb), 2);
openfile[curdir].fcbstate = 1;
my_cd(dir_tmp);
}
//打开文件函数
int my_open(char* dirname)
{
int i,num,value;
int fd = -1;
fcb* fcbptr;
char *fname,exname[3] = "",*tmp;
char dest_dir[80] = "",filename[20] = "",text[MAXTEXT]; //要创建文件的目录
if(strlen(dirname) == 0)
return -2;
extractDir(dirname, dest_dir, filename);
if(!(strcmp(dest_dir,"") == 0))
value = my_cd(dest_dir);
fname = strtok(filename,".");
tmp = strtok(NULL,".");
if(tmp)
strcpy(exname,tmp);
else
strcpy(exname, "");
for(i = 0; i < MAXOPENFILE; i++)
{
if(!strcmp(fname,openfile[i].filename) && !strcmp(exname,openfile[i].exname))
{
return i;
}
}
//查看父目录中的fcb
openfile[curdir].count = 0;
num = do_read(curdir, openfile[curdir].length, text);
fcbptr = (fcb*)text;
for(i = 0; i< (num / sizeof(fcb)); i++)
{
if(strcmp(fcbptr->filename, fname) == 0 && strcmp(fcbptr->exname,exname) == 0)
break;
fcbptr++;
}
if(i == num / sizeof(fcb))
{
printf("Error,the file is not exist.\n");
return ERROR;
}
//查找用户打开表的空闲位置
for(i = 0; i < MAXOPENFILE; i++)
{
if(openfile[i].topenfile == 0)
{
fd = i;
break;
}
}
if(fd == -1)
{
printf("用户打开表无空闲表项!\n");
return ERROR;
}
strcpy(openfile[fd].filename, fname);
strcpy(openfile[fd].exname, fcbptr->exname);
openfile[fd].attribute = fcbptr->attribute;
openfile[fd].time = fcbptr->time;
openfile[fd].date = fcbptr->date;
openfile[fd].first = fcbptr->first;
openfile[fd].length = fcbptr->length;
strcpy(openfile[fd].dir, openfile[curdir].dir);
strcat(openfile[fd].dir, filename);
if(!fcbptr->attribute)
strcat(openfile[fd].dir, "\\");
openfile[fd].father = curdir;
openfile[fd].count = 0;
openfile[fd].fcbstate = 0;
openfile[fd].topenfile = 1;
return fd;
}
//关闭文件函数
void my_close(int fd)
{
fcb *fcbptr, *pos;
int father,num,i;
char text[MAXTEXT];
//检查fd的有效性
if(fd < 0 || fd >= MAXOPENFILE)
{
printf("the input fd is out of range!\n");
return;
}
if(openfile[fd].fcbstate)
{
fcbptr = (fcb *)malloc(sizeof(fcb));
strcpy(fcbptr->filename, openfile[fd].filename);
strcpy(fcbptr->exname,openfile[fd].exname);
fcbptr->attribute = openfile[fd].attribute;
fcbptr->time = openfile[fd].time;
fcbptr->date = openfile[fd].date;
fcbptr->first = openfile[fd].first;
fcbptr->length = openfile[fd].length;
father = openfile[fd].father;
//读取父目录文件内容
openfile[father].count = 0;
num = do_read(father, openfile[father].length, text);
pos = (fcb*)text;
//定位到文件对应的目录项
for(i = 0; i < (num / sizeof(fcb)); i ++)
{
if(strcmp(pos->filename, openfile[fd].filename) == 0 && strcmp(pos->exname, openfile[fd].exname) == 0)
break;
pos++;
}
openfile[father].count = i * sizeof(fcb);
do_write(father, (char *)fcbptr, sizeof(fcb), 2);
openfile[fd].fcbstate = 0;
}
//初始化用户打开表
strcpy(openfile[fd].filename, "");
strcpy(openfile[fd].exname, "");
openfile[fd].topenfile = 0;
//printOpenFile();
}
//写文件函数
int my_write(int fd)
{
fat *fat1,*fat2,*fatptr1,*fatptr2;
int wstyle,num_cur,num = 0,len;
char text[MAXTEXT];
unsigned short blk_num;
fat1 = (fat*)(myvhard + BLOCKSIZE);
fat2 = (fat*)(myvhard + 3 * BLOCKSIZE);
//检查fd的有效性
if(fd < 0 || fd >= MAXOPENFILE)
{
printf("the input fd is out of range!\n");
return ERROR;
}
//选择写入方式
while(1)
{
printf("Please choose the number of the style that the order file be written:\n1.截断写\t2.覆盖写\t3.追加写\n");
scanf("%d",&wstyle);
if(wstyle > 0 && wstyle < 4)
break;
printf("the wstyle must be in [1,3]!");
}
//从键盘输入内容
getchar(); //需要把之前缓冲区中的换行符读出来,否则对后面读入text会有影响。
switch(wstyle)
{
case 1:
blk_num = openfile[fd].first;
fatptr1 = fat1 + blk_num;
fatptr2 = fat2 + blk_num;
blk_num = fatptr1->id;
fatptr1->id = END;
fatptr2->id = END;
while(blk_num != END)
{
fatptr1 = fat1 + blk_num;
fatptr2 = fat2 + blk_num;
blk_num = fatptr1->id;
fatptr1->id = FREE;
fatptr2->id = FREE;
}
openfile[fd].count = 0;
openfile[fd].length = 0;
break;
case 2:
openfile[fd].count = 0;
//文件的读写指针应该不需要变
break;
case 3:
openfile[fd].count = openfile[fd].length;
default:
break;
}
printf("Please input text that you want to write:\n");
while(gets(text))
{
len = strlen(text);
num_cur = do_write(fd,text,len,wstyle);
if(num_cur != ERROR)
num += num_cur;
if(num_cur < len)
{
printf("Write ERROR!\n");
break;
}
}
return num;
}
//实际写文件函数
int do_write(int fd,char *text,int len, char wstyle)
{
fat *fat1,*fat2,*fatptr1,*fatptr2;
unsigned short blk_num;
int blk_off,i,num,cur_blk,father;
unsigned char *pos;
fcb *fcbptr;
char text2[MAXTEXT];
fat1 = (fat*)(myvhard + BLOCKSIZE);
fat2 = (fat*)(myvhard + 3 * BLOCKSIZE);
//逻辑块块号
blk_num = openfile[fd].count / BLOCKSIZE;
//块内偏移量
blk_off = openfile[fd].count;
fatptr1 = fat1 + openfile[fd].first;
fatptr2 = fat2 + openfile[fd].first;
cur_blk = openfile[fd].first;
//根据fat表找到要写入的起始逻辑块的位置
for(i = 0;i < blk_num; i++)
{
cur_blk = fatptr1->id;
blk_off = blk_off - BLOCKSIZE;
fatptr1 = fat1 + cur_blk;
}
//写的同时还要考虑写到哪里
num = 0;
while(num < len)
{
pos = (unsigned char *)(myvhard + cur_blk * BLOCKSIZE);
//读要整块一起读,写也要整块一起写
for( i = 0; i < BLOCKSIZE; i++)
buf[i] = pos[i];
for(;blk_off < BLOCKSIZE; blk_off++)
{
buf[blk_off] = text[num];
num++;
openfile[fd].count++;
//这是写入长度小于(磁盘块大小 - 初始块内偏移量)的时候。
if(num == len)
break;
}
for( i = 0; i< BLOCKSIZE; i++)
{
pos[i] = buf[i];
}
//如果写入长度大于一个(磁盘块大小 - 初始块内偏移量)的时候
if(num < len)
{
cur_blk = fatptr1->id;
if(cur_blk == END)
{
cur_blk = findFreeBlock();
if(cur_blk == ERROR)
break;
fatptr1->id = cur_blk;
fatptr2->id = cur_blk;
fatptr1 = fatptr1 + cur_blk;
fatptr2 = fatptr2 + cur_blk;
fatptr1->id = END;
fatptr2->id = END;
}
else
{
fatptr1 = fat1 + cur_blk;
fatptr1 = fat1 + cur_blk;
}
blk_off = 0;
}
}
if(openfile[fd].count > openfile[fd].length)
{
openfile[fd].length = openfile[fd].count;
}
//文件的fcb是否被修改
openfile[fd].fcbstate = 1;
return num;
}
//读文件函数
int my_read(int fd,int len)
{
int num;
char text[MAXTEXT];
if(fd < 0 || fd >= MAXOPENFILE)
{
printf("the input fd is out of range!\n");
return ERROR;
}
openfile[fd].count = 0;
num = do_read(fd,len,text);
if(num == ERROR)
{
printf("Read fails!\n");
return ERROR;
}
else
{
printf("%s\n",text);
}
return num;
}
//实际读文件函数
int do_read(int fd, int len, char *text)
{
fat* fat1;
fat* fat_ptr;
int cur_blk,i;
unsigned short blk_num;
unsigned char* pos;
int blk_off,num = 0; // num记录读取长度
//判断读取长度是否超过文件长度
if((openfile[fd].count + len) > openfile[fd].length)
{
printf("the read length is more than the file's length!\n");
return ERROR;
}