1、. -操作系统课程设计报告专 业:学 号:姓 名:提交日期:【设计目的】1本实验的目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能和内部实现。2结合数据构造、程序设计、计算机原理等课程的知识,设计一个二级文件系统,进一步理解操作系统。3通过分对实际问题的分析、设计、编程实现,提高学生实际应用、编程的能力【设计内容】为Linux系统设计一个简单的二级文件系统。要求做到以下几点:1.可以实现以下几条命令:login 用户登录dir 列目录create 创立文件delete 删除文件open 翻开文件close 关闭文件read 读文件write 写文件cd 进出目录2列目录时要列
2、出文件名,物理地址,保护码和文件长度3源文件可以进展读写保护【实验环境】C+/VC+【源程序清单】#include stdio.h#include string.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 /*th
3、e structure of OSFILE*/int fpaddr; /*file physical address*/ int flength; /*file length*/ int fmode; /*file mode:0-Read Only;1-Write Only;2-Read and Write; 3-Protect;*/ char fnameMAXNAME; /*file name*/ OSFILE;typedef struct /*the structure of OSUFD*/char ufdnameMAXNAME; /*ufd name*/OSFILE ufdfileMAX
4、CHILD; /*ufd own file*/OSUFD;typedef struct /*the structure of OSUFDLOGIN*/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_
5、OPENMODE;OSUFD *ufdMAXCHILD; /*ufd and ufd own files*/OSUFD_LOGIN ufd_lp;int ucount=0; /*the 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 direct
6、ory*/int fpaddrnoMAX; /*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 LoginF(); /*LOGIN FileSystem*/void DirF(); /*Dir FileSystem*/void CdF(); /*Change Dir*/void Crea
7、teF(); /*Create File*/void DeleteF(); /*Delete File*/void ModifyFM(); /*Modify FileMode*/void OpenF(); /*Open File*/void CloseF(); /*Close File*/void ReadF(); /*Read File*/void WriteF(); /*Write File*/void QuitF(); /*Quit FileSystem*/void help();char *rtrim(char *str); /*remove the trailing blanks.*
8、/char *ltrim(char *str); /*remove the heading blanks.*/void InputPW(char *password); /*input password,use * replace*/void SetPANo(int RorW); /*Set physical address num*/int ExistD(char *dirname); /*Whether DirName Exist,Exist-i,Not Exist-0*/int WriteF1(); /*write file*/int ExistF(char *filename); /*
9、Whether FileName Exist,Exist-i,Not Exist-0*/int FindPANo(); /*find out physical address num*/void clrscr()system(cls);int main()int i,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
10、 trailing blanks.*/char *ltrim(char *str); /*remove the heading blanks.*/if(fp_mfd=fopen(c:osfilemfd.txt,rb)=NULL)fp_mfd=fopen(c:osfilemfd.txt,wb);fclose(fp_mfd);for(i=0;i,strupr(dirname);elseprintf(Bad mand or file name.nC:%s,strupr(username); gets(choice);/输入所选择的 strcpy(choice,ltrim(rtrim(strlwr(c
11、hoice);/将输入的值赋给choice if (strcmp(choice,dir)=0) choice1=1;/依次将输入的值与dir,create等进展比拟 else if(strcmp(choice,create)=0) choice1=2;/如果输入create将choice1置为2通过switch选择,以下依次类推 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
12、 if(strcmp(choice,close)=0) choice1=6; else if(strcmp(choice,read)=0) choice1=7; else if(strcmp(choice,write)=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 cho
13、ice1=12;/choice1=12时跳转到default,然后继续循环switch(choice1)case 1:DirF();choiceend=1;break;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:OpenF();choiceend=1;if (!wgetcha
14、r) getchar();break;case 6:CloseF();choiceend=1;if (!wgetchar) getchar();break;case 7:ReadF();choiceend=1;if (!wgetchar) getchar();break;case 8:WriteF();choiceend=1;if (!wgetchar) getchar();break;case 9:printf(nYou have exited this system.); QuitF();exit(0);break;case 10:clrscr();choiceend=1;break;ca
15、se 11:CdF();choiceend=1;break;case 20:help();choiceend=1;break;default:choiceend=0;elseprintf(nAccess denied.);/登录成功void help(void)printf(nThe mand Listn);printf(nCd Attrib Create write Read Open Cls Delete Exit Closen);char *rtrim(char *str) /*除去末尾端的空格指针指向字符串第一个*/int n=strlen(str)-1;/n为字符串长度减1while
16、(n=0)if(*(str+n)!= )/末尾不存在空格*(str+n+1)=0;/n表示完毕符break;else n-;/如果存在空格将空格去掉if (nufdname,strupr(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);pr
17、intf(nnLogin successful! Wele to this FileSystemnn);loginsuc=1;return;elseprintf(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;elseprintf(New Passwo
18、rd(=8):);InputPW(loginpw); /*input new password,use * replace*/printf(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);pr
19、intf(nnLogin Successful! Wele to this Systemnn);loginsuc=1;return; elseprintf(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) /
20、*Set physical address num,0-read,1-write*/int i,j;if (RorW=0)if(fp_file_p=fopen(c:osfilefilefile_p.txt,rb)=NULL)/如果文件未读成功 fp_file_p=fopen(c:osfilefilefile_p.txt,wb);/创立该文件fclose(fp_file_p); fp_file_p=fopen(c:osfilefilefile_p.txt,rb);/for(i=0;fread(&j,sizeof(int),1,fp_file_p)!=0;i+)fpaddrnoj=1;/真正模拟的
21、位示图的关系/*for(i=1;iMAX;i+)if (i%13)=0)fpaddrnoi=1;*/elsefp_file_p=fopen(c:osfilefilefile_p.txt,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 * repla
22、ce*/int j;for(j=0;j0)/且密码个数大于0j-;j-;putchar(b);putchar( );putchar(b); /b表示退格,putchar函数只能用于单个字符输出else j-;elsepasswordj=0; /0是字符串的完毕符,如果输出完毕那么终止break;passwordj=0;void DirF() /*Dir FileSystem*/int i,j,count=0;char sfmode25,sfpaddr25,str25;clrscr();if (strcmp(strupr(ltrim(rtrim(dirname),)!=0)printf(nnC:
23、%sdirn,dirname);printf(n%14s%16s%14s%10s%18sn,FileName,FileAddress,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 i
24、f(ufdj-ufdfilei.fmode=2)strcpy(sfmode,Read And Write);else strcpy(sfmode,Protect);printf(%14s%16s%14d%10s%18sn,ufdj-ufdfilei.fname,sfpaddr,ufdj-ufdfilei.flength,sfmode);printf(n %3d file(s)n,fcountj);elseprintf(nnC:dirn);printf(n%14s%18s%8sn,DirName,OwnFileCount,Type);for(i=0;iufdname,fcounti,);coun
25、t=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;iufdname),strupr(dirname)=0)exist=1;break;if (exist) return(i);else return(-1);void CdF() /*Exchange Dir*/char dnameMAXNAME;printf(nPlease i
26、nput DirName (cd.-Previous dir; DirNAME-cd DirNAME):);gets(dname);ltrim(rtrim(dname);if (ExistD(dname)=0) strcpy(dirname,strupr(dname);elseif(strcmp(strupr(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;c
27、har fnameMAXNAME,str50,str150,a25;char fmode25;if (strcmp(strupr(dirname),strupr(username)!=0)printf(nError. You must create file in your own dir.n);wgetchar=1;elseprintf(nPlease input FileName:);gets(fname);ltrim(rtrim(fname);if (ExistF(fname)=0)printf(nError. Name %s has already existed.n,fname);w
28、getchar=1;elseprintf(Please input FileMode(0-Read Only, 1-Write Only, 2-Read and Write, 3-Protect):);gets(fmode);ltrim(rtrim(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,
29、fname);ufdi-ufdfilefcounti.fpaddr=fpaddrno;ufdi-ufdfilefcounti.fmode=atoi(fmode);ifopenifcounti.ifopen=0;ifopenifcounti.openmode=4;strcpy(str,c:osfilefilefile);itoa(fpaddrno,str1,10);strcat(str,str1);strcat(str,.txt);fp_file=fopen(str,wb);fclose(fp_file);fcounti+;while(flag)printf(Input text now(Y/N):);get