⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesystem.c

📁 一个关于操作系统的线程模拟
💻 C
字号:
#include  <stdio.h>
#include  <string.h>
#include  <io.h>
#include  <dos.h>
#include  <conio.h>
#include  <process.h>
#include "head.h"

void main()
{ long currentuserpos,newuserpos;
  long currentfilepos,prefilepos,sourcefilepos,targetfilepos;
  long textpos;
  long currentpasspos,newpasspos;
  log newuser,user;
  pas newpass,pass;
  fil currentfile,prefile,sourcefile,targetfile;
  texter content,newtext,sourcetext,targettext;
  int Pcode,numbers,basement;
  char choice,choice1,choice2,choice3;
  char loguser[20],pw[20],enterfile[8],texts[1000];
  FILE *fp; 
  printf("              Wellcome to virtual file system.\n"
		 "    This program will not damage your current file system.\n"
		 "                   copyright 2004/7/2\n"     
		 "         computer 015       xudong        01171121\n");
                //创建虚拟硬盘
if((fp=fopen("vdisk.vdr","rb+"))==NULL)	//新建vdisk为空文件
	{if((fp=fopen("vdisk.vdr","wb+"))==NULL)//判断剩余空间,无空间则退出系统
		{printf("Not enough disk space for a virtual drive");
		 exit(0);
		}
   	 else
		{strcpy(newuser.username,"Administrator");//给newuser.username赋一个初值
	     strcpy(newpass.password,"newpassword");//给newpass.password赋一个初值
		 newuser.link=newuser.nextuser=0l;
		 newpass.link1=newpass.nextpass=0l;
		 fwrite(&newuser,loglen,1,fp);
		 fwrite(&newpass,paslen,1,fp);
		}
	 }
	

/////////////////////login///////////////////////////////////
begin:rewind(fp);
printf("\n LOGIN  or  QUIT(l/q):");
enter:
        choice=getch();
		putch(choice);
	  	
 switch(choice)
		{case 'l':
		 case 'L':break;
		 case 'q':
		 case 'Q':exit(0);
		 default:printf("\nplease choose again(l/q)");goto enter;
		}
printf("\nENTER UR USERNAME:");
scanf("%s",loguser);printf("\n");
password: printf("\nENTER UR PASSWORD:");
scanf("%s",pw);printf("\n");
start:
     currentuserpos=ftell(fp);//输入的新指针给当前用户地址
	 currentpasspos=ftell(fp);//输入的新指针给当前密码地址
     fread(&user,loglen,1,fp);
	 fread(&pass,paslen,1,fp);
    if(!strcmp(user.username,loguser))//输入用户名和当前用户名比较
	   {if(!strcmp(pass.password,pw))//输入密码和当前密码比较
		{printf("\nWelcome back,%s.U have been registered!^_^\n",loguser);//相同,输出欢迎老用户
		 goto menu;
	}else 
		goto password;
	}
 else
		{if(user.nextuser==0l) goto anewuser;//如果当前vdisk中没有用户,跳转newuser
		 else
			{fseek(fp,user.nextuser,0);
		     fseek(fp,pass.nextpass,0);
			 goto start;
			}
		}
anewuser:
 goend;
 newuserpos=ftell(fp);//输入的新指针给新用户地址
 newpasspos=ftell(fp);//输入的新指针给新密码地址
 strcpy(newuser.username,loguser);//输入的新用户名给newuser.username
 strcpy(newpass.password,pw);//输入的新密码给newpass.password
 newuser.link=newuser.nextuser=0l;
 newpass.link1=newpass.nextpass=0l;
 fwrite(&newuser,loglen,1,fp);//写入
 fwrite(&newpass,paslen,1,fp);
 fseek(fp,currentuserpos,0);//移动指针到当前用户地址
 fseek(fp,currentpasspos,0);//移动指针到当前密码地址
 fread(&user,loglen,1,fp);//打开
 fread(&pass,paslen,1,fp);
 user.nextuser=newuserpos;//新用户地址给user的后继
 pass.nextpass=newpasspos;//新密码地址给password的后继
 fseek(fp,currentuserpos,0);//指针移动到当前用户地址
 fseek(fp,currentpasspos,0);//指针移动到当前密码地址
 fwrite(&user,loglen,1,fp);//写入用户名
 fwrite(&pass,paslen,1,fp);//写入密码
 currentuserpos=newuserpos;//新用户地址给当前用户地址
 currentpasspos=newpasspos;//新密码地址给当前密码地址
 user=newuser; 

 printf("\nWelcome new user,try to use this OS %s.\n",loguser);
 goto menu;
 pass=newpass;



///////////////////////////////MENU/////////////////////////////////////
menu:
   
	while (1)
		{

		printf("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n"
			  "         THE DEMO DISPLAY FOR THE FILE SYSTEM         \n"
			  "                                                      \n"
			  "            1 : create a new file                     \n"
			  "            2 : delete a file                         \n"
			  "            3 : write                                 \n"
			  "            4 : read                                  \n"
			  "            5 : directory                             \n"
		      "            6 : copy                                  \n"
			  "            7 : logout for another user                 \n"
			  "            8 : exit the OS                  \n"
			  "\n");
		do
		{
			printf("\n		PLEASE ENTER YOUR CHOICE :");
			choice1=getch();
			putch(choice1);
		}while((choice1<'1')||(choice1>'8'));
		printf("\n\n");
		switch (choice1)
		{
			case '1' :goto create;
						break;
			case '2' :goto del;
						break;
			case '3' :goto write;
						break;
            case '4' :goto read;
						break;
			case '5' :goto dir;
						break;
			case '6' :goto copy;
						break;
			case '7' :goto logout;
						break;
			case '8'  :goto exit;
						break;
			default: goto error;
				      break;
		}


 
/////////////////////exit/////////////////////////
exit:
	exit(0);//退出



//////////////////////logout/////////////////////////
logout:
  
 printf("\nlogout or not(Y/N):");
 choice2=getch();
 //	putch(choice2);
 switch(choice2)
		{case 'Y':goto begin;
		 case 'y':goto begin;
		 case 'N':goto menu;
		 case 'n':goto menu;
		 default: goto logout;
		
		}


/////////////////////create//////////////////////////
create:
 printf("\nEnter a new file name:");//创建文件
 scanf("%s",enterfile);
 printf("\nenter a pcode(0-read only,1-write only,2-both w/r):");//输入保护码
 entercode:
 scanf("%d",&Pcode);
 if(Pcode!=0&&Pcode!=1&&Pcode!=2)
			{printf("\nout of range,re-enter:");
			 goto entercode;
			}
          if(user.link!=0l)//用户还没有文件
			{again:
		       currentfilepos=user.link;
			 loop:
			   fseek(fp,currentfilepos,0);
		       fread(&currentfile,fillen,1,fp);
			   if(!strcmp(currentfile.filename,enterfile))//输入的文件名和当前文件文件名比较
				{printf("\nThere is a file with the same file name!Please re-enter the file name:");
				 scanf("%s",enterfile);
				 goto again;//文件名相同,跳转again
				}
			 if(currentfile.nextfile==0l) goto out;
			 currentfilepos=currentfile.nextfile;//后继文件地址给当前文件地址,跳转loop,重新比较
			 goto loop;
			 out:goend;
			 currentfile.nextfile=ftell(fp);
			 fseek(fp,currentfilepos,0);
			 fwrite(&currentfile,fillen,1,fp);
			 goto createnew;
				}
 else
				{goend;
				 currentfilepos=ftell(fp);
				 user.link=currentfilepos;
				 fseek(fp,currentuserpos,0);
				 fwrite(&user,loglen,1,fp);
				 createnew:
				   goend;
				 currentfilepos=ftell(fp);
				 strcpy(currentfile.filename,enterfile);//输入文件名赋给当前文件的文件名
				 currentfile.pcode=Pcode;//保护码赋给当前文件的保护码
				 currentfile.length=0;//默认当前文件长度为0
				 currentfile.addr=currentfile.nextfile=0l;
				 fwrite(&currentfile,fillen,1,fp);
				 if(Pcode==0)
					{printf("\nThis file is read-only! ");
					
					 printf("\?");
					 getch();	
					 fgets(texts,1000,stdin);
				 anewtext://创建新文件
					   goend;
					 currentfile.addr=ftell(fp);
				     fseek(fp,currentfilepos,0);
					 fwrite(&currentfile,fillen,1,fp);
			     	 printf("%s has been created!\n",enterfile);
				 goto writing;	
					}
				else goto menu;
				}
 /////////////////////////directory////////////////////

dir:
	
	printf("\nFile name\tPcode\tblocks\n");	//显示当前用户所拥有的文件
  
     currentfilepos=user.link;
    while(currentfilepos!=0l)
	
	{fseek(fp,currentfilepos,0);
		 fread(&currentfile,fillen,1,fp);
		 printf("%s\t\t  %d\t  %d\n",currentfile.filename,currentfile.pcode,currentfile.length);
		currentfilepos=currentfile.nextfile;
			}
 goto menu;
}


//////////////////////write///////////////////////
write:
     printf("\nPlease enter the file name:");	//写入
 scanf("%s",enterfile);
 getchar();	
if(user.link==0l) goto nofile;
 currentfilepos=user.link;
 fseek(fp,currentfilepos,0);
 fread(&currentfile,fillen,1,fp);
 while(strcmp(currentfile.filename,enterfile)&&currentfile.nextfile!=0l)//输入用户名跟当前用户名比较
			{currentfilepos=currentfile.nextfile;           //当前用户后继地址给当前文件地址            
			 fseek(fp,currentfilepos,0);
			 fread(&currentfile,fillen,1,fp);
			}
 if(strcmp(currentfile.filename,enterfile)) goto nofile;
 if(currentfile.pcode==0)//如果只读,则不允许
			{printf("\nThis file is write-only.You cannot read it!\n");
			 goto menu;
			}
 printf("\nstart:");//开始写入文字
 fgets(texts,1000,stdin);
 printf("\nSave or Not(Y/N):");
 //////////////////////////save///////////////////////
save:choice3=getchar();	//保存
 switch(choice3)
		{case 'Y':
		 case 'y':break;
		 case 'N':
		 case 'n':goto menu;
		 default:printf("\nwrong choice,again(Y/N):");goto save;
		 }
  if((textpos=currentfile.addr)==0l) goto anewtext;//当前文件块的地址
  fseek(fp,textpos,0);
  fread(&content,textlen,1,fp);
  while(content.nexttext!=0l)
			{textpos=content.nexttext;
			 fseek(fp,textpos,0);
			 fread(&content,textlen,1,fp);
			}
 goend;
 content.nexttext=ftell(fp);
 fseek(fp,textpos,0);
 fwrite(&content,textlen,1,fp);
writing:
 goend;
 textpos=ftell(fp);
 numbers=strlen(texts)/19;	//需要的文件块的数量/
 basement=0;
 while(basement<numbers)
			{strcopy(newtext.text,texts+basement*19,19);
			 newtext.nexttext=0l;
			 fwrite(&newtext,textlen,1,fp);
			 goend;
			 newtext.nexttext=ftell(fp);
			 fseek(fp,textpos,0);
			 fwrite(&newtext,textlen,1,fp);
			 textpos=newtext.nexttext;
			 basement++;
			 fseek(fp,textpos,0);
			}
 strcopy(newtext.text,texts+basement*19,19);	//最后一个文件块的内容/
 newtext.nexttext=0l;
 fwrite(&newtext,textlen,1,fp);
 currentfile.length+=numbers+1;////////change///////////
 fseek(fp,currentfilepos,0);
 fwrite(&currentfile,fillen,1,fp);
 goto menu;


 ////////////////////////copy/////////////////////////////
copy:
	 printf("\nPlease enter the source file name:");	
 scanf("%s",enterfile);
 if(user.link==0l) goto nofile;
 sourcefilepos=user.link;
 fseek(fp,sourcefilepos,0);
 fread(&sourcefile,fillen,1,fp);
 while(strcmp(sourcefile.filename,enterfile)&&sourcefile.nextfile!=0l)	//寻找原文件
			{sourcefilepos=sourcefile.nextfile;
			 fseek(fp,sourcefilepos,0);
			 fread(&sourcefile,fillen,1,fp);
			}
 if(strcmp(sourcefile.filename,enterfile)) goto nofile;
 printf("\nPlease enter the target file name:");
tarfile:scanf("%s",enterfile);
 currentfilepos=user.link;
 fseek(fp,currentfilepos,0);
 fread(&currentfile,fillen,1,fp);
 while(strcmp(currentfile.filename,enterfile)&&currentfile.nextfile!=0l)	//寻找目标文件
			{currentfilepos=currentfile.nextfile;
			 fseek(fp,currentfilepos,0);
			 fread(&currentfile,fillen,1,fp);
			}
 if(!strcmp(currentfile.filename,enterfile))
			{printf("\nThere is a file with the same name.Please re-enter the target file name:");
			 goto tarfile;
			}
 goend;
 targetfilepos=currentfile.nextfile=ftell(fp);
 fseek(fp,currentfilepos,0);
 fwrite(&currentfile,fillen,1,fp);
 goend;
 targetfile=sourcefile;	//把sourcefile的file信息给targefile
 targetfile.nextfile=0l;
 strcpy(targetfile.filename,enterfile);
 fwrite(&targetfile,fillen,1,fp);
 if(targetfile.addr==0l) goto menu;
 goend;
 targetfile.addr=ftell(fp);
 fseek(fp,targetfilepos,0);
 fwrite(&targetfile,fillen,1,fp);
 textpos=sourcefile.addr;	//sourcefile文件块的地址
 fseek(fp,textpos,0);
 fread(&sourcetext,textlen,1,fp);
 while(sourcetext.nexttext!=0l)
			{strcpy(targettext.text,sourcetext.text);
			 goend;
			 fwrite(&targettext,textlen,1,fp);
			 goend;
			 targettext.nexttext=ftell(fp);
			 fseek(fp,-(long)textlen,2);
			 fwrite(&targettext,textlen,1,fp);
			 textpos=sourcetext.nexttext;
			 fseek(fp,textpos,0);
			 fread(&sourcetext,textlen,1,fp);
			}
 strcpy(targettext.text,sourcetext.text);	//拷贝最后一个文件块
 targettext.nexttext=0l;
 goend;
 fwrite(&targettext,textlen,1,fp);
 
 goto menu;
nofile:printf("\nThe file is not found!");	//error
 goto menu;


 /////////////////////read///////////////////////////
read:
 printf("\nPlease enter the file name:");	
 scanf("%s",enterfile);
 if(user.link==0l) goto nofile;
 currentfilepos=user.link;
 fseek(fp,currentfilepos,0);
 fread(&currentfile,fillen,1,fp);
 while(strcmp(currentfile.filename,enterfile)&&currentfile.nextfile!=0l)	
			{currentfilepos=currentfile.nextfile;
			 fseek(fp,currentfilepos,0);
			 fread(&currentfile,fillen,1,fp);
			}
 if(strcmp(currentfile.filename,enterfile)) goto nofile;
 if(currentfile.pcode==1)	//文件只读则不允许
			{printf("\nThis file is read-only.You cannot modify it!\n");
			 goto menu;
			}
 else
			{textpos=currentfile.addr;
			 printf("\n");
			 while(textpos!=0l)
				{fseek(fp,textpos,0);
				 fread(&content,textlen,1,fp);
				 printf("%s",content.text);
				 textpos=content.nexttext;
				}
			 goto menu;
			};


 /////////////////////////delete///////////////////////////
del:printf("\nPlease enter the file name:");	
 scanf("%s",enterfile);
 if(user.link==0l) goto nofile;		
 currentfilepos=user.link;
 fseek(fp,currentfilepos,0);
 fread(&currentfile,fillen,1,fp);
 while(strcmp(currentfile.filename,enterfile)&&currentfile.nextfile!=0l)	
			{prefilepos=currentfilepos;
			 currentfilepos=currentfile.nextfile;
			 fseek(fp,currentfilepos,0);
			 fread(&currentfile,fillen,1,fp);
			}
 if(strcmp(currentfile.filename,enterfile)) goto nofile;
       if(currentfilepos==user.link)
			{user.link=currentfile.nextfile;
			 fseek(fp,currentuserpos,0);
			 fwrite(&user,fillen,1,fp);
			 goto menu;
			}
 else
			{fseek(fp,prefilepos,0);
			 fread(&prefile,fillen,1,fp);
			 prefile.nextfile=currentfile.nextfile;
			 fseek(fp,prefilepos,0);
			 fwrite(&prefile,fillen,1,fp);
			 goto menu;
			};

error:printf("you have chosen a wrong choice,choose again!!\n");
	 goto menu;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -