操作系统文件系统C语言实现的部分函数(一)

void startsys()
{
	int i;
	FILE *fp;
	char str[9];
	myvhard = (unsigned char *)malloc(SIZE*sizeof(char)); /*申请 1M空间*/
	if((fp=fopen("myfsys.dat","rb")) == NULL)
	{
		printf("myfsys文件系统不存在,现在开始创建文件系统\n");
		my_format();
		//fclose(fp);
		if((fp = fopen("myfsys.dat","w"))== NULL)
		{
			printf("open error!\n");
			exit(1);
		}
		strcpy(str,"10101010");
		fwrite(str,sizeof( char ),8,fp);
		fwrite(myvhard,SIZE,1,fp);
		fclose(fp);
	}
	else
	{
		fread(str, sizeof( char ), 8, fp );
		str[8] = '\0';
		if(strcmp(str,"10101010") == 0)
		{
			//将上述缓冲区中的内容复制到内存中的虚拟磁盘空间中
			fread(myvhard,SIZE,1,fp);
		}
		else
		{
			printf("myfsys文件系统不存在,现在开始创建文件系统\n");
			my_format();
			fclose(fp);
			if((fp=fopen("myfsys.dat","w"))== NULL)
			{
				printf("open error!\n");
				exit(1);
			}
			strcpy(str,"10101010");
			fwrite(str,sizeof( char ),8,fp);
			fwrite(myvhard,SIZE,1,fp);
		}
		fclose(fp);
	}

	fat_f = (struct FAT *)(myvhard + BLOCKSIZE);  /*找到FAT表地址*/
	root = (struct FCB *)(myvhard + BLOCKSIZE * 5);/*找到根目录地址*/

	strcpy(currentdir,"Root:");
	lengthdir = 2;
	for(i = 0; i < MAXOPENFILE; i++)
	{
		strcpy(openfilelist[i].filename,"");
		openfilelist[i].topenfile = -1;
		openfilelist[i].dirno = 5;
	}

	struct FCB *cur_mkdir = root;
	strcpy(openfilelist[0].filename,cur_mkdir[0].filename);
	openfilelist[0].attribute = 0;
	openfilelist[0].data =0;
	openfilelist[0].time = 0;
	openfilelist[0].nextdirectory = 0;
	openfilelist[0].nextfile = -1;
	openfilelist[0].length = 0;

	openfilelist[0].diroff = -1;
	openfilelist[0].dirno = 5;
	openfilelist[0].topenfile = -2;
	fd = 0;
}

//格式化
void my_format()
{
	int i;
	block0 block;
	strcpy(block.information,"10101010 磁盘块大小:1024000 磁盘块数量: 1024 最多打开文件数:10\n");
	block.root = 5;
	block.startblock = (unsigned char *)(myvhard + BLOCKSIZE * 6);

	fat_f = (struct FAT*)(myvhard + BLOCKSIZE);
	for(i = 0; i < 6; i++)
		fat_f[i].id = END;
	for(; i < 1000 ;i++)
		fat_f[i].id = FREE;

	lengthdir = 0;
	root = (struct FCB *)(myvhard + BLOCKSIZE * 5);

	strcpy(root[0].filename,".");
	root[0].attribute = 0;
	root[0].data = 0;
	root[0].time = 12;
	root[0].nextdirectory = -1;
	root[0].nextfile = 1;
	root[0].length = 0;
	root[0].first = 0;
	lengthdir++;

	strcpy(root[1].filename,"..");
	root[1].attribute = 0;
	root[1].data = 0;
	root[1].time = 12;
	root[1].nextdirectory = -1;
	root[1].nextfile = -1;
	root[1].length = 0;
	root[1].first = 0;

	lengthdir++;
	for(i = 2 ; i < MAXFCB ; i++)
	{
		root[i].attribute = -1;
		root[i].nextdirectory = -1;
		root[i].nextfile = -1;
	}
}


int my_cd(char *dirname)
{
    int i,j;
	struct FCB *cur_mkdir = root;
	for(j = fd ; j >= 0 ; j--)
		if(openfilelist[j].attribute == 0)
			break;
	char dir = openfilelist[j].nextdirectory;

	if(dir == -1)
		return -1;  //no file frontdir

	int flag = 0;

	/*
	for(int i = 0; i < MAXOPENFILE; i++)
	{
		if(!strcmp(openfilelist[i].filename,dirname))
			return -2;  //
	}
	*/

	while(cur_mkdir[dir].nextfile != -1)
	{
		if(!strcmp(cur_mkdir[dir].filename,dirname) && cur_mkdir[dir].attribute == 0)
		{
			flag = 1;
			break;
		}
		dir = cur_mkdir[dir].nextfile;
	}
	if(!strcmp(cur_mkdir[dir].filename,dirname))
		flag = 1;
	if(flag == 0) //not find
		return -1;

	for(i = 0; i < MAXOPENFILE; i++)
	{
		if(openfilelist[i].topenfile == -1)
			break;
	}
	if(i == MAXOPENFILE)
		return -3; // openlist is full

	strcpy(openfilelist[i].filename,dirname);
	openfilelist[i].attribute = cur_mkdir[dir].attribute;
	openfilelist[i].data = cur_mkdir[dir].data;
	openfilelist[i].time = cur_mkdir[dir].time;
	openfilelist[i].nextdirectory = cur_mkdir[dir].nextdirectory;
	openfilelist[i].nextfile = cur_mkdir[dir].nextfile;
	openfilelist[i].length = cur_mkdir[dir].length;

	openfilelist[i].dirno = 5;
	openfilelist[i].topenfile = dir;
	openfilelist[i].fcbstate = 0;

	for(j = i-1 ;j >0 ; j--)
		if(openfilelist[i].topenfile != -1 && openfilelist[i].attribute == 0)
			break;
	openfilelist[i].diroff = j;

	fd = i;
	strcat(currentdir,"\\");
	strcat(currentdir,dirname);
	return 1;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值