1、 - 操作系统课程设计报告 专 业: 学 号: 姓 名: 提交日期: 【设计目的】 〔1〕本实验的目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能和内部实现。 〔2〕结合数据构造、程序设计、计算机原理等课程的知识,设计一个二级文件系统,进一步理解操作系统。 〔3〕通过分对实
2、际问题的分析、设计、编程实现,提高学生实际应用、编程的能力 【设计内容】 为Linux系统设计一个简单的二级文件系统。要求做到以下几点: 1.可以实现以下几条命令: login 用户登录 dir 列目录 create 创立文件 delete 删除文件 open 翻开文件 close 关闭文件 read 读文件 write 写文件 cd 进出目录 2.列目录时要列出文件名,物理地址,保护码和文件长度 3.源文件可以进展读写保护 【实验环境】
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
4、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 fname[MAXNAME]; /*file name*/ } OSFILE; typedef struct /
5、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; typede
6、f 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 f
7、count[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][MAXCH
8、ILD]; /*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(
9、); /*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
10、 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-
11、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 *st
12、r); /*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 13、); /*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 mand or file name.\nC:\\%s>",strupr(username));
gets(ch 14、oice); //输入所选择的
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")== 15、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( 16、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 17、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; 18、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 19、CdF();choiceend=1;break;
case 20:help();choiceend=1;break;
default:choiceend=0;
}
}
}
else
printf("\nAccess denied."); //登录成功
}
void help(void)
{
printf("\nThe mand List\n");
printf("\nCd Attrib Create write Read Open Cls Delete Exit Close\n");
}
char 20、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) /*除去前端的空格*/
{ 21、
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)
{
findou 22、t=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( 23、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)
{
24、 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]=(OSU 25、FD*)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;
}
f 26、close(fp_ufd);
}
fclose(fp_mfd);
ucount=j;
SetPANo(0);
printf("\n\nLogin successful! Wele to this FileSystem\n\n");
loginsuc=1;
return;
}
else
{
printf("\n\n");
flag=1;
while(flag)
{
printf("Login Failed! Password Error. 27、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 Passw 28、ord(<=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 29、);
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\\");
strca 30、t(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++)
{
/////////////////////////// 31、//////////
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 32、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! Wele to this System\n\n");
loginsuc=1;
return;
}
else
{
33、 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)
{
log 34、insuc=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\\fil 35、e\\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 36、1;i 37、区号写进文件
}
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) //如果不是退格
38、putchar('*'); //就输入J个*号
else //如果是退格
{
if (j>0) //且密码个数大于0
{
j--;
j--;
putchar('\b');putchar(' ');putchar('\b'); //'\b'表示退格,putchar函数只能用于单个字符输出
}
else j--;
}
}
else
{ password[j]='\0'; //\0是字符串的完毕符,如果输出完毕那么终止 39、
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","Fil 40、eAddress","FileLength","Type","FileMode");
j=ExistD(dirname);
for(i=0;i 41、);
}
/////////////////////////////////////////////////////////
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" 42、);
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," 43、nC:\\>dir\n");
printf("\n%14s%18s%8s\n","DirName","OwnFileCount","Type");
for(i=0;i 44、4s%18d%8s\n",ufd[i]->ufdname,fcount[i]," 45、fdname),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(dn 46、ame)>=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 f 47、mode[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 \'% 48、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," 49、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;
ifope 50、n[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):");
get