实验六文件系统设计结果.doc

上传人:大张伟 文档编号:6059598 上传时间:2020-09-02 格式:DOC 页数:23 大小:92.50KB
返回 下载 相关 举报
实验六文件系统设计结果.doc_第1页
第1页 / 共23页
实验六文件系统设计结果.doc_第2页
第2页 / 共23页
实验六文件系统设计结果.doc_第3页
第3页 / 共23页
实验六文件系统设计结果.doc_第4页
第4页 / 共23页
实验六文件系统设计结果.doc_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《实验六文件系统设计结果.doc》由会员分享,可在线阅读,更多相关《实验六文件系统设计结果.doc(23页珍藏版)》请在三一文库上搜索。

1、实验六 文件系统设计1目的和要求本实验的目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能和内部实现。2实验内容为DOS系统设计一个简单的二级文件系统,可以实现下列几条命令DIR 列文件目录CREATE 创建文件DELETE 删除文件MODIFY 修改文件OPEN 打开文件CLOSE 关闭文件列目录时要列出文件名,物理地址,保护码和文件长度。3实验环境PC兼容机Windows、DOS系统、Turbo c 2.0C语言4实验提示首先应确定文件系统的数据结构:主目录、活动文件等。主目录文件的形式存放于磁盘,这样便于查找和修改。主目录结构:Ufdname 用户名 Ufdfile 指向

2、用户的活动文件活动文件结构:Fpaddr 文件物理地址 Flength 文件长度 Fmode 文件属性(file mode:0-Read Only;1-Write Only;2-Read and Write(default)) Fname 文件名称用户创建的文件,可以编号存储于磁盘上。如:file0,file1,file2并以编号作为物理地址,在目录中进行登记。本程序需要在c:下建一个名为osfile的目录及一个名为file的子目录,在利用程序创建了文件系统后,可以在这个文件夹下查看到相关的内容。5实验程序#include stdio.h#include string.h#include co

3、nio.h#include stdlib.h#define MAXNAME 25 /*the largest length of mfdname,ufdname,filename*/#define MAXCHILD 50 /*the largest child*/#define MAX (MAXCHILD*MAXCHILD) /*the size of fpaddrno*/typedef struct /*the structure of OSFILE*/ int fpaddr; /*file physical address*/ int flength; /*file length*/ in

4、t fmode; /*file mode:0-Read Only;1-Write Only;2-Read and Write(default);*/ char fnameMAXNAME; /*file name*/ OSFILE;typedef struct /*the structure of OSUFD*/ char ufdnameMAXNAME; /*ufd name*/ OSFILE ufdfileMAXCHILD; /*ufd own file*/OSUFD;typedef struct /*the structure of OSUFDLOGIN*/ char ufdnameMAXN

5、AME; /*ufd name*/ char ufdpword8; /*ufd password*/ OSUFD_LOGIN;typedef struct /*file open mode*/ int ifopen; /*ifopen:0-close,1-open*/ int openmode; /*0-read only,1-write only,2-read and write,3-initial*/OSUFD_OPENMODE;OSUFD *ufdMAXCHILD; /*ufd and ufd own files*/OSUFD_LOGIN ufd_lp;int ucount=0; /*t

6、he count of mfds ufds*/int fcountMAXCHILD; /*the count of ufds files*/int loginsuc=0; /*whether login successfully*/char usernameMAXNAME; /*record login users name22*/char dirnameMAXNAME;/*record current directory*/int fpaddrnoMAX; /*record file physical address num*/OSUFD_OPENMODE ifopenMAXCHILDMAX

7、CHILD; /*record file open/close*/int wgetchar; /*whether getchar()*/FILE *fp_mfd,*fp_ufd,*fp_file_p,*fp_file;void main()int i,j,choice1;char choice50; /*choice operation:dir,create,delete,open,delete,modify,read,write*/int choiceend=1; /*whether choice end*/char *rtrim(char *str); /*remove the trail

8、ing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/void LoginF(); /*LOGIN FileSystem*/void DirF(); /*Dir FileSystem*/void CdF(); /*Change Dir*/void CreateF(); /*Create File*/void DeleteF(); /*Delete File*/void ModifyFM(); /*Modify FileMode*/void OpenF(); /*Open File*/void CloseF(); /

9、*Close File*/void ReadF(); /*Read File*/void WriteF(); /*Write File*/void QuitF(); /*Quit FileSystem*/void help(); if(fp_mfd=fopen(c:osfilemfd,rb)=NULL) fp_mfd=fopen(c:osfilemfd,wb);fclose(fp_mfd); for(i=0;i,strupr(dirname); else printf(Bad command or file name.nC:%s,strupr(username); gets(choice);s

10、trcpy(choice,ltrim(rtrim(strlwr(choice);if (strcmp(choice,dir)=0) choice1=1;else if(strcmp(choice,create)=0) choice1=2;else if(strcmp(choice,delete)=0) choice1=3;else if(strcmp(choice,attrib)=0) choice1=4;else if(strcmp(choice,open)=0) choice1=5;else if(strcmp(choice,close)=0) choice1=6;else if(strc

11、mp(choice,read)=0) choice1=7;else if(strcmp(choice,modify)=0) choice1=8;else if(strcmp(choice,exit)=0) choice1=9;else if(strcmp(choice,cls)=0) choice1=10;else if(strcmp(choice,cd)=0) choice1=11;else if(strcmp(choice,help)=0) choice1=20;else choice1=12; switch(choice1) case 1:DirF();choiceend=1;break

12、;case 2:CreateF();choiceend=1;if(!wgetchar) getchar();break;case 3:DeleteF();choiceend=1;if(!wgetchar)getchar();break;case 4:ModifyFM();choiceend=1;if(!wgetchar) getchar();break;case 5:choiceend=1;OpenF();if (!wgetchar) getchar();break;case 6:choiceend=1;CloseF();if (!wgetchar) getchar();break;case

13、7:choiceend=1;ReadF();if (!wgetchar) getchar();break;case 8:choiceend=1;WriteF();if (!wgetchar) getchar();break;case 9:printf(nYou have exited this system.); QuitF();exit(0);break;case 10:choiceend=1;clrscr();break;case 11:CdF();choiceend=1;break;case 20:help();choiceend=1;break;default:choiceend=0;

14、 else printf(nAccess denied.);void help(void)printf(nThe Command Listn);/*printf(nCd Attrib Create Modify Read Open Cls Delete Exit Closen);*/printf(Create : Create a file(You can initialize files attribute and content.)n);printf(Open : Open a file to modifyn);printf(Close : Close a file.n);printf(M

15、odify : Modify the opened file.n);printf(Delete : Delete existed files.n);printf(CD : Change current directory.n);printf(Exit : Exit this program.n);char *rtrim(char *str) /*remove the trailing blanks.*/int n=strlen(str)-1; while(n=0) if(*(str+n)!= ) *(str+n+1)=0;break; else n-; if (nufdname,strupr(

16、ufd_lp.ufdname); fp_ufd=fopen(str,rb); fcountj=0; for(i=0;fread(&ufdj-ufdfilei,sizeof(OSFILE),1,fp_ufd)!=0;i+,fcountj+) ifopenji.ifopen=0; ifopenji.openmode=4; fclose(fp_ufd); fclose(fp_mfd); ucount=j;SetPANo(0); printf(nnLogin successful! Welcome to this FileSystemnn); loginsuc=1; return; else prin

17、tf(nn); flag=1; while(flag) printf(Login Failed! Password Error. Try Again(Y/N):); gets(a); ltrim(rtrim(a); if (strcmp(strupr(a),Y)=0) loginsuc=0;flag=0; else if(strcmp(strupr(a),N)=0)loginsuc=0;flag=0;return; else printf(New Password(=8):); InputPW(loginpw); /*input new password,use * replace*/ pri

18、ntf(nConfirm Password(ufdname,strupr(ufd_lp.ufdname); fp_ufd=fopen(str,rb); for(i=0;fread(&ufdj-ufdfilei,sizeof(OSFILE),1,fp_ufd)!=0;i+,fcountj+) ifopenji.ifopen=0; ifopenji.openmode=4; fclose(fp_ufd); fclose(fp_mfd); ucount=j; SetPANo(0); printf(nnLogin Successful! Welcome to this Systemnn); logins

19、uc=1; return; else printf(nn); flag=1; while(flag) printf(Login Failed! Password Error. Try Again(Y/N):); gets(a); ltrim(rtrim(a); if (strcmp(strupr(a),Y)=0) loginsuc=0;flag=0; else if(strcmp(strupr(a),N)=0)loginsuc=0;flag=0;return; void SetPANo(int RorW) /*Set physical address num,0-read,1-write*/i

20、nt i,j; if (RorW=0)if(fp_file_p=fopen(c:osfilefilefile_p,rb)=NULL) fp_file_p=fopen(c:osfilefilefile_p,wb); fclose(fp_file_p); fp_file_p=fopen(c:osfilefilefile_p,rb); for(i=0;fread(&j,sizeof(int),1,fp_file_p)!=0;i+) fpaddrnoj=1; /*for(i=1;iMAX;i+) if (i%13)=0) fpaddrnoi=1;*/ elsefp_file_p=fopen(c:osf

21、ilefilefile_p,wb); /*for(i=1;iMAX;i+) if(i%13)=0) fpaddrnoi=0;*/ for(i=0;iMAX;i+) if (fpaddrnoi=1)fwrite(&i,sizeof(int),1,fp_file_p); fclose(fp_file_p);void InputPW(char *password) /*input password,use * replace*/int j; for(j=0;j0)j-;j-; putchar(b);putchar( );putchar(b);else j-; else passwordj=0; br

22、eak; passwordj=0;void DirF() /*Dir FileSystem*/int i,j,count=0; char sfmode25,sfpaddr25,str25; int ExistD(char *dirname); /*Whether DirName Exist,Exist-i,Not Exist-0*/ /*clrscr();*/ if (strcmp(strupr(ltrim(rtrim(dirname),)!=0) printf(nnC:%sdirn,dirname); printf(n%14s%16s%14s%10s%18sn,FileName,FileAd

23、dress,FileLength,Type,FileMode); j=ExistD(dirname); for(i=0;iufdfilei.fpaddr,str,10);strcpy(sfpaddr,file);strcat(sfpaddr,str);if (ufdj-ufdfilei.fmode=0) strcpy(sfmode,Read Only);else if(ufdj-ufdfilei.fmode=1) strcpy(sfmode,Write Only);else if(ufdj-ufdfilei.fmode=2)strcpy(sfmode,Read And Write);else

24、strcpy(sfmode,Protect);printf(%14s%16s%14d%10s%18sn,ufdj-ufdfilei.fname,sfpaddr,ufdj-ufdfilei.flength,sfmode); printf(n %3d file(s)n,fcountj); else printf(nnC:dirn); printf(n%14s%18s%8sn,DirName,OwnFileCount,Type); for(i=0;iufdname,fcounti,);count=count+fcounti; printf(n %3d dir(s),%5d file(s)n,ucou

25、nt,count); int ExistD(char *dirname) /*Whether DirName Exist,Exist-i,Not Exist-0*/int i; int exist=0; for(i=0;iufdname),strupr(dirname)=0)exist=1; break; if (exist) return(i); else return(-1);void CdF() /*Exchange Dir*/char dnameMAXNAME; char *rtrim(char *str); /*remove the trailing blanks.*/ char *

26、ltrim(char *str); /*remove the heading blanks.*/ int ExistD(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/ printf(nPlease input DirName (cd.-Previous dir; DirNAME-cd DirNAME):); gets(dname); ltrim(rtrim(dname); if (ExistD(dname)=0) strcpy(dirname,strupr(dname); else if(strcmp(strupr

27、(dname),CD.)=0) strcpy(ltrim(rtrim(dirname),); else printf(nError.%s does not exist.n,dname);void CreateF() /*Create File*/int fpaddrno,flag=1,i; char fnameMAXNAME,str50,str150,strtext255,a25; char fmode25; char *rtrim(char *str); /*remove the trailing blanks.*/ char *ltrim(char *str); /*remove the

28、heading blanks.*/ int FindPANo(); /*find out physical address num*/ int WriteF1(); /*write file*/ int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/ int ExistD(char *dirname); if (strcmp(strupr(dirname),strupr(username)!=0) printf(nError. You must create file in your own dir.

29、n);wgetchar=1; else printf(nPlease input FileName:); gets(fname); ltrim(rtrim(fname); if (ExistF(fname)=0) printf(nError. Name %s has already existed.n,fname);wgetchar=1; else printf(Please input FileMode(0-Read Only, 1-Write Only, 2-Read and Write, 3-Protect):);gets(fmode);ltrim(rtrim(fmode);if(str

30、cmp(fmode,0)=0)|(strcmp(fmode,1)=0)|(strcmp(fmode,2)=0)|(strcmp(fmode,3)=0) fpaddrno=FindPANo(); if (fpaddrno=0) i=ExistD(username); strcpy(ufdi-ufdfilefcounti.fname,fname); ufdi-ufdfilefcounti.fpaddr=fpaddrno; ufdi-ufdfilefcounti.fmode=atoi(fmode); ifopenifcounti.ifopen=0; ifopenifcounti.openmode=4

31、; strcpy(str,c:osfilefilefile); itoa(fpaddrno,str1,10); strcat(str,str1); fp_file=fopen(str,wb); fclose(fp_file); fcounti+; while(flag) printf(Input text now(Y/N):); gets(a); ltrim(rtrim(a); ufdi-ufdfilefcounti-1.flength=0; if(strcmp(strupr(a),Y)=0)fp_file=fopen(str,wb+); ufdi-ufdfilefcounti-1.flength=WriteF1(); flag=0;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1