操作系统实验2源码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#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 /*每个用户名下最多有50个文件*/
#define MAX (MAXCHILD*MAXCHILD) /*the size of fpaddrno*/
typedef struct /*定义文件*/
{
int fpaddr; /*文件地址*/
int flength; /*文件长度*/
int fmode; /*文件属性:0-Read Only;1-Write Only;2-Read and Write; 3-Protect;*/
char fname[MAXNAME]; /*文件名*/
} 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-protect*/
}OSUFD_OPENMODE;
void LoginF(); /*登录文件系统*/
void DirF(); /*文件系统目录*/
void CreateF(); /*创建文件*/
void DeleteF(); /*删除文件*/
void ModifyFM(); /*修改文件属性*/
void OpenF(); /*打开文件*/
void CloseF(); /*关闭文件*/
void ReadF(); /*读文件*/
void WriteF(); /*写文件*/
void QuitF(); /*退出*/
void CdF(); /*更改目录名称*/
void help();
char *rtrim(char *str); /*remove the trailing blanks.Rtrim() 函数清除字符串结尾空格符*/ char *ltrim(char *str); /*remove the heading blanks.ltrim() 函数从字符串左侧删除空格*/
void InputPW(char *password); /*input password,use '*' replace*/
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*/
void SetPANo(int RorW); /*Set physical address num*/
int FindPANo(); /*find out physical address num*/
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 name 用户名*/
char dirname[MAXNAME];/*record current directory使用的用户目录*/
int fpaddrno[MAX]; /*record file physical address num物理地址号,存放自己所创建的所有文件的地址*/
int wgetchar; /*whether getchar() 是否获取字符*/
OSUFD *ufd[MAXCHILD]; /*ufd and ufd own files*/
OSUFD_LOGIN ufd_lp;//用户登录结构体类型的变量
OSUFD_OPENMODE ifopen[MAXCHILD][MAXCHILD]; /*record file open/close*/
FILE *fp_mfd,*fp_ufd,*fp_file_p,*fp_file;//FILE 是变量类型,实际上是语言定义的标准数据结构,用于文件
void clrscr()/*清屏*/
{
system("cls");
}
void main()
{
int i,choice1;
char choice[50]; /*choice operation:dir,create,delete,open,delete,modify,read,write*/
int choiceend=1; /*whether choice end*/
//fopen标准函数,打开文件msd.txt,打开一个二进制文件,只允许读数据,送返指针,指向FILE类型对象。

if((fp_mfd=fopen("d:\\osfile\\mfd.txt","rb"))==NULL)
{
fp_mfd=fopen("d:\\osfile\\mfd.txt","wb");//打开一个二进制文件,只允许写数据
fclose(fp_mfd);//关闭一个流
}
for(i=0;i<MAX;i++)
fpaddrno[i]=0;
clrscr(); /*clear screen*/
LoginF(); /*user login*/
if(loginsuc==1) /*Login Successfully*/
{
while (1)
{
wgetchar=0;
if (choiceend==1)
printf("\n\nd:\\%s>",strupr(dirname));//表示以字符串型输出
else
printf("Bad command or file name.Choose help\nd:\\%s>",strupr(dirname));//strupr-变为大写字母
gets(choice);
strcpy(choice,ltrim(rtrim(strlwr(choice))));//strlwr将字符串转换为小写形式,strcpy(dest,char)复制
//ltrim(rtrim())去掉输入用户名的头部和尾部空格
if (strcmp(choice,"dir")==0) choice1=1;//strcmp相同为0
else if(strcmp(choice,"create")==0) choice1=2;
else if(strcmp(choice,"delete")==0) choice1=3;
else if(strcmp(choice,"modify")==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=12;
else choice1=13;
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.\n");
QuitF();exit(0);break; //退出
case 10:clrscr();choiceend=1;break;//清屏
case 11:CdF();choiceend=1;break;//更改目录名称
case 12:help();choiceend=1;break;//显示命令
default:choiceend=0;
}
}
}
else
printf("\nAccess denied.");
}
char *rtrim(char *str) /*去掉登陆用户名的尾空格*/
{
int n=strlen(str)-1;
while(n>=0)
{
if(*(str+n)!=' ')
{
*(str+n+1)='\0';
break;
}
else n--;
}
if (n<0) str[0]='\0';
return str;
}
char *ltrim(char *str) /*去掉登陆用户名的头空格*/
{
strrev(str);//字符的顺序颠倒
rtrim(str);
strrev(str);
return str;
}
void SetPANo(int RorW) /*Set physical address num,0-read,1-write物理地址*/ {
int i,j;
if (RorW==0)
{
if((fp_file_p=fopen("d:\\osfile\\file\\file_p.txt","rb"))==NULL)//只读
{
fp_file_p=fopen("d:\\osfile\\file\\file_p.txt","wb");//只写
fclose(fp_file_p);
}
fp_file_p=fopen("d:\\osfile\\file\\file_p.txt","rb");//只读
//fread读数据(输入地址,大小,个数,提供数据的文件),sizeof是内存空间的大小不是长度
for(i=0;fread(&j,sizeof(int),1,fp_file_p)!=0;i++)
fpaddrno[j]=1;
}
else
{
fp_file_p=fopen("d:\\osfile\\file\\file_p.txt","wb");//只写
for(i=0;i<MAX;i++)
if (fpaddrno[i]==1)
fwrite(&i,sizeof(int),1,fp_file_p);//fwrite写数据输出地址,字节数,个数,目标文件)
}
fclose(fp_file_p);
}
void InputPW(char *password) /*input password,use '*' replace*/
{
int j;
for(j=0;j<=7;j++)
{
password[j]=getch();//gerch()返回从键盘上读取到的字符
if ((int)(password[j])!=13)
{
if((int)(password[j])!=8)
putchar('*');//输出一个字符
else
{
if (j>0)
{
j--;
putchar('\b');putchar(' ');putchar('\b');
}
else j--;
}
}
else
{
password[j]='\0';
break;
}
}
password[j]='\0';
}
int ExistD(char *dirname) /*Whether DirName Exist,Exist-i,Not Exist-(-1)*/ {
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);
}
int ExistF(char *filename) /*Whether FileName Exist,Exist-i,Not Exist-(-1)*/ {
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);
if (c!='\n') length++;
}
fprintf(fp_file,"\n");
fclose(fp_file);
return(length);
}
void LoginF() /*LOGIN FileSystem登陆函数*/
{
char loginame[MAXNAME],loginpw[9],logincpw[9],str[50];//loginpw-输入密码,logincpw-确认输入密码,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("d:\\osfile\\mfd.txt","rb");//打开文件mfd
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);//关闭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("d:\\osfile\\mfd.txt","rb");
for(j=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j++)//记录用户名
{
strcpy(str,"d:\\osfile\\");
strcat(str,ufd_lp.ufdname);/*strcat(dest,char)添加char到dest结尾处*/
strcat(str,".txt");
ufd[j]=(OSUFD*)malloc(sizeof(OSUFD));//malloc向系统申请分配指定size个字节的内存空间分配二级文件空间
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/*user not exist*/
{
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("d:\\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,"d:\\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("d:\\osfile\\mfd.txt","rb");
for(j=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j++)
{
strcpy(str,"d:\\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 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\nd:\\%s>dir\n",dirname);
printf("\n%14s%16s%14s%10s%18s\n","FileName","FileAddress","FileLength","Type","Fil eMode");
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","Fil eMode");
}
itoa(ufd[j]->ufdfile[i].fpaddr,str,10);//itoa()把整数转换为字符串
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].flen gth,"<FILE>",sfmode);
}
printf("\n %3d file(s)\n",fcount[j]);
}
else//空
{
printf("\n\nd:\\>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);
}
}
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");
}
else
{
printf("\nPlease input FileName:");
gets(fname);
ltrim(rtrim(fname));
if (ExistF(fname)>=0)
{
printf("\nError. Name \'%s\' has already existed.\n",fname);
}
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,"d:\\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;
}
}
printf("\n\'%s\' has been created successfully!\n",fname);
}
else
{
printf("\nFail!No Disk Space. Please format your disk.\n");
}
}
else
{
printf("\nError. FileMode\'s Range is 0-3\n");
}
}
}
}
void DeleteF() /*Delete File*/
{ char fname[MAXNAME],str[50],str1[50];
int i,k,j;
int fpaddrno1;
if (strcmp(strupr(ltrim(rtrim(dirname))),"")==0) /*无法删除主目录的文件*/
{
printf("\nError.Please convert to ufd dir before delete.\n");
}
if (strcmp(strupr(dirname),strupr(username))!=0) /*无法删除非自己目录的文件*/ {
printf("\nError.You can only delete the file in yourself dir.\n");
}
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 delete.\n",fname);
}
else
{
if(ufd[k]->ufdfile[i].fmode==3)/*被保护的文件无法删除*/
{
printf("\nError.\'%s\' is in protect status.The file can not be deleted!.\n",fname);
}
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,"d:\\osfile\\file\\file");
itoa(fpaddrno1,str1,10);//整数转化成字符串
strcat(str,str1);
strcat(str,".txt");
remove(str);//删除物理文件
fcount[k]--;//用户文件数量少1
printf("\n \'%s\'is deleted successfully.\n",fname);
}
}
}
else//所要删除的文件不存在
{
printf("\nError. \'%s\' dose not exist.\n",fname);
}
}
}
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);
}
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 ONL Y mode successfully.\n",fname);
}
else if(strcmp(fmode,"1")==0)
{
ufd[k]->ufdfile[i].fmode=1;
printf("\n\'%s\' has been modified to WRITE ONL Y mode successfully.\n",fname);
}
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);
}
else if(strcmp(fmode,"3")==0)
{
ufd[k]->ufdfile[i].fmode=3;
printf("\n\'%s\' has been modified to FORBID mode successfully.\n",fname);
}
else
{
printf("\nError.\'%s\' is not modified.\n",fname);
}
}
}
else
{
printf("\nError. \'%s\' dose not exist.\n",fname);
}
}
}
void OpenF() /*Open File*/
{
char fname[MAXNAME];
int i,k,j;
if (strcmp(strupr(dirname),strupr(username))!=0) /*在自己的目录里才能打开*/
{
printf("\nError.You can only open in yourself dir.\n");
}
else
{
k=ExistD(username);
for(j=0;j<fcount[k];j++)
{
printf("%15s",ufd[k]->ufdfile[j].fname);
}
printf("\nPlease input FileName:");
gets(fname);//获取所要打开的文件名
ltrim(rtrim(fname));
i=ExistF(fname);//获取目录编号
if (i>=0)
{
if(ifopen[k][i].ifopen==1)//输入的文件是打开的
{
printf("\nError.\'%s\' is in open status.\n",fname);
}
else
{
ifopen[k][i].ifopen=1;//修改为打开的
if(ufd[k]->ufdfile[i].fmode==0)/*根据文件的模式设置打开模式*/
{
ifopen[k][i].openmode=0;}
else if(ufd[k]->ufdfile[i].fmode==1)
{
ifopen[k][i].openmode=1;}
else if(ufd[k]->ufdfile[i].fmode==2)
{
ifopen[k][i].openmode=2;}
else ifopen[k][i].openmode=3;
printf("\n\'%s\' is opened successfully\n",fname);
}
}
else//文件不存在
{
printf("\nError. \'%s\' dose not exist.\n",fname);
}
}
}
void CloseF() /*Close File*/
{
char fname[MAXNAME];
int i,k,j;
if (strcmp(strupr(dirname),strupr(username))!=0) /*不在自己的目录里没法进行*/ {
printf("\nError.You can only close file in yourself dir.\n");
}
else
{
k=ExistD(username);
for(j=0;j<fcount[k];j++)/*列出已经打开的文件*/
{
if(ifopen[k][j].ifopen==1)//如果文件是打开的
printf("%15s",ufd[k]->ufdfile[j].fname);
}
printf("\nPlease input FileName:");
gets(fname);//从键盘上读入所要关闭的文件
ltrim(rtrim(fname));
i=ExistF(fname);//获取文件编号
if (i>=0)
{
ifopen[k][i].ifopen=0;//关闭文件
printf("\n \'%s\' closed successfully\n",fname);
}
else//所要关闭的文件不存在
{
printf("\nError. \'%s\' dose not exist.\n",fname);
}
}
}
void ReadF() /*Read File*/
{
int i,k,n=0;
char fname[MAXNAME];
char str[255],str1[255],c;
if (strcmp(strupr(dirname),strupr(username))!=0)//必须在自己的目录内{
printf("\nError.Please convert to your own dir before read.\n");
return;
}
else
{
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);
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,"d:\\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);
}
else if(ifopen[k][i].openmode==1)//如果文件只写
{
printf("\nError.\'%s\' has been opened with WRITE ONLY mode. It isn\'t read.\n",fname);
}
else//如果文件保护
{
printf("\nError.\'%s\' has been opened with PROTECT mode. It isn\'t read.\n",fname);
}
}
else
{
printf("\nError.\'%s\' is in closing status. Please open it before read\n",fname);
}
}
else
{
printf("\nError. \'%s\' does not exist.\n",fname);
}
}
}
}
void WriteF() /*Write File*/
{
int i,k,n=0;
int length;
char fname[MAXNAME],values[1000];
char str[255],str1[255],c;
if (strcmp(strupr(dirname),strupr(username))!=0) //只能写自己目录下的文件
{
printf("\nError.Please convert to your own dir before write.\n");
}
else
{
k=ExistD(dirname);//获取目录编号
printf("\nCaution:Open file first\n");
printf("Opened File(s) List:\n");
for(i=0;i<fcount[k];i++)/*列出可以写的文件*/
{
if(ifopen[k][i].ifopen==1)//如果文件是打开的
{
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==1) ||(ifopen[k][i].openmode==2))//文件是只写,或者是可读可写的
{
itoa(ufd[k]->ufdfile[i].fpaddr,str,10);//获取文件对应的物理地址
strcpy(str1,"file");
strcat(str1,str);
strcpy(str,"d:\\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\nYou have wrote file successfully!!");
fclose(fp_file);
wgetchar=0;
}
else if(ifopen[k][i].openmode==0)//文件是只读的
{
printf("\nError.\'%s\' has been opened with READ ONL Y mode. It isn\'t write.\n",fname);
}
else//文件是保护的
{
printf("\nError.\'%s\' has been opened with PROTECT mode. It isn\'t write.\n",fname);
}
}
else //文件是关闭的
{
printf("\nError.\'%s\' is in closing status. Please open it before write\n",fname);
}
}
else //所指定的文件不存在
{
printf("\nError. \'%s\' does not exist.\n",fname);
}
}
}
}
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,"d:\\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);
}
}
void CdF() /*Exchange Dir更换用户*/
{
char dname[MAXNAME];
printf("\nPlease input 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 help(void)
{
printf("\nThe Command List\n");
printf("\ncd modify create write read open cls delete Exit close\n"); }。

相关文档
最新文档