-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesys.c
1451 lines (1329 loc) · 29 KB
/
filesys.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
#include<stdio.h>
#include<malloc.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>
#define BLOCKSIZE 1024
#define SIZE 1024000
#define END 65535
#define FREE 0
#define ROOT_BLOCKNUM 2
#define MAX_OPEN_FILE 10
#define MAX_TXT_SIZE 10000
typedef struct FCB
{
char filename[8]; /*8B文件名*/
char exname[3]; /*3B扩展名*/
unsigned char attribute; /*文件属性字段*/
char retainbyte[10] ; /*10B保留字*/
unsigned short time; /*文件创建时间*/
unsigned short date; /*文件创建日期*/
unsigned short first; /*首块号*/
unsigned long length; /*文件大小*/
}fcb;
/*
文件分配表 file allocation table
*/
typedef struct FAT
{
unsigned short id;
}fat;
/*
属性对照表
位 7 6 5 4 3 2 1 0
属性 保留 保留 存档 子目录 卷标 系统文件 隐藏 只读
*/
typedef struct USEROPEN
{
char filename[8]; /*8B文件名*/
char exname[3]; /*3B扩展名*/
unsigned char attribute; /*文件属性字段*/
char retainbyte[10] ; /*10B保留字*/
unsigned short time; /*文件创建时间*/
unsigned short date; /*文件创建日期*/
unsigned short first; /*首块号*/
unsigned long length; /*文件大小*/
char free; /*表示目录项是否为空 0空1分配*/
int father; /*父目录的文件描述符*/
int dirno; /*相应打开文件的目录项在父目录文件中的盘块号*/
int diroff; /*相应打开文件的目录项在父目录文件的dirno盘块中的目录项序号*/
char dir[MAX_OPEN_FILE][80]; /*相应打开文件所在的目录*/
int count; /*读写指针在文件中的位置*/
char fcbstate; /*是否修改了文件的FCB内容,修改置1,否则为0*/
char topenfile; /*表示该用户打开的表项是否为空,若值为0,表示为空*/
}useropen;
typedef struct BLOCK0
{
unsigned short fbnum;
char information[200];
unsigned short root;
unsigned char *startblock;
}block0;
/*全局变量定义*/
unsigned char *myvhard; /*指向虚拟磁盘的起始地址*/
useropen openfilelist[MAX_OPEN_FILE]; /*用户打开文件表数组*/
useropen *ptrcurdir; /*指向用户打开文件表中的当前目录所在打开文件表项的位置*/
int curfd;
char currentdir[80]; /*记录当前目录的文件名*/
unsigned char *startp; /*记录虚拟磁盘上数据区开始位置*/
char filename[]="c:\\myfilesys"; /*虚拟空间保存路径*/
unsigned char buffer[SIZE];
/*函数声明*/
void startsys();
void my_format();
void my_cd(char *dirname);
void my_mkdir(char *dirname);
void my_rmdir(char *dirname);
void my_ls();
void my_create(char *filename);
void my_rm(char *filename);
int my_open(char *filename);
int my_close(int fd);
int my_write(int fd);
int do_write(int fd,char *text,int len,char wstyle);
unsigned short findFree();
int my_read(int fd,int len);
int do_read(int fd,int len,char *text);
void my_exitsys();
/*函数设计*/
/*文件系统初始化*/
/*
原型声明: void startsys()
功能描述: 文件系统初始化,初始化所建立的文件系统
输入:无
输出:无
函数功能实现算法描述:
1)申请磁盘空间
2)打开系统磁盘,若不存在,创建新的系统磁盘,并格式化
3)初始化用户打开文件表,将表项0分配给根目录文件使用
并填写根目录文件的相关信息
4)将ptrcurdir指向该用户打开文件表项
5)将当前目录设置为根目录
*/
void startsys()
{
FILE *fp;
int i;
myvhard=(unsigned char *)malloc(SIZE);
memset(myvhard,0,SIZE);
fp=fopen(filename,"r");
if(fp)
{
fread(buffer,SIZE,1,fp);
fclose(fp);
if(buffer[0]!=0xaa||buffer[1]!=0xaa)
{
printf("myfilesys is not exist,begin to creat the file...\n");
my_format();
}
else
{
for(i=0;i<SIZE;i++)
myvhard[i]=buffer[i];
}
}
else
{
printf("myfilesys is not exist,begin to creat the file...\n");
my_format();
}
strcpy(openfilelist[0].filename,"root");
strcpy(openfilelist[0].exname,"di");
openfilelist[0].attribute=0x2d;
openfilelist[0].time=((fcb *)(myvhard+5*BLOCKSIZE))->time;
openfilelist[0].date=((fcb *)(myvhard+5*BLOCKSIZE))->date;
openfilelist[0].first=((fcb *)(myvhard+5*BLOCKSIZE))->first;
openfilelist[0].length=((fcb *)(myvhard+5*BLOCKSIZE))->length;
openfilelist[0].free=1;
openfilelist[0].dirno=5;
openfilelist[0].diroff=0;
openfilelist[0].count=0;
openfilelist[0].fcbstate=0;
openfilelist[0].topenfile=0;
openfilelist[0].father=0;
memset(currentdir,0,sizeof(currentdir));
strcpy(currentdir,"\\root\\");
strcpy(openfilelist->dir[0],currentdir);
startp=((block0 *)myvhard)->startblock;
ptrcurdir=&openfilelist[0];
curfd=0;
}
/*
原型声明: void my_format()
功能描述: 对虚拟磁盘进行格式化,布局虚拟磁盘,建立根目录文件
输入:无
输出:无
函数功能实现算法描述:
虚拟磁盘空间布局
1块 2块 2块 995块
引导块 FAT1 FAT2 数据区
虚拟磁盘一共划分成1000个磁盘块
每块1024个字节,磁盘空间布局如上
将数据区的第一块(即虚拟磁盘的第6块)分配给根目录文件
*/
void my_format()
{
FILE *fp;
fat *fat1,*fat2;
block0 *b0;
time_t *now;
struct tm *nowtime;
unsigned char *p;
fcb *root;
int i;
p=myvhard;
b0=(block0 *)p;
fat1=(fat *)(p+BLOCKSIZE);
fat2=(fat*)(p+3*BLOCKSIZE);
/*
引导块
*/
b0->fbnum=0xaaaa; /*文件系统魔数 10101010*/
b0->root = 5;
strcpy(b0->information,"My FileSystem Ver 1.0 \n Blocksize=1KB Whole size=1000KB Blocknum=1000 RootBlocknum=2\n");
/*
FAT1,FAT2
前面五个磁盘块已分配,标记为END
*/
fat1->id=END;
fat2->id=END;
fat1++;fat2++;
fat1->id=END;
fat2->id=END;
fat1++;fat2++;
fat1->id=END;
fat2->id=END;
fat1++;fat2++;
fat1->id=END;
fat2->id=END;
fat1++;fat2++;
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++;
}
/*
创建根目录文件root,将数据区的第一块分配给根目录区
在给磁盘上创建两个特殊的目录项:".","..",
除了文件名之外,其它都相同
*/
p+=BLOCKSIZE*5;
root=(fcb *)p;
strcpy(root->filename,".");
strcpy(root->exname,"di");
root->attribute=40;
now=(time_t *)malloc(sizeof(time_t));
time(now);
nowtime=localtime(now);
root->time=nowtime->tm_hour*2048+nowtime->tm_min*32+nowtime->tm_sec/2;
root->date=(nowtime->tm_year-80)*512+(nowtime->tm_mon+1)*32+nowtime->tm_mday;
root->first=5;
root->length=2*sizeof(fcb);
root++;
strcpy(root->filename,"..");
strcpy(root->exname,"di");
root->attribute=40;
time(now);
nowtime=localtime(now);
root->time=nowtime->tm_hour*2048+nowtime->tm_min*32+nowtime->tm_sec/2;
root->date=(nowtime->tm_year-80)*512+(nowtime->tm_mon+1)*32+nowtime->tm_mday;
root->first=5;
root->length=2*sizeof(fcb);
root++;
for(i=2;i<BLOCKSIZE*2/sizeof(fcb);i++,root++)
{
root->filename[0]='\0';
}
fp=fopen(filename,"w");
b0->startblock=p+BLOCKSIZE*4;
fwrite(myvhard,SIZE,1,fp);
free(now);
fclose(fp);
}
/**/
/*
原型声明: void my_exitsys()
功能描述: 退出文件系统
输入: 无
输出: 无
函数功能实现算法描述:
*/
void my_exitsys()
{
FILE *fp;
fcb *rootfcb;
char text[MAX_TXT_SIZE];
while(curfd)
curfd=my_close(curfd);
if(openfilelist[curfd].fcbstate)
{
openfilelist[curfd].count=0;
do_read(curfd,openfilelist[curfd].length,text);
rootfcb=(char *)text;
rootfcb->length=openfilelist[curfd].length;
openfilelist[curfd].count=0;
do_write(curfd,text,openfilelist[curfd].length,2);
}
fp=fopen(filename,"w");
fwrite(myvhard,SIZE,1,fp);
free(myvhard);
fclose(fp);
}
/*
原型声明: int do_read(int fd,int len,char *text)
功能描述: 实际读文件函数,读出指定文件中从指定指针开始的长度
为len的内容到用户空间的text中
输入:
fd open()函数的返回值,文件的描述符
len 要求从文件中读出的字节数
text 指向存放读出数据的用户区地址
输出:
实际读出的字节数
函数功能实现算法描述:
*/
int do_read(int fd,int len,char *text)
{
unsigned char *buf;
unsigned short bknum;
int off,i,lentmp;
unsigned char *bkptr;
char *txtmp,*p;
fat *fat1,*fatptr;
fat1=(fat *)(myvhard+BLOCKSIZE);
lentmp = len;
txtmp=text;
/*
申请1024B空间作为缓冲区buffer
*/
buf=(unsigned char *)malloc(1024);
if(buf==NULL)
{
printf("malloc failed!\n");
return -1;
}
off = openfilelist[fd].count;
bknum = openfilelist[fd].first;
fatptr = fat1+bknum;
while(off >= BLOCKSIZE)
{
off=off-BLOCKSIZE;
bknum=fatptr->id;
fatptr=fat1+bknum;
if(bknum == END)
{
printf("Error,the block is not exist.\n");
return -1;
}
}
bkptr=(unsigned char *)(myvhard+bknum*BLOCKSIZE);
//strncpy(buf,bkptr,BLOCKSIZE);
for(i=0;i<BLOCKSIZE;i++)
{
buf[i]=bkptr[i];
}
while(len > 0)
{
if(BLOCKSIZE-off > len)
{
//strncpy(txtmp,buf+off,len);
//len=len-len;
//openfilelist[fd].count+=len;
//off+=len;
for(p=buf+off;len>0;p++,txtmp++)
{
*txtmp=*p;
len--;
off++;
openfilelist[fd].count++;
}
}
else
{
//strncpy(txtmp,buf+off,BLOCKSIZE-off);
//len=len-(BLOCKSIZE-off);
//openfilelist[fd].count+=(BLOCKSIZE-off);
for(p=buf+off;p<buf+BLOCKSIZE;p++,txtmp++)
{
*txtmp=*p;
len--;
openfilelist[fd].count++;
}
off=0;
//txtmp+=(BLOCKSIZE-off);
bknum =fatptr->id;
fatptr = fat1+bknum;
bkptr=(unsigned char *)(myvhard+bknum*BLOCKSIZE);
//strncpy(buf,bkptr,BLOCKSIZE);
for(i=0;i<BLOCKSIZE;i++)
{
buf[i]=bkptr[i];
}
}
}
free(buf);
return lentmp-len;
}
/**/
/*
原型声明: int my_read(int fd,int len)
功能描述: 读文件函数
输入:
fd 打开文件的id
len 要读出字符的个数
输出: 返回实际读的字符的个数
函数功能实现算法描述:
*/
int my_read(int fd,int len)
{
char text[MAX_TXT_SIZE];
if(fd > MAX_OPEN_FILE)
{
printf("The File is not exist!\n");
return -1;
}
openfilelist[curfd].count=0;
if(do_read(fd,len,text)>0)
{
text[len]='\0';
printf("%s\n",text);
}
else
{
printf("Read Error!\n");
return -1;
}
return len;
}
/**/
/*
原型声明: int do_write(int fd,char *text,int len,char wstyle)
功能描述: 实际写文件函数
输入:
fd 当前打开的文件的id
text 指向要写入的内容的指针
len 本次要写入字节数
wstyle 写方式
输出: 实际写入的字节数
函数功能实现算法描述:
*/
int do_write(int fd,char *text,int len,char wstyle)
{
unsigned char *buf;
unsigned short bknum;
int off,tmplen=0,tmplen2=0,i,rwptr;
unsigned char *bkptr,*p;
char *txtmp,flag=0;
fat *fat1,*fatptr;
fat1=(fat *)(myvhard+BLOCKSIZE);
txtmp=text;
/*
申请临时1024B的buffer
*/
buf=(unsigned char *)malloc(BLOCKSIZE);
if(buf==NULL)
{
printf("malloc failed!\n");
return -1;
}
rwptr = off = openfilelist[fd].count;
bknum = openfilelist[fd].first;
fatptr=fat1+bknum;
while(off >= BLOCKSIZE )
{
off=off-BLOCKSIZE;
bknum=fatptr->id;
if(bknum == END)
{
bknum=fatptr->id=findFree();
if(bknum==END) return -1;
fatptr=fat1+bknum;
fatptr->id=END;
}
fatptr=fat1+bknum;
}
fatptr->id=END;
bkptr=(unsigned char *)(myvhard+bknum*BLOCKSIZE);
while(tmplen<len)
{
for(i=0;i<BLOCKSIZE;i++)
{
buf[i]=0;
}
//if(off)
//{
for(i=0;i<BLOCKSIZE;i++)
{
buf[i]=bkptr[i];
tmplen2++;
if(tmplen2==openfilelist[curfd].length)
break;
}
//}
//else
//{
//for(i=0;i<BLOCKSIZE;i++)
//{
// buf[i]=0;
//}
//}
for(p=buf+off;p<buf+BLOCKSIZE;p++)
{
*p=*txtmp;
tmplen++;
txtmp++;
off++;
if(tmplen==len)
break;
/*if((*p)==NULL)
{
break;
}*/
}
for(i=0;i<BLOCKSIZE;i++)
{
bkptr[i]=buf[i];
}
openfilelist[fd].count=rwptr+tmplen;
if(off>=BLOCKSIZE)
{
off=off-BLOCKSIZE;
bknum=fatptr->id;
if(bknum==END)
{
bknum=fatptr->id=findFree();
if(bknum==END) return -1;
fatptr=fat1+bknum;
fatptr->id=END;
}
fatptr=fat1+bknum;
bkptr=(unsigned char *)(myvhard+bknum*BLOCKSIZE);
}
}
free(buf);
if(openfilelist[fd].count>openfilelist[fd].length)
{
openfilelist[fd].length=openfilelist[fd].count;
}
openfilelist[fd].fcbstate=1;
return tmplen;
}
/**/
/*
原型声明: unsigned short findFree()
功能描述: 寻找下一个空闲盘块
输入: 无
输出: 返回空闲盘块的id
函数功能实现算法描述:
*/
unsigned short findFree()
{
unsigned short i;
fat *fat1,*fatptr;
fat1=(fat *)(myvhard+BLOCKSIZE);
for(i=6; i<END; i++)
{
fatptr=fat1+i;
if(fatptr->id == FREE)
{
return i;
}
}
printf("Error,Can't find free block!\n");
return END;
}
/*
原型声明: int findFreeO()
功能描述: 寻找空闲文件表项
输入: 无
输出: 返回空闲文件表项的id
函数功能实现算法描述:
*/
int findFreeO()
{
int i;
for(i=0;i<MAX_OPEN_FILE;i++)
{
if(openfilelist[i].free==0)
{
return i;
}
}
printf("Error,open too many files!\n");
return -1;
}
/**/
/*
原型声明: int my_write(int fd)
功能描述: 写文件函数
输入:
fd 打开文件的id
输出: 返回实际写的长度
函数功能实现算法描述:
*/
int my_write(int fd)
{
int wstyle=0,wlen=0;
fat *fat1,*fatptr;
unsigned short bknum;
unsigned char *bkptr;
char text[MAX_TXT_SIZE];
fat1=(fat *)(myvhard+BLOCKSIZE);
if(fd>MAX_OPEN_FILE)
{
printf("The file is not exist!\n");
return -1;
}
while(wstyle<1||wstyle>3)
{
printf("Please enter the number of write style:\n1.cut write\t2.cover write\t3.add write\n");
scanf("%d",&wstyle);
getchar();
switch(wstyle)
{
case 1://截断写
{
bknum=openfilelist[fd].first;
fatptr=fat1+bknum;
while(fatptr->id!=END)
{
bknum=fatptr->id;
fatptr->id=FREE;
fatptr=fat1+bknum;
}
fatptr->id=FREE;
bknum=openfilelist[fd].first;
fatptr=fat1+bknum;
fatptr->id=END;
openfilelist[fd].length=0;
openfilelist[fd].count=0;
break;
}
case 2://覆盖写
{
openfilelist[fd].count=0;
break;
}
case 3://追加写
{
bknum=openfilelist[fd].first;
fatptr=fat1+bknum;
openfilelist[fd].count=0;
while(fatptr->id!=END)
{
bknum=fatptr->id;
fatptr=fat1+bknum;
openfilelist[fd].count+=BLOCKSIZE;
}
bkptr=(unsigned char *)(myvhard+bknum*BLOCKSIZE);
while(*bkptr !=0)
{
bkptr++;
openfilelist[fd].count++;
}
break;
}
default:
break;
}
}
printf("please input write data:\n");
gets(text);
if(do_write(fd,text,strlen(text),wstyle)>0)
{
wlen+=strlen(text);
}
else
{
return -1;
}
if(openfilelist[fd].count>openfilelist[fd].length)
{
openfilelist[fd].length=openfilelist[fd].count;
}
openfilelist[fd].fcbstate=1;
return wlen;
}
/*
原型声明: void my_mkdir(char *dirname)
功能描述: 创建子目录函数,在当前目录下创建名为dirname的目录
输入:
dirname 指向新建目录的名字的指针
输出:无
函数功能实现算法描述:
*/
void my_mkdir(char *dirname)
{
fcb *dirfcb,*pcbtmp;
int rbn,i,fd;
unsigned short bknum;
char text[MAX_TXT_SIZE],*p;
time_t *now;
struct tm *nowtime;
/*
将当前的文件信息读到text中
rbn 是实际读取的字节数
*/
openfilelist[curfd].count=0;
rbn = do_read(curfd,openfilelist[curfd].length,text);
dirfcb=(fcb *)text;
/*
检测是否有相同的目录名
*/
for(i=0;i<rbn/sizeof(fcb);i++)
{
if(strcmp(dirname,dirfcb->filename)==0)
{
printf("Error,the dirname is already exist!\n");
return -1;
}
dirfcb++;
}
/*
分配一个空闲的打开文件表项
*/
dirfcb=(fcb *)text;
for(i=0;i<rbn/sizeof(fcb);i++)
{
if(strcmp(dirfcb->filename,"")==0)
break;
dirfcb++;
}
openfilelist[curfd].count=i*sizeof(fcb);
fd=findFreeO();
if(fd<0)
{
return -1;
}
/*
寻找空闲盘块
*/
bknum = findFree();
if(bknum == END )
{
return -1;
}
pcbtmp=(fcb *)malloc(sizeof(fcb));
now=(time_t *)malloc(sizeof(time_t));
//在当前目录下新建目录项
pcbtmp->attribute=0x30;
time(now);
nowtime=localtime(now);
pcbtmp->time=nowtime->tm_hour*2048+nowtime->tm_min*32+nowtime->tm_sec/2;
pcbtmp->date=(nowtime->tm_year-80)*512+(nowtime->tm_mon+1)*32+nowtime->tm_mday;
strcpy(pcbtmp->filename,dirname);
strcpy(pcbtmp->exname,"di");
pcbtmp->first=bknum;
pcbtmp->length=2*sizeof(fcb);
openfilelist[fd].attribute=pcbtmp->attribute;
openfilelist[fd].count=0;
openfilelist[fd].date=pcbtmp->date;
strcpy(openfilelist[fd].dir[fd],openfilelist[curfd].dir[curfd]);
p=openfilelist[fd].dir[fd];
while(*p!='\0')
p++;
strcpy(p,dirname);
while(*p!='\0') p++;
*p='\\';p++;
*p='\0';
openfilelist[fd].dirno=openfilelist[curfd].first;
openfilelist[fd].diroff=i;
strcpy(openfilelist[fd].exname,pcbtmp->exname);
strcpy(openfilelist[fd].filename,pcbtmp->filename);
openfilelist[fd].fcbstate=1;
openfilelist[fd].first=pcbtmp->first;
openfilelist[fd].length=pcbtmp->length;
openfilelist[fd].free=1;
openfilelist[fd].time=pcbtmp->time;
openfilelist[fd].topenfile=1;
do_write(curfd,(char *)pcbtmp,sizeof(fcb),2);
pcbtmp->attribute=0x28;
time(now);
nowtime=localtime(now);
pcbtmp->time=nowtime->tm_hour*2048+nowtime->tm_min*32+nowtime->tm_sec/2;
pcbtmp->date=(nowtime->tm_year-80)*512+(nowtime->tm_mon+1)*32+nowtime->tm_mday;
strcpy(pcbtmp->filename,".");
strcpy(pcbtmp->exname,"di");
pcbtmp->first=bknum;
pcbtmp->length=2*sizeof(fcb);
do_write(fd,(char *)pcbtmp,sizeof(fcb),2);
pcbtmp->attribute=0x28;
time(now);
nowtime=localtime(now);
pcbtmp->time=nowtime->tm_hour*2048+nowtime->tm_min*32+nowtime->tm_sec/2;
pcbtmp->date=(nowtime->tm_year-80)*512+(nowtime->tm_mon+1)*32+nowtime->tm_mday;
strcpy(pcbtmp->filename,"..");
strcpy(pcbtmp->exname,"di");
pcbtmp->first=openfilelist[curfd].first;
pcbtmp->length=openfilelist[curfd].length;
do_write(fd,(char *)pcbtmp,sizeof(fcb),2);
openfilelist[curfd].count=0;
do_read(curfd,openfilelist[curfd].length,text);
pcbtmp=(fcb *)text;
pcbtmp->length=openfilelist[curfd].length;
my_close(fd);
openfilelist[curfd].count=0;
do_write(curfd,text,pcbtmp->length,2);
}
/**/
/*
原型声明: void my_ls()
功能描述: 显示目录函数
输入: 无
输出: 无
函数功能实现算法描述:
*/
void my_ls()
{
fcb *fcbptr;
int i;
char text[MAX_TXT_SIZE];
unsigned short bknum;
openfilelist[curfd].count=0;
do_read(curfd,openfilelist[curfd].length,text);
fcbptr=(fcb *)text;
for(i=0;i<(int)(openfilelist[curfd].length/sizeof(fcb));i++)
{
if(fcbptr->filename[0]!='\0')
{
if(fcbptr->attribute&0x20)
{
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,fcbptr->length,((fcbptr->date)>>9)+1980,(fcbptr->date>>5)&0x000f,fcbptr->date&0x1f,fcbptr->time>>11,(fcbptr->time>>5)&0x3f,fcbptr->time&0x1f*2);
}
}
fcbptr++;
}
openfilelist[curfd].count=0;
}
/**/
/*
原型声明: void my_rmdir(char *dirname)
功能描述: 删除子目录函数
输入:
dirname 指向新建目录的名字的指针
输出:无
函数功能实现算法描述:
*/
void my_rmdir(char *dirname)
{
int rbn,fd;
char text[MAX_TXT_SIZE];
fcb *fcbptr,*fcbtmp,*fcbtmp2;
unsigned short bknum;
int i,j;
fat *fat1,*fatptr;
if(strcmp(dirname,".")==0 || strcmp(dirname,"..")==0)
{
printf("Error,can't remove this directory.\n");
return -1;
}
fat1=(fat *)(myvhard+BLOCKSIZE);
openfilelist[curfd].count=0;
rbn=do_read(curfd,openfilelist[curfd].length,text);
fcbptr=(fcb *)text;
for(i=0;i<rbn/sizeof(fcb);i++)
{
if(strcmp(dirname,fcbptr->filename)==0)
{
break;
}
fcbptr++;
}
if(i >= rbn/sizeof(fcb))
{
printf("Error,the directory is not exist.\n");
return -1;
}
bknum=fcbptr->first;
fcbtmp2=fcbtmp=(fcb *)(myvhard+bknum*BLOCKSIZE);
for(j=0;j<fcbtmp->length/sizeof(fcb);j++)
{
if(strcmp(fcbtmp2->filename,".") && strcmp(fcbtmp2->filename,"..") && fcbtmp2->filename[0]!='\0')
{
printf("Error,the directory is not empty.\n");
return -1;
}
fcbtmp2++;
}
while(bknum!=END)
{
fatptr=fat1+bknum;
bknum=fatptr->id;
fatptr->id=FREE;
}
strcpy(fcbptr->filename,"");
strcpy(fcbptr->exname,"");
fcbptr->first=END;
openfilelist[curfd].count=0;
do_write(curfd,text,openfilelist[curfd].length,2);
}
/*
原型声明: int my_open(char *filename)
功能描述: 打开文件函数
输入:
filename 指向要打开的文件的名字的指针
输出: 返回打开的文件的id
函数功能实现算法描述:
*/
int my_open(char *filename)
{
int i,fd,rbn;
char text[MAX_TXT_SIZE],*p,*fname,*exname;
fcb *fcbptr;
char exnamed=0;
fname=strtok(filename,".");
exname=strtok(NULL,".");
if(!exname)
{
exname=(char *)malloc(3);
strcpy(exname,"di");
exnamed=1;
}
for(i=0;i<MAX_OPEN_FILE;i++)
{
if(strcmp(openfilelist[i].filename,filename)==0 &&strcmp(openfilelist[i].exname,exname)==0&& i!=curfd)
{
printf("Error,the file is already open.\n");
return -1;
}
}
openfilelist[curfd].count=0;
rbn=do_read(curfd,openfilelist[curfd].length,text);
fcbptr=(fcb *)text;
for(i=0;i<rbn/sizeof(fcb);i++)