搜索
您的当前位置:首页操作系统课程设计报告77292

操作系统课程设计报告77292

时间:2023-02-01 来源:飒榕旅游知识分享网


操作系统课程设计报告

专 业: 学 号: 姓 名: 提交日期:

【设计目的】

(1)本实验的目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能和内部实现。

(2)结合数据结构、程序设计、计算机原理等课程的知识,设计一个二级文件系统,进一步理解操作系统。

(3)通过分对实际问题的分析、设计、编程实现,提高学生实际应用、编程的能力

【设计内容】

为Linux系统设计一个简单的二级文件系统。要求做到以下几点: 1.可以实现下列几条命令: login 用户登录 dir 列目录 create 创建文件 delete 删除文件 open 打开文件 close 关闭文件 read 读文件 write 写文件 cd 进出目录

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 /*the 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 fname[MAXNAME]; /*file name*/ } OSFILE;

typedef struct /*the structure of OSUFD*/ {

欢迎下载

1

char ufdname[MAXNAME]; /*ufd name*/ OSFILE ufdfile[MAXCHILD]; /*ufd own file*/ }OSUFD;

typedef struct /*the structure of OSUFD'LOGIN*/ { char ufdname[MAXNAME]; /*ufd name*/ char ufdpword[8]; /*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 *ufd[MAXCHILD]; /*ufd and ufd own files*/ OSUFD_LOGIN ufd_lp;

int ucount=0; /*the count of mfd's ufds*/

int fcount[MAXCHILD]; /*the count of ufd's files*/ int loginsuc=0; /*whether login successfully*/

char username[MAXNAME]; /*record login user's name22*/ char dirname[MAXNAME];/*record current directory*/ int fpaddrno[MAX]; /*record file physical address num*/

OSUFD_OPENMODE ifopen[MAXCHILD][MAXCHILD]; /*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 CreateF(); /*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.*/

欢迎下载

2

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); /*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 choice[50]; /*choice operation:dir,create,delete,open,delete,modify,read,write*/ int choiceend=1; /*whether choice end*/ char *rtrim(char *str); /*remove the trailing blanks.*/ char *ltrim(char *str); /*remove the heading blanks.*/ if((fp_mfd=fopen(\"c:\\\\osfile\\\\mfd.txt\ { fp_mfd=fopen(\"c:\\\\osfile\\\\mfd.txt\ fclose(fp_mfd); } for(i=0;i\ else printf(\"Bad command or file name.\\nC:\\\\%s>\

欢迎下载

3

gets(choice); //输入所选择的 strcpy(choice,ltrim(rtrim(strlwr(choice)))); //将输入的值赋给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 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 choice1=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 (!wgetchar) 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; case 11:CdF();choiceend=1;break; case 20:help();choiceend=1;break; default:choiceend=0; } } } else printf(\"\\nAccess denied.\"); //登录成功 }

void help(void) { printf(\"\\nThe Command List\\n\");

欢迎下载

4

printf(\"\\nCd Attrib Create write Read Open Cls Delete Exit Close\\n\"); }

char *rtrim(char *str) /*除去末尾端的空格指针指向字符串第一个*/ { int n=strlen(str)-1; //n为字符串长度减1 while(n>=0) { if(*(str+n)!=' ') //末尾不存在空格 { *(str+n+1)='\\0'; //'\\n'表示结束符 break; } else n--; //如果存在空格将空格去掉 } if (n<0) str[0]='\\0'; return str; }

char *ltrim(char *str) /*除去前端的空格*/ { strrev(str); //把字符串str的所有字符的顺序颠倒 rtrim(str); //去掉尾端空格 strrev(str); //再颠倒过来 return str; }

void LoginF() /*LOGIN FileSystem*/ { char loginame[MAXNAME],loginpw[9],logincpw[9],str[50]; int i,j,flag=1; char a[25]; int findout; /*login user not exist*/ while(1) { findout=0; printf(\"\\n\\nLogin Name:\"); gets(loginame); //输入字符串 ltrim(rtrim(loginame)); //去掉前后端的空格 fp_mfd=fopen(\"c:\\\\osfile\\\\mfd.txt\ //打开该文件,返回给一个文件指针 for(i=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;i++) //检测输入登录名和mfd.txt是否一样

欢迎下载

5

if (strcmp(strupr(ufd_lp.ufdname),strupr(loginame))==0) //若已经注册过 { findout=1; //表示该用户已注册过 strcpy(logincpw,ufd_lp.ufdpword); } fclose(fp_mfd); 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:\\\\osfile\\\\mfd.txt\ for(j=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j++) { strcpy(str,\"c:\\\\osfile\\\\\"); strcat(str,ufd_lp.ufdname); strcat(str,\".txt\"); ufd[j]=(OSUFD*)malloc(sizeof(OSUFD)); strcpy(ufd[j]->ufdname,strupr(ufd_lp.ufdname)); fp_ufd=fopen(str,\"rb\"); fcount[j]=0;

for(i=0;fread(&ufd[j]->ufdfile[i],sizeof(OSFILE),1,fp_ufd)!=0;i++,fcount[j]++) { ifopen[j][i].ifopen=0; ifopen[j][i].openmode=4; } fclose(fp_ufd); } fclose(fp_mfd);

ucount=j;

SetPANo(0); printf(\"\\n\\nLogin successful! Welcome to this FileSystem\\n\\n\"); loginsuc=1; return; } else {

printf(\"\\n\\n\");

欢迎下载

6

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*/ printf(\"\\nConfirm Password(<=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:\\\\osfile\\\\mfd.txt\ fwrite(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd); fclose(fp_mfd); strcpy(username,strupr(loginame)); strcpy(dirname,loginame); //////////////////////////////////////////////////////// strcpy(str,\"c:\\\\osfile\\\\\"); strcat(str,username); strcat(str,\".txt\");

if((fp_ufd=fopen(str,\"rb\"))==NULL) { fp_ufd=fopen(str,\"wb\");

fclose(fp_ufd);

欢迎下载

7

} fp_mfd=fopen(\"c:\\\\osfile\\\\mfd.txt\ for(j=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j++) { ///////////////////////////////////// strcpy(str,\"c:\\\\osfile\\\\\"); strcat(str,ufd_lp.ufdname); strcat(str,\".txt\"); ufd[j]=(OSUFD*)malloc(sizeof(OSUFD)); strcpy(ufd[j]->ufdname,strupr(ufd_lp.ufdname)); fp_ufd=fopen(str,\"rb\");

for(i=0;fread(&ufd[j]->ufdfile[i],sizeof(OSFILE),1,fp_ufd)!=0;i++,fcount[j]++) { ifopen[j][i].ifopen=0; ifopen[j][i].openmode=4; } fclose(fp_ufd); } fclose(fp_mfd); ucount=j; SetPANo(0); printf(\"\\n\\nLogin Successful! Welcome to this System\\n\\n\"); loginsuc=1; return; } else { printf(\"\\n\\n\"); 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;

欢迎下载

8

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:\\\\osfile\\\\file\\\\file_p.txt\ //如果文件未读成功 { fp_file_p=fopen(\"c:\\\\osfile\\\\file\\\\file_p.txt\ //创建该文件 fclose(fp_file_p); } fp_file_p=fopen(\"c:\\\\osfile\\\\file\\\\file_p.txt\ /////////////////////////////////////////////////////////////////// for(i=0;fread(&j,sizeof(int),1,fp_file_p)!=0;i++) fpaddrno[j]=1; //真正模拟的位示图的关系 /*for(i=1;i欢迎下载

9

void InputPW(char *password) /*input password,use '*' replace*/ { int j; for(j=0;j<=7;j++) //密码的长度小于8 { password[j]=getch(); //获得输入值存入数组 if ((int)(password[j])!=13) //13表示ASCII码,如果不是回车 { if((int)(password[j])!=8) //如果不是退格 putchar('*'); //就输入J个*号 else //如果是退格 { if (j>0) //且密码个数大于0 { j--; j--; putchar('\\b');putchar(' ');putchar('\\b'); //'\\b'表示退格,putchar函数只能用于单个字符输出 } else j--; } } else { password[j]='\\0'; //\\0是字符串的结束符,如果输出完毕则终止 break; } } password[j]='\\0'; }

void DirF() /*Dir FileSystem*/ { int i,j,count=0; char sfmode[25],sfpaddr[25],str[25]; clrscr(); if (strcmp(strupr(ltrim(rtrim(dirname))),\"\")!=0) { printf(\"\\n\\nC:\\\\%s>dir\\n\ printf(\"\\n%14s%16s%14s%10s%18s\\n\eMode\"); j=ExistD(dirname); for(i=0;i欢迎下载

10

{ if ((i%16==0)&&(i!=0)) { printf(\"\\nPress any key to continue..\"); getch(); clrscr(); printf(\"\\n%14s%16s%14s%10s%18s\\n\eMode\"); } ///////////////////////////////////////////////////////// itoa(ufd[j]->ufdfile[i].fpaddr,str,10); strcpy(sfpaddr,\"file\"); strcat(sfpaddr,str); if (ufd[j]->ufdfile[i].fmode==0) strcpy(sfmode,\"Read Only\"); else if(ufd[j]->ufdfile[i].fmode==1) strcpy(sfmode,\"Write Only\"); else if(ufd[j]->ufdfile[i].fmode==2)strcpy(sfmode,\"Read And Write\"); else strcpy(sfmode,\"Protect\"); printf(\"%14s%16s%14d%10s%18s\\n\gth,\"\ } printf(\"\\n %3d file(s)\\n\ } else { printf(\"\\n\\nC:\\\\>dir\\n\"); printf(\"\\n%14s%18s%8s\\n\ for(i=0;i欢迎下载

11

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 dname[MAXNAME]; 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(\"\\nError.\\'%s\\' does not exist.\\n\}

void CreateF() /*Create File*/ { int fpaddrno,flag=1,i; char fname[MAXNAME],str[50],str1[50],a[25]; char fmode[25]; if (strcmp(strupr(dirname),strupr(username))!=0) { printf(\"\\nError. You must create file in your own dir.\\n\"); wgetchar=1; } else { printf(\"\\nPlease input FileName:\"); gets(fname); ltrim(rtrim(fname));

欢迎下载

12

if (ExistF(fname)>=0) { printf(\"\\nError. Name \\'%s\\' has already existed.\\n\ 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((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(ufd[i]->ufdfile[fcount[i]].fname,fname); ufd[i]->ufdfile[fcount[i]].fpaddr=fpaddrno; ufd[i]->ufdfile[fcount[i]].fmode=atoi(fmode); ifopen[i][fcount[i]].ifopen=0; ifopen[i][fcount[i]].openmode=4; strcpy(str,\"c:\\\\osfile\\\\file\\\\file\"); itoa(fpaddrno,str1,10); strcat(str,str1); strcat(str,\".txt\"); fp_file=fopen(str,\"wb\"); fclose(fp_file); fcount[i]++; while(flag) { printf(\"Input text now(Y/N):\"); gets(a); ltrim(rtrim(a)); ufd[i]->ufdfile[fcount[i]-1].flength=0; if(strcmp(strupr(a),\"Y\")==0) { fp_file=fopen(str,\"wb+\"); ufd[i]->ufdfile[fcount[i]-1].flength=WriteF1(); flag=0; } else if(strcmp(strupr(a),\"N\")==0)

欢迎下载

13

{ flag=0; wgetchar=1; } } printf(\"\\n\\'%s\\' has been created successfully!\\n\ } else { printf(\"\\nFail!No Disk Space. Please format your disk.\\n\"); wgetchar=1; } } else { printf(\"\\nError. FileMode\\'s Range is 0-3\\n\"); wgetchar=1; } } } }

int ExistF(char *filename) /*该函数检测某文件是否存在*/ { int i,j; int exist=0; j=ExistD(dirname); for(i=0;iufdfile[i].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欢迎下载

14

{ fpaddrno[i]=1; //然后加以使用 break; } if (iint WriteF1() /*write file*/ { int length=0; char c; printf(\"Please input text(\\'#\\' stands for end):\\n\"); while((c=getchar())!='#') { fprintf(fp_file,\"%c\ //传送格式化输出到一个fp_file文件中 if (c!='\\n') length++; } fprintf(fp_file,\"\\n\"); fclose(fp_file); return(length); }

void OpenF() /*打开文件*/ { int fc, i; //定义两个整型变量 char fname[MAXNAME], fmode[25]; //定义两个字符数组 int fm; printf(\"\\nPlease enter filename:\"); gets(fname); //从键盘获得文件名 ltrim(rtrim(fname)); //出去前后的空格 if(ExistF(fname)<0) //判断文件是否存在 { printf(\"\\nError.Name\\'%s\\'is not existed.\\n\ //文件不存在 wgetchar = 1; } else //如果存在文件 { i = ExistD(username); //获取用户标识 for(int a = 0; a < fcount[i]; a++) //循环检验文件名是否匹配 { if(strcmp(fname,ufd[i]->ufdfile[a].fname)==0) //如果文件名匹配 {

欢迎下载

15

fc=a; //记住是第几个文件 break; } } ifopen[i][fc].ifopen = 1; //设置ifopen数组的第i个用户的第fc个文件的状态未打开 printf(\"please input OpenMode(0-Read Only, 1-Write Only, 2-Read and Write, 3-Protect):\"); gets(fmode); //得到用户输入的打开权限 fm = atoi(fmode); //字符串转成整型 ifopen[i][fc].openmode = fm; //设置ifopen的打开模式 printf(\"\\nOpen successed\"); wgetchar = 1; } }

void CloseF() /*关闭文件*/ { char fname[MAXNAME]; //定义一个字符数组 int i,k; if(strcmp(strupr(dirname),strupr(username))!=0) //判断所处的目录名是否存在 { printf(\"\\nerror.you can only moddify filemode in yourself dir.\\n\"); //只能关闭自己目录下的文件 } else { printf(\"\\nplease enter filename:\"); gets(fname); //输入需要关闭的文件名 ltrim(rtrim(fname)); //除去前面和后面的空格 i = ExistF(fname); //获取用户标识 if(i>=0) { k = ExistD(username); if(ifopen[k][i].ifopen==0) //文件已被关闭 { printf(\"\\nError.Name\\'%s\\'has been closed.you can not close it again.\\n\ } else //文件关闭成功 { ifopen[k][i].ifopen=0; //ifopen的状态设为0 ifopen[k][i].openmode=4; //打开状态为4 printf(\"\\'%s\\'has been closed successfully!\

欢迎下载

16

} } else //文件不存在 { printf(\"\\nError.Name\\'%s\\'dose not exist.\\n\ } } }

void WriteF() /*Write File*/ { int i,k,m = 0; //定义三个整型变量 int length; char fname[MAXNAME]; char str[255], str1[255]; if(strcmp(strupr(dirname),strupr(username))!=0) //只能写自己目录下的文件 { printf(\"\\nerror.please convert to ufd dir before write.\\n\"); wgetchar=1; return; } printf(\"\\ncaution:open file first\\n\"); printf(\"opened file(s) list:\\n\"); k=ExistD(dirname); //获取目录编号 for(i=0; i欢迎下载

17

if(i>=0) //文件是打开的 { if(ifopen[k][i].openmode==1 || (ifopen[k][i].openmode==2)) //文件是可写或者可读可写 { itoa(ufd[k]->ufdfile[i].fpaddr,str,10); //获取文件对应的物理地址 strcpy(str1,\"file\"); strcat(str1,str); strcpy(str,\"c:\\\\osfile\\\\file\\\\\"); strcat(str,str1); strcat(str,\".txt\"); fp_file=fopen(str,\"ab\"); //以二进制只写的方式打开,每次将内容添加到文件内容的末尾 length = WriteF1(); //写入并获得长度 ufd[k]->ufdfile[i].flength=ufd[k]->ufdfile[i].flength+length; //获得总长度 printf(\"\\n\\n%d Length.\\n\ printf(\"\\n you have write file successfully!\"); fclose(fp_file); wgetchar=0; } else if(ifopen[k][i].openmode==0) //文件是只读的 { printf(\"\\nerror.\\'%s\\' has been opened with read only mode.it isn\\'t write.\\n\ wgetchar=1; } else //文件是保护的 { printf(\"\\nerror.\\'%s\\' has been opened with protect mode . it isn\\'t write.\\n\ wgetchar=1; } } else //文件是关闭的 { printf(\"\\nerror.\\'%s\\' is in closing status.please open it before write\\n\ wgetchar=1; } } else //文件不存在 { printf(\"\\nerror.\\'%s\\' dose not exist.\\n\ wgetchar=1;

欢迎下载

18

} }

void DeleteF() /*Delete File*/ { char fname[MAXNAME],str[50],str1[50]; //定义了三个数组 int i, j, k; //定义三个整形变量 int fpaddrno1; //文件的物理地址 if(strcmp(strupr(ltrim(rtrim(dirname))),\"\")==0) //将获取的文件名的前后空格去掉,再与空格比较 { printf(\"\\nerror.please convert to ufd dir before delete.\\n\"); wgetchar = 1; } if(strcmp(strupr(dirname),strupr(username))!=0) //无法删除不是自己目录的文件 { printf(\"\\nerror.you can only modify filemode in youself dir.\\n\"); wgetchar = 1; } else { printf(\"\\nplease enter filename:\\n\"); gets(fname); //从键盘获得要删除的文件名 ltrim(rtrim(fname)); //去除前面和后面的空格 i = ExistF(fname); //得到文件编号 if(i>=0) { k = ExistD(username); //获取用户名 if(ifopen[k][i].ifopen==1) //如果文件打开,则无法删除 { printf(\"\\nerror.\\'%s\\' is in open status.close it before delete.\\n\ wgetchar=1; } else { if(ufd[k]->ufdfile[i].fmode==3) //保护的文件无法删除 { printf(\"error.\\'%s\\' is in protect status.close it before delete.\\n\ wgetchar=1; } else {

欢迎下载 19

fpaddrno1=ufd[k]->ufdfile[i].fpaddr; //获取文件对应的物理文件名 fpaddrno[fpaddrno1]=0; //收回盘块 for(j=i; jufdfile[j]=ufd[k]->ufdfile[j+1]; //从被删除的文件开始,数组值全部向前移动一个 } strcpy(str,\"c:\\\\osfile\\\\file\\\\file\"); itoa(fpaddrno1,str1,10); //将整数转化成字符串 strcat(str,str1); //把str1添加到str结尾处 strcat(str,\".txt\"); //把.txt添加到str结尾处 remove(str); //删除物理文件 fcount[k]--; //K用户数量减少1 printf(\"\\n \\'%s\\' is deleted successfully.\\n\ wgetchar=1; } } } else //需要删除的文件不存在 { printf(\"\\nerror.\\'%s\\' dose not exist.\\n\ wgetchar=1; } } }

void ModifyFM() /*Modify FileMode*/ { char fname[MAXNAME],str[50]; int i,k; char fmode[25]; /*whether delete*/ if (strcmp(strupr(dirname),strupr(username))!=0) { printf(\"\\nError.You can only modify filemode in yourself dir.\\n\"); wgetchar=1; } else { printf(\"\\nPlease input FileName:\"); gets(fname); ltrim(rtrim(fname)); i=ExistF(fname);

欢迎下载

20

if (i>=0) { k=ExistD(username); if(ifopen[k][i].ifopen==1) { printf(\"\\nError.\\'%s\\' is in open status. Close it before modify.\\n\ wgetchar=1; } else { if(ufd[k]->ufdfile[i].fmode==0) strcpy(str,\"read only\"); /*FileMode*/ else if(ufd[k]->ufdfile[i].fmode==1) strcpy(str,\"write only\"); else if(ufd[k]->ufdfile[i].fmode==2) strcpy(str,\"read and write\"); else strcpy(str,\"Protect\"); printf(\"\\'%s\\' filemode is %s.\\n\ printf(\"Modify to(0-read only,1-write only,2-read and write,3-Protect):\"); gets(fmode); ltrim(rtrim(fmode)); if(strcmp(fmode,\"0\")==0) { ufd[k]->ufdfile[i].fmode=0; printf(\"\\n\\'%s\\' has been modified to READ ONLY mode successfully.\\n\ wgetchar=1; } else if(strcmp(fmode,\"1\")==0) { ufd[k]->ufdfile[i].fmode=1; printf(\"\\n\\'%s\\' has been modified to WRITE ONLY mode successfully.\\n\ wgetchar=1; } else if(strcmp(fmode,\"2\")==0) { ufd[k]->ufdfile[i].fmode=2; printf(\"\\n\\'%s\\' has been modified to READ AND WRITE mode successfully.\\n\ wgetchar=1; } else if(strcmp(fmode,\"3\")==0) { ufd[k]->ufdfile[i].fmode=3;

欢迎下载 —

21

printf(\"\\n\\'%s\\' has been modified to FORBID mode successfully.\\n\ wgetchar=1; } else { printf(\"\\nError.\\'%s\\' is not modified.\\n\ wgetchar=1; } } } else { printf(\"\\nError. \\'%s\\' dose not exist.\\n\ wgetchar=1; } } }

void ReadF() /*Read File*/ { int i,k,n=0; char fname[MAXNAME]; char str[255],str1[255],c; if (strcmp(strupr(ltrim(rtrim(dirname))),\"\")==0) { printf(\"\\nError.Please convert to ufd dir before read.\\n\"); wgetchar=1; return; } printf(\"\\nCaution:Open file first\\n\"); printf(\"Opened File(s) List:\\n\"); k=ExistD(dirname); for(i=0;i欢迎下载

22

} printf(\"\\n%d files openned.\\n\ if (n==0) wgetchar=1; if(n!=0) { printf(\"\\nPlease input FileName:\"); gets(fname); ltrim(rtrim(fname)); i=ExistF(fname); if(i>=0) { if(ifopen[k][i].ifopen==1) { if((ifopen[k][i].openmode==0) ||(ifopen[k][i].openmode==2)) { itoa(ufd[k]->ufdfile[i].fpaddr,str,10); strcpy(str1,\"file\"); strcat(str1,str); strcpy(str,\"c:\\\\osfile\\\\file\\\\\"); strcat(str,str1); strcat(str,\".txt\"); fp_file=fopen(str,\"rb\"); fseek(fp_file,0,0); printf(\"\\nThe text is:\\n\\n\"); printf(\" \"); while(fscanf(fp_file,\"%c\ if (c=='\\n') printf(\"\\n \"); else printf(\"%c\ printf(\"\\n\\n%d Length.\\n\ fclose(fp_file); wgetchar=1; } else { printf(\"\\nError.\\'%s\\' has been opened with WRITE ONLY mode. It isn\\'t read.\\n\ wgetchar=1; } } else { printf(\"\\nError.\\'%s\\' is in closing status. Please open it before read\\n\ wgetchar=1;

欢迎下载

23

} } else { printf(\"\\nError. \\'%s\\' does not exist.\\n\ wgetchar=1; } } }

void QuitF() /*Quit FileSystem*/ { int i,j; char str[50]; SetPANo(1); if (fp_mfd!=NULL) fclose(fp_mfd); if (fp_ufd!=NULL) fclose(fp_ufd); if (fp_file!=NULL) fclose(fp_file); for(j=0;jufdname); ltrim(rtrim(str)); strcat(str,\".txt\"); fp_ufd=fopen(str,\"wb\"); fclose(fp_ufd); fp_ufd=fopen(str,\"ab\"); for(i=0;iufdfile[i],sizeof(OSFILE),1,fp_ufd); fclose(fp_ufd); } }

【测试结果】 1.创建和打开文件

欢迎下载 —

24

2.写入文件

3.关闭和删除文件

欢迎下载

25

【设计总结】

这次操作系统的课程设计是设计二级文件系统,虽然只有短短的两周时间,还是在考试周,时间非常的短暂,但是我从中学到了很多课本以外的知识,在课设中并不是一帆风顺,课设中需要很多的专业知识,遇到了很多的问题,通过问老师,问同学还有网上查找资料解决了,在编写程序的过程中老师还要求我们养成良好的注释习惯,方便其他人理解程序。通过这次的课设感觉自己还欠缺很多的东西,在以后我会看更多的书籍,来丰富自己的知识。

欢迎下载 26

因篇幅问题不能全部显示,请点此查看更多更全内容

Top