實驗六文件系統(tǒng)設計結果_第1頁
實驗六文件系統(tǒng)設計結果_第2頁
實驗六文件系統(tǒng)設計結果_第3頁
實驗六文件系統(tǒng)設計結果_第4頁
實驗六文件系統(tǒng)設計結果_第5頁
已閱讀5頁,還剩18頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、實驗六 文件系統(tǒng)設計1目的和要求本實驗的目的是通過一個簡單多用戶文件系統(tǒng)的設計,加深理解文件系統(tǒng)的內部功能和內部實現(xiàn)。2實驗內容為DOS系統(tǒng)設計一個簡單的二級文件系統(tǒng),可以實現(xiàn)下列幾條命令DIR 列文件目錄CREATE 創(chuàng)建文件DELETE 刪除文件MODIFY 修改文件OPEN 打開文件CLOSE 關閉文件列目錄時要列出文件名,物理地址,保護碼和文件長度。3實驗環(huán)境PC兼容機Windows、DOS系統(tǒng)、Turbo c 2.0C語言4實驗提示首先應確定文件系統(tǒng)的數(shù)據(jù)結構:主目錄、活動文件等。主目錄文件的形式存放于磁盤,這樣便于查找和修改。主目錄結構:Ufdname 用戶名 Ufdfile 指向

2、用戶的活動文件活動文件結構:Fpaddr 文件物理地址 Flength 文件長度 Fmode 文件屬性(file mode:0-Read Only;1-Write Only;2-Read and Write(default)) Fname 文件名稱用戶創(chuàng)建的文件,可以編號存儲于磁盤上。如:file0,file1,file2并以編號作為物理地址,在目錄中進行登記。本程序需要在c:下建一個名為osfile的目錄及一個名為file的子目錄,在利用程序創(chuàng)建了文件系統(tǒng)后,可以在這個文件夾下查看到相關的內容。5實驗程序#include "stdio.h"#include "s

3、tring.h"#include "conio.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 ph

4、ysical address*/ int flength; /*file length*/ int 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

5、/*the structure of OSUFD'LOGIN*/ char ufdnameMAXNAME; /*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 an

6、d ufd own files*/OSUFD_LOGIN ufd_lp;int ucount=0; /*the count of mfd's ufds*/int fcountMAXCHILD; /*the count of ufd's files*/int loginsuc=0; /*whether login successfully*/char usernameMAXNAME; /*record login user's name22*/char dirnameMAXNAME;/*record current directory*/int fpaddrnoMAX;

7、/*record file physical address num*/OSUFD_OPENMODE ifopenMAXCHILDMAXCHILD; /*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

8、=1; /*whether choice end*/char *rtrim(char *str); /*remove the trailing 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 Modify

9、FM(); /*Modify FileMode*/void OpenF(); /*Open File*/void CloseF(); /*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");fc

10、lose(fp_mfd); for(i=0;i<MAX;i+) fpaddrnoi=0; /*textattr(BLACK*16|WHITE);*/ /*clrscr();*/ /*clear screen*/ LoginF(); /*user login*/ /*clrscr();*/ if(loginsuc=1) /*Login Successfully*/ while (1) wgetchar=0; if (choiceend=1) printf("nnC:%s>",strupr(dirname); else printf("Bad comman

11、d or file name.nC:%s>",strupr(username); gets(choice);strcpy(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"

12、;)=0) choice1=4;else if(strcmp(choice,"open")=0) choice1=5;else if(strcmp(choice,"close")=0) choice1=6;else if(strcmp(choice,"read")=0) choice1=7;else if(strcmp(choice,"modify")=0) choice1=8;else if(strcmp(choice,"exit")=0) choice1=9;else if(strcmp(c

13、hoice,"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;case 2:CreateF();choiceend=1;if(!wgetchar) getchar();break;case 3:DeleteF();choiceend=1;if(!

14、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 7:choiceend=1;ReadF();if (!wgetchar) getchar();break;case 8:choiceend=1;WriteF();if (!wgetcha

15、r) 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; else printf("nAccess denied.");void help(void)printf("nThe Comman

16、d Listn");/*printf("nCd Attrib Create Modify Read Open Cls Delete Exit Closen");*/printf("Create : Create a file(You can initialize file's attribute and content.)n");printf("Open : Open a file to modifyn");printf("Close : Close a file.n");printf("

17、;Modify : 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)!='

18、; ') *(str+n+1)='0'break; else n-; if (n<0) str0='0' return str;char *ltrim(char *str) /*remove the heading blanks.*/char *rtrim(char *str);strrev(str);rtrim(str);strrev(str);return str;void LoginF() /*LOGIN FileSystem*/char loginameMAXNAME,loginpw9,logincpw9,str50;int i,j,fla

19、g=1;char a25;int findout; /*login user not exist*/char *rtrim(char *str); /*remove the trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/void InputPW(char *password); /*input password,use '*' replace*/void SetPANo(int RorW); /*Set physical address num*/while(1) findout=

20、0;printf("nnLogin Name:");gets(loginame);ltrim(rtrim(loginame);fp_mfd=fopen("c:osfilemfd","rb"); for(i=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;i+) if (strcmp(strupr(ufd_lp.ufdname),strupr(loginame)=0)findout=1;strcpy(logincpw,ufd_lp.ufdpword); fclose(fp_mfd

21、); if (findout=1) /*user exist*/printf("Login Password:"); InputPW(loginpw); /*input password,use '*' replace*/ if (strcmp(loginpw,logincpw)=0) strcpy(username,strupr(loginame); strcpy(dirname,username); fp_mfd=fopen("c:osfilemfd","rb"); for(j=0;fread(&ufd_l

22、p,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j+)strcpy(str,"c:osfile"); strcat(str,ufd_lp.ufdname); ufdj=(OSUFD*)malloc(sizeof(OSUFD); strcpy(ufdj->ufdname,strupr(ufd_lp.ufdname); fp_ufd=fopen(str,"rb"); fcountj=0; for(i=0;fread(&ufdj->ufdfilei,sizeof(OSFILE),1,fp_ufd)!=0;i+,fcou

23、ntj+) 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 printf("nn"); flag=1; while(flag) printf("Login Failed! Password Error. Try Again(Y/N):&quo

24、t;); 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*/ printf("nConfirm Password(

25、<=8):"); /*input new password,use '*' replace*/ InputPW(logincpw); if (strcmp(loginpw,logincpw)=0) strcpy(ufd_lp.ufdname,strupr(loginame); strcpy(ufd_lp.ufdpword,loginpw); fp_mfd=fopen("c:osfilemfd","ab"); fwrite(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd); fclose(f

26、p_mfd); strcpy(username,strupr(loginame); strcpy(dirname,loginame); strcpy(str,"c:osfile"); strcat(str,username); if(fp_ufd=fopen(str,"rb")=NULL)fp_ufd=fopen(str,"wb"); fclose(fp_ufd); fp_mfd=fopen("c:osfilemfd","rb"); for(j=0;fread(&ufd_lp,sizeo

27、f(OSUFD_LOGIN),1,fp_mfd)!=0;j+)strcpy(str,"c:osfile"); strcat(str,ufd_lp.ufdname); ufdj=(OSUFD*)malloc(sizeof(OSUFD); strcpy(ufdj->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.ifo

28、pen=0; ifopenji.openmode=4; fclose(fp_ufd); fclose(fp_mfd); ucount=j; SetPANo(0); printf("nnLogin Successful! Welcome to this Systemnn"); loginsuc=1; return; else printf("nn"); flag=1; while(flag) printf("Login Failed! Password Error. Try Again(Y/N):"); gets(a); ltrim(r

29、trim(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*/int i,j; if (RorW=0)if(fp_file_p=fopen("c:osfilefilefile_p","rb")=NULL) fp_file

30、_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;i<MAX;i+) if (i%13)=0) fpaddrnoi=1;*/ elsefp_file_p=fopen("c:osfilefilefile_

31、p","wb"); /*for(i=1;i<MAX;i+) if(i%13)=0) fpaddrnoi=0;*/ for(i=0;i<MAX;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;j<=7;j+)passwordj=getch(); if (int)(passw

32、ordj)!=13) if(int)(passwordj)!=8) putchar('*');else if (j>0)j-;j-; putchar('b');putchar(' ');putchar('b');else j-; else passwordj='0' break; passwordj='0'void DirF() /*Dir FileSystem*/int i,j,count=0; char sfmode25,sfpaddr25,str25; int ExistD(char *

33、dirname); /*Whether DirName Exist,Exist-i,Not Exist-0*/ /*clrscr();*/ if (strcmp(strupr(ltrim(rtrim(dirname),"")!=0) printf("nnC:%s>dirn",dirname); printf("n%14s%16s%14s%10s%18sn","FileName","FileAddress","FileLength","Type",&q

34、uot;FileMode"); j=ExistD(dirname); for(i=0;i<fcountj;i+) if (i%16=0)&&(i!=0) printf("nPress any key to continue."); getch(); /*clrscr();*/ printf("n%14s%16s%14s%10s%18sn","FileName","FileAddress","FileLength","Type","Fi

35、leMode"); itoa(ufdj->ufdfilei.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,"

36、;Read And Write");else strcpy(sfmode,"Protect");printf("%14s%16s%14d%10s%18sn",ufdj->ufdfilei.fname,sfpaddr,ufdj->ufdfilei.flength,"<FILE>",sfmode); printf("n %3d file(s)n",fcountj); else printf("nnC:>dirn"); printf("n%14s%18

37、s%8sn","DirName","OwnFileCount","Type"); for(i=0;i<ucount;i+) if (i%16=0)&&(i!=0) printf("nPress any key to continue."); getch(); /*clrscr();*/ printf("n%14s%18s%8sn","DirName","OwnFileCount","Type");prin

38、tf("%14s%18d%8sn",ufdi->ufdname,fcounti,"<UFD>");count=count+fcounti; printf("n %3d dir(s),%5d file(s)n",ucount,count); int ExistD(char *dirname) /*Whether DirName Exist,Exist-i,Not Exist-0*/int i; int exist=0; for(i=0;i<ucount;i+) if (strcmp(strupr(ufdi->

39、;ufdname),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 *ltrim(char *str); /*remove the heading blanks.*/ int ExistD(char *filename); /*Whether FileName Exist,Exist-i,

40、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(dname),"CD.")=0) strcpy(ltrim(rtrim(dirname),""); else printf("nErro

41、r.'%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 heading blanks.*/ int FindPANo(); /*find out phy

42、sical 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.n");wgetchar=1; else printf("nPl

43、ease 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

44、(fmode);if(strcmp(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->ufdfil

45、efcounti.fmode=atoi(fmode); ifopenifcounti.ifopen=0; ifopenifcounti.openmode=4; 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

46、(a); ufdi->ufdfilefcounti-1.flength=0; if(strcmp(strupr(a),"Y")=0)fp_file=fopen(str,"wb+"); ufdi->ufdfilefcounti-1.flength=WriteF1(); flag=0; else if(strcmp(strupr(a),"N")=0)flag=0;wgetchar=1; printf("n'%s' has been created successfully!n",fname)

47、; elseprintf("nFail!No Disk Space. Please format your disk.n");wgetchar=1; else printf("nError. FileMode's Range is 0-3n");wgetchar=1; int ExistF(char *filename) /*Whether FileName Exist,Exist-i,Not Exist-0*/int i,j; int exist=0; int ExistD(char *dirname); j=ExistD(dirname);

48、for(i=0;i<fcountj;i+) if (strcmp(strupr(ufdj->ufdfilei.fname),strupr(filename)=0) exist=1; break; if (exist) return(i); else return(-1);int FindPANo() /*find out physical address num*/int i; for(i=0;i<MAX;i+) if (fpaddrnoi=0) fpaddrnoi=1;break; if (i<MAX) return(i); else return(-1);int W

49、riteF1() /*write file*/int length=0; char c; printf("Please input text('#' stands for end):n"); while(c=getchar()!='#') fprintf(fp_file,"%c",c); if (c!='n') length+; fprintf(fp_file,"n"); fclose(fp_file); return(length);void DeleteF() /*Delete Fi

50、le*/char fnameMAXNAME; char str50,str150; int i,j,k,flag=1; char a25; /*whether delete*/ char *rtrim(char *str); /*remove the trailing blanks.*/ char *ltrim(char *str); /*remove the heading blanks.*/ int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/ int ExistD(char *dirname)

51、; if (strcmp(strupr(dirname),strupr(username)!=0) printf("nError. You can only delete file in your own dir.n");wgetchar=1; else printf("nPlease input FileName:"); gets(fname); ltrim(rtrim(fname); i=ExistF(fname); if (i>=0)k=ExistD(username); if(ifopenki.ifopen=1) printf("

52、nError. '%s' is in open status. Close it before delete.n",fname);wgetchar=1; else while(flag) printf("'%s' will be deleted. Are you sure(Y/N):",fname);gets(a);ltrim(rtrim(a);if(strcmp(strupr(a),"Y")=0)fpaddrnoufdk->ufdfilei.fpaddr=0;itoa(ufdk->ufdfilei.

53、fpaddr,str,10);for(j=i;j<fcountk-1;j+) strcpy(ufdk->ufdfilej.fname,ufdk->ufdfilej+1.fname); ufdk->ufdfilej.fpaddr=ufdk->ufdfilej+1.fpaddr; ufdk->ufdfilej.flength=ufdk->ufdfilej+1.flength; ufdk->ufdfilej.fmode=ufdk->ufdfilej+1.fmode; ifopenkj=ifopenkj+1; fcountk-;strcpy(str

54、1,"c:osfilefilefile");strcat(str1,str);remove(str1);flag=0;printf("n'%s' has been deleted successfully.n",fname);wgetchar=1; else if(strcmp(strupr(a),"N")=0) printf("nError. '%s' hasn't been deleted.n",fname); wgetchar=1; flag=0; else print

55、f("nError. '%s' does not exist.n",fname);wgetchar=1;void ModifyFM() /*Modify FileMode*/char fnameMAXNAME,str50; int i,j,k,flag; char fmode25; /*whether delete*/ char *rtrim(char *str); /*remove the trailing blanks.*/ char *ltrim(char *str); /*remove the heading blanks.*/ void InputPW(char *password); /*input password,use '*' replace*/ void SetPANo(int RorW); /*Set physical address n

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論