char topenfile; }useropen;
/*表示该用户打开的表项是否为空,若值为0,表示为空*/
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[]=\ /*虚拟空间保存路径*/ 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();
/*函数设计*/
/*文件系统初始化*/
/* 原型声明: 功能描述: 输入:无 输出:无
函数功能实现算法描述: 1)申请磁盘空间
2)打开系统磁盘,若不存在,创建新的系统磁盘,并格式化 3)初始化用户打开文件表,将表项0分配给根目录文件使用
并填写根目录文件的相关信息
void startsys()
文件系统初始化,初始化所建立的文件系统
4)将ptrcurdir指向该用户打开文件表项 5)将当前目录设置为根目录 */
void startsys() {
FILE *fp; int i;
myvhard=(unsigned char *)malloc(SIZE); memset(myvhard,0,SIZE); fp=fopen(filename,\ if(fp) { } else { }
strcpy(openfilelist[0].filename,\
printf(\my_format();
fread(buffer,SIZE,1,fp); fclose(fp);
if(buffer[0]!=0xaa||buffer[1]!=0xaa) { } else { }
for(i=0;i myvhard[i]=buffer[i]; printf(\my_format(); strcpy(openfilelist[0].exname,\ 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,\ strcpy(openfilelist->dir[0],currentdir); startp=((block0 *)myvhard)->startblock; ptrcurdir=&openfilelist[0]; curfd=0; } /* 原型声明: 功能描述: 输入:无 输出:无 函数功能实现算法描述: 虚拟磁盘空间布局 1块 引导块 2块 2块 FAT1 FAT2 995块 数据区 void my_format() 对虚拟磁盘进行格式化,布局虚拟磁盘,建立根目录文件 虚拟磁盘一共划分成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,\FileSystem Ver 1.0 \\n Blocksize=1KB Whole size=1000KB 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++; /* 将数据区的标记为空闲状态 */ Blocknum=1000 for(i=7;i 创建根目录文件root,将数据区的第一块分配给根目录区 在给磁盘上创建两个特殊的目录项:\, 除了文件名之外,其它都相同 */ p+=BLOCKSIZE*5; root=(fcb *)p; strcpy(root->filename,\ strcpy(root->exname,\ 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,\ 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 fp=fopen(filename,\\ b0->startblock=p+BLOCKSIZE*4; fwrite(myvhard,SIZE,1,fp); root->filename[0]='\\0'; (*fat1).id=FREE; (*fat2).id=FREE; fat1++; fat2++;

