专 业:
学 号:
姓 名:
提交日期:
【设计目的】
(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*/
{
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.*/
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","rb"))==NULL)
{
fp_mfd=fopen("c:\\osfile\\mfd.txt","wb");
fclose(fp_mfd);
}
for(i=0;i<MAX;i++) fpaddrno[i]=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("\n\nC:\\%s>",strupr(dirname));
else
printf("Bad command or file name.\nC:\\%s>",strupr(username));
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");
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","rb"); //打开该文件,返回给一个文件指针
for(i=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;i++) //检测输入登录名和mfd.txt是否一样
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","rb");
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");
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","ab");
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);
}
fp_mfd=fopen("c:\\osfile\\mfd.txt","rb");
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;
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","rb"))==NULL) //如果文件未读成功
{
fp_file_p=fopen("c:\\osfile\\file\\file_p.txt","wb"); //创建该文件
fclose(fp_file_p);
}
fp_file_p=fopen("c:\\osfile\\file\\file_p.txt","rb");
///////////////////////////////////////////////////////////////////
for(i=0;fread(&j,sizeof(int),1,fp_file_p)!=0;i++)
fpaddrno[j]=1; //真正模拟的位示图的关系
/*for(i=1;i<MAX;i++)
if ((i%13)==0)
fpaddrno[i]=1;*/
}
else
{
fp_file_p=fopen("c:\\osfile\\file\\file_p.txt","wb");
/*for(i=1;i<MAX;i++)
if((i%13)==0)
fpaddrno[i]=0;*/
for(i=0;i<MAX;i++)
if (fpaddrno[i]==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++) //密码的长度小于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",dirname);
printf("\n%14s%16s%14s%10s%18s\n","FileName","FileAddress","FileLength","Type","FileMode");
j=ExistD(dirname);
for(i=0;i<fcount[j];i++)
{
if ((i%16==0)&&(i!=0))
{
printf("\nPress any key to continue..");
getch();
clrscr();
printf("\n%14s%16s%14s%10s%18s\n","FileName","FileAddress","FileLength","Type","FileMode");
}
/////////////////////////////////////////////////////////
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",ufd[j]->ufdfile[i].fname,sfpaddr,ufd[j]->ufdfile[i].flength,"<FILE>",sfmode);
}
printf("\n %3d file(s)\n",fcount[j]);
}
else
{
printf("\n\nC:\\>dir\n");
printf("\n%14s%18s%8s\n","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%8s\n","DirName","OwnFileCount","Type");
}
printf("%14s%18d%8s\n",ufd[i]->ufdname,fcount[i],"<UFD>");
count=count+fcount[i];
}
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(ufd[i]->ufdname),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",dname);
}
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));
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((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)
{
flag=0;
wgetchar=1;
}
}
printf("\n\'%s\' has been created successfully!\n",fname);
}
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;i<fcount[j];i++)
if (strcmp(strupr(ufd[j]->ufdfile[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<MAX;i++)
if (fpaddrno[i]==0) //找到没有使用的磁盘号
{
fpaddrno[i]=1; //然后加以使用
break;
}
if (i<MAX) return(i);
else return(-1);
}
int WriteF1() /*write file*/
{
int length=0;
char c;
printf("Please input text(\'#\' stands for end):\n");
while((c=getchar())!='#')
{
fprintf(fp_file,"%c",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",fname); //文件不存在
wgetchar = 1;
}
else //如果存在文件
{
i = ExistD(username); //获取用户标识
for(int a = 0; a < fcount[i]; a++) //循环检验文件名是否匹配
{
if(strcmp(fname,ufd[i]->ufdfile[a].fname)==0) //如果文件名匹配
{
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",fname);
}
else //文件关闭成功
{
ifopen[k][i].ifopen=0; //ifopen的状态设为0
ifopen[k][i].openmode=4; //打开状态为4
printf("\'%s\'has been closed successfully!",fname);
}
}
else //文件不存在
{
printf("\nError.Name\'%s\'dose not exist.\n",fname);
}
}
}
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<fcount[k]; i++) //列出可以写的文件
{
if(ifopen[k][i].ifopen==1) //如果文件是打开的
{
printf("%15s",ufd[k]->ufdfile[i].fname);
m++;
}
if((m%4==0)&&(m!=0))
printf("\n");
}
printf("\n%d files openned.\n",m);
if(m==0) //没有打开的文件
wgetchar=1;
if(m!=0)
{
printf("\nplease input filename:");
gets(fname); //获得所要写的文件名
ltrim(rtrim(fname)); //除去前面和后面的空格
i=ExistF(fname); //获取文件的编号
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",ufd[k]->ufdfile[i].flength);
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",fname);
wgetchar=1;
}
else //文件是保护的
{
printf("\nerror.\'%s\' has been opened with protect mode . it isn\'t write.\n",fname);
wgetchar=1;
}
}
else //文件是关闭的
{
printf("\nerror.\'%s\' is in closing status.please open it before write\n",fname);
wgetchar=1;
}
}
else //文件不存在
{
printf("\nerror.\'%s\' dose not exist.\n",fname);
wgetchar=1;
}
}
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",fname);
wgetchar=1;
}
else
{
if(ufd[k]->ufdfile[i].fmode==3) //保护的文件无法删除
{
printf("error.\'%s\' is in protect status.close it before delete.\n",fname);
wgetchar=1;
}
else
{
fpaddrno1=ufd[k]->ufdfile[i].fpaddr; //获取文件对应的物理文件名
fpaddrno[fpaddrno1]=0; //收回盘块
for(j=i; j<fcount[k]; j++)
{
ufd[k]->ufdfile[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",fname);
wgetchar=1;
}
}
}
else //需要删除的文件不存在
{
printf("\nerror.\'%s\' dose not exist.\n",fname);
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);
if (i>=0)
{
k=ExistD(username);
if(ifopen[k][i].ifopen==1)
{
printf("\nError.\'%s\' is in open status. Close it before modify.\n",fname);
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",fname,strupr(str));
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",fname);
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",fname);
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",fname);
wgetchar=1;
}
else if(strcmp(fmode,"3")==0)
{
ufd[k]->ufdfile[i].fmode=3;
printf("\n\'%s\' has been modified to FORBID mode successfully.\n",fname);
wgetchar=1;
}
else
{
printf("\nError.\'%s\' is not modified.\n",fname);
wgetchar=1;
}
}
}
else
{
printf("\nError. \'%s\' dose not exist.\n",fname);
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<fcount[k];i++)
{
if (ifopen[k][i].ifopen==1)
if ((ifopen[k][i].openmode==0) ||(ifopen[k][i].openmode==2))
{
printf("%15s",ufd[k]->ufdfile[i].fname);
n++;
}
if((n%4==0)&&(n!=0)) printf("\n");
}
printf("\n%d files openned.\n",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",&c)!=EOF)
if (c=='\n') printf("\n ");
else printf("%c",c);
printf("\n\n%d Length.\n",ufd[k]->ufdfile[i].flength);
fclose(fp_file);
wgetchar=1;
}
else
{
printf("\nError.\'%s\' has been opened with WRITE ONLY mode. It isn\'t read.\n",fname);
wgetchar=1;
}
}
else
{
printf("\nError.\'%s\' is in closing status. Please open it before read\n",fname);
wgetchar=1;
}
}
else
{
printf("\nError. \'%s\' does not exist.\n",fname);
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;j<ucount;j++)
{
strcpy(str,"c:\\osfile\\");
strcat(str,ufd[j]->ufdname);
ltrim(rtrim(str));
strcat(str,".txt");
fp_ufd=fopen(str,"wb");
fclose(fp_ufd);
fp_ufd=fopen(str,"ab");
for(i=0;i<fcount[j];i++)
fwrite(&ufd[j]->ufdfile[i],sizeof(OSFILE),1,fp_ufd);
fclose(fp_ufd);
}
}
【测试结果】
1.创建和打开文件
2.写入文件
3.关闭和删除文件
【设计总结】
这次操作系统的课程设计是设计二级文件系统,虽然只有短短的两周时间,还是在考试周,时间非常的短暂,但是我从中学到了很多课本以外的知识,在课设中并不是一帆风顺,课设中需要很多的专业知识,遇到了很多的问题,通过问老师,问同学还有网上查找资料解决了,在编写程序的过程中老师还要求我们养成良好的注释习惯,方便其他人理解程序。通过这次的课设感觉自己还欠缺很多的东西,在以后我会看更多的书籍,来丰富自己的知识。
《操作系统原理》实验报告院(部):管理工程学院专业:信息管理与信息系统实验项目:实验一二三五班级:信管102姓名:学号:目录引言.…
西安郵電大學操作系统设计报告题目进程线程互斥锁院系名称计算机学院班级1104学生姓名赵大伟学号8位04113124指导教师舒新峰设…
操作系统课程论文院系班级姓名学号指导教师完成时间东莞理工学院摘要本文分析面向对象教学操作系统EOS的系统结构和代码构成通过源代码分…
操作系统课程设计实验报告姓名学号班级地点20xx年月日任务说明共完成四个任务任务一IO系统调用开销比较任务二实现一个简单的shel…
操作系统课程设计总结报告学期20xx20xx学年第2学期学院软件学院学号姓名20xx年7月3日本学期开设了操作系统课程主要学习了计…
操作系统课程设计题目与要求课程设计要求1可以依据教材中的算法自行选题也可以从下面给出的题目中选题要求每两名同学之间课程设计内容应该…
课程设计说明书设计题目操作系统课程设计班级信管学号姓名山东科技大学20xx年12月15日课程设计任务书学院信息学院专业信息管理与信…
四川大学操作系统课程设计报告学院专业年级组编号组成员软件学院第X组乔心轲姓名0743111340学号张雯姓名XXXXXXXX学号康…