操作系统模拟UNIX文件系统的设计及实现.docx
《操作系统模拟UNIX文件系统的设计及实现.docx》由会员分享,可在线阅读,更多相关《操作系统模拟UNIX文件系统的设计及实现.docx(43页珍藏版)》请在冰点文库上搜索。
操作系统模拟UNIX文件系统的设计及实现
一、模拟UNIX文件系统流程图
二、模拟UNIX文件系统源代码
/*TYPEUnixFileSysSim.cpp
*版权信息
*
*文件名称:
UnixFileSysSim.cpp
*摘要:
模拟实现UNIX的文件系统
*/
#include"UnixFileSysSim.h"
/*
*函数介绍:
寻找第一个空白的文件块ID
*输入参数:
无
*输出参数:
*返回值:
返回第一个空白块的ID
unsignedFindBlankFileBlockId()
{
unsignedcharc;
for(unsignedi=0;i{c=FS.bm.BMStart[i]|0x7F;if(c==0x7F){returni*8;//一个字节左边第一位为0,表示该区域未使用}c=FS.bm.BMStart[i]|0xBF;if(c==0xBF){returni*8+1;}c=FS.bm.BMStart[i]|0xDF;if(c==0xDF){returni*8+2;}c=FS.bm.BMStart[i]|0xEF;if(c==0xEF){returni*8+3;}c=FS.bm.BMStart[i]|0xF7;if(c==0xF7){returni*8+4;}c=FS.bm.BMStart[i]|0xFB;if(c==0xFB){returni*8+5;}c=FS.bm.BMStart[i]|0xFD;if(c==0xFD){returni*8+6;}c=FS.bm.BMStart[i]|0xFE;if(c==0xFE){returni*8+7;}}returnFILEBLOCKCOU+1;}/**函数介绍:寻找第一个文件块地址*输入参数:fileblockid文件块ID*输出参数:无*返回值:返回文件块的地址*/char*FindBlankFileBlock(unsignedfileblockid){FileBlock*fblock=FS.head;while(fblock->next!=NULL){if(fblock->FileBlockId==fileblockid){returnfblock->FileBlockAddr;}else{fblock=fblock->next;}}returnNULL;}/**函数介绍:得到当前时间的字符串*输入参数:时间字符串的指针*输出参数:无*返回值:void*/voidGetCurrentTime(char*currtime){chardbuffer[9];chartbuffer[9];_strdate(dbuffer);_strtime(tbuffer);strcpy(currtime,dbuffer);strcat(currtime,"");strcat(currtime,tbuffer);}/**函数介绍:更新文件索引*输入参数:fileblockid文件块ID*输出参数:无*返回值:无*/voidAddFileIndex(unsignedfileblockid,unsignedfilelevel,char*filename,char*parentname){FS.FI.FIStart[FS.FI.FICount].FileBlockId=fileblockid;FS.FI.FIStart[FS.FI.FICount].FileLevel=filelevel;strcpy(FS.FI.FIStart[FS.FI.FICount].FileName,filename);if(parentname==NULL){memset(FS.FI.FIStart[FS.FI.FICount].ParentName,'\0',MAXFILENAMELEN);}else{strcpy(FS.FI.FIStart[FS.FI.FICount].ParentName,parentname);}FS.FI.FIStart[FS.FI.FICount].Index=FS.FI.FICount;FS.FI.FIStart[FS.FI.FICount].effect=1;FS.FI.FICount++;}/**函数介绍:更新位示图*输入参数:fileblockid文件块ID*输出参数:无*返回值:无*/voidUpdateBitMap(unsignedfileblockid){//计复所在位示图的位置intdirInBitmap=((int)(fileblockid/8));intdirInChar=fileblockid%8;char*c=&(FS.bm.BMStart[dirInBitmap]);charxor;switch(dirInChar){case0:xor=0x80;break;case1:xor=0x40;break;case2:xor=0x20;break;case3:xor=0x10;break;case4:xor=0x08;break;case5:xor=0x04;break;case6:xor=0x02;break;case7:xor=0x01;break;}*c=*c^xor;}/**函数介绍:创建一个文件元素*输入参数:acc文件元素可操作权限,filename文件元素名称,type文件元素类型,filecontent文件内容*输出参数:无*返回值:返回一个文件元素的指针*/FSElement*CreateFileElement(FEAccessacc,char*filename,FETypetype,char*filecontent,FSElement*parent){//查找第一个空白文件块IDunsignedblankFileBlockId=FindBlankFileBlockId();if(blankFileBlockId>=FILEBLOCKCOU){printf("未找到一个文件块的id\n");returnNULL;}//查找第一个空白块的地址char*blank=FindBlankFileBlock(blankFileBlockId);if(blank==NULL){printf("未找到一个文件块的地址\n");returnNULL;}FSElement*fs=(FSElement*)blank;fs->Access=acc;fs->Creator=CS.CurrentUser;GetCurrentTime(fs->CreateTime);fs->FileBlockId=blankFileBlockId;fs->FileLevel=CS.FileLevel;strcpy(fs->FileName,filename);strcpy(fs->LastModTime,fs->CreateTime);fs->Type=type;fs->parent=parent;if(type==dir){fs->FileElemLen=sizeof(FSElement);fs->FileData=NULL;}else{fs->FileElemLen=(unsigned)strlen(filename);fs->fileStu=closed;fs->FileData=(char*)fs+sizeof(FSElement);if(filecontent==NULL){}else{strcpy(fs->FileData,filecontent);}}//更新索引if(parent==NULL){AddFileIndex(blankFileBlockId,CS.FileLevel,filename,NULL);}else{AddFileIndex(blankFileBlockId,CS.FileLevel,filename,parent->FileName);}//更新BITMAPUpdateBitMap(blankFileBlockId);returnfs;}/**函数介绍:创建文件块链表*输入参数:datahead第一块数据的地址,blockcap一个文件块的大小,len链表的长度*输出参数:无*返回值:返回链表的头指针*/FileBlock*CreateFileBlockList(char*datahead,unsignedblockcap,unsignedlen){if(datahead==NULL||len==0){returnNULL;}FileBlock*head;FileBlock*pnew;FileBlock*pold;head=pold=pnew=(FileBlock*)malloc(sizeof(FileBlock));for(unsignedi=0;i{pold->FileBlockId=i;pold->FileBlockCap=FILEBLOCKCAP;pold->FileBlockAddr=datahead+i*blockcap;memset(pold->FileBlockAddr,'\0',blockcap);if(i!=len-1){pnew=(FileBlock*)malloc(sizeof(FileBlock));}else{pnew=NULL;}pold->next=pnew;pold=pnew;}returnhead;}/**函数介绍:初始化模拟文件系统*输入参数:无*输出参数:无*返回值:true-初始化成功,false-初始化失败*/boolInitFileSys(){//初始化模拟的文件系统if((FS.FSStart=(char*)malloc(FILESYSCAP))==NULL){returnfalse;}FS.FileSystemCap=FILESYSCAP;FS.bm.BitMapLen=BITMAPLEN;FS.bm.BMStart=FS.FSStart;//设置位示图为未使用memset(FS.bm.BMStart,'\0',FS.bm.BitMapLen);//初始化文件系统索引FS.FI.FIStart=(FileIndexElement*)(FS.FSStart+BITMAPLEN);//因为是模拟系统,暂定一个文件或文件夹最多占用一个文件块,一个文件块只放一个文件元素FS.FI.FILen=sizeof(FileIndexElement)*FILEBLOCKCOU+sizeof(unsigned)*2;FS.FI.FICount=0;memset(FS.FI.FIStart,'\0',FS.FI.FILen);//初始化文件块FS.FileBlockCou=FILEBLOCKCOU;FS.head=CreateFileBlockList((FS.FSStart+FILESYSCAP-FILEBLOCKCAP*FILEBLOCKCOU),FILEBLOCKCAP,FS.FileBlockCou);//区域的后FILEBLOCKCAP*FILEBLOCKCOU个单元用来存储数据if(FS.head==NULL){returnfalse;} //初始化系统当前状态CS.CurrentUser.UserName=(char*)calloc(10,sizeof(char));strcpy(CS.CurrentUser.UserName,"man");CS.CurrentUser.ut=admin;CS.CurrParent=NULL;CS.FileLevel=0;CS.CurrentPath=(char*)calloc(1000,sizeof(char));//创建一个根目录base=CreateFileElement(pub,"root",dir,NULL,NULL);if(base==NULL){returnfalse;}else{returntrue;}}/**函数介绍:系统登录模块*输入参数:无*输出参数:无*返回值:true登录成功,false登录失败*/boolLogin(){charusername[10];charpassword[MAXPASSWORDLEN];intc;for(c=0;c{inti=0;memset(password,'\0',MAXPASSWORDLEN);memset(username,'\0',10);printf("login:");gets(username);printf("password:");while(i=0x0d){};password[strlen(password)-1]='\0';printf("\nusername:%s\npassword:%s\n",username,password);if((strcmp(username,"user1")==0&&strcmp(password,"user1")==0)||(strcmp(username,"user2")==0&&strcmp(password,"user2")==0)||(strcmp(username,"user3")==0&&strcmp(password,"user3")==0)||(strcmp(username,"user4")==0&&strcmp(password,"user4")==0)||(strcmp(username,"user5")==0&&strcmp(password,"user5")==0)||(strcmp(username,"user6")==0&&strcmp(password,"user6")==0)||(strcmp(username,"user7")==0&&strcmp(password,"user7")==0)||(strcmp(username,"user8")==0&&strcmp(password,"user8")==0)||(strcmp(username,"super")==0&&strcmp(password,"super")==0)){if(strcmp(username,"super")==0)//一个管理员{strcpy(CS.CurrentUser.UserName,username);CS.CurrentUser.ut=admin;}else{//memset(CS.CurrentUser.UserName,'\0',10);strcpy(CS.CurrentUser.UserName,username);CS.CurrentUser.ut=comm;}CS.FileLevel++;CS.CurrParent=base;strcpy(CS.CurrentPath,"\\");printf("\n欢迎使用模拟UNIX文件系统。\n");printf("\n%s%s\n$",username,CS.CurrentPath);break;}else{printf("\n用户名或密码错误,请重新输入。\n");}}if(c>=USERTESTLOGINCOU)//非法用户{printf("\n对不起,您不是该系统用户,按任意键退出系统。\n");returnfalse;}else{returntrue;}}/**函数介绍:命令分解*输入参数:command用户输入命令字,key关键字,path路径*输出参数:无*返回值:无*/voidFindCommKey(char*command,char*key,char*path){for(unsignedi=0;i{if(command[i]==''){i++;if(i{strcpy(path,command+i);}break;}if(i<=9){key[i]=command[i];}else{for(unsignedj=i;j{if(command[j]!=''){strcpy(path,command+j);break;}}break;}}strlwr(key);strlwr(path);}/**函数介绍:创建一个文件*输入参数:无*输出参数:无*返回值:无*/voidCreate(char*filename){if(strcmp(filename,"")==0){printf("对不起,文件名不能为空。\n");}else{CreateFileElement(protect,filename,file,NULL,CS.CurrParent);}printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:列出当前目录的文件和文件夹*输入参数:path路径*输出参数:无*返回值:无*/voidDir(char*path){chardisplay[1000];memset(display,'\0',1000);//查找显示内容for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)==0&&FS.FI.FIStart[i].FileLevel==CS.FileLevel&&FS.FI.FIStart[i].effect==1){strcat(display,FS.FI.FIStart[i].FileName);strcat(display,"\t\t");}}printf("\n%s\n",display);printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:打开当前目录的文件*输入参数:path路径*输出参数:无*返回值:无*/voidOpen(char*path){chardisplay[100];for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)
c=FS.bm.BMStart[i]|0x7F;
if(c==0x7F)
returni*8;//一个字节左边第一位为0,表示该区域未使用
}
c=FS.bm.BMStart[i]|0xBF;
if(c==0xBF)
returni*8+1;
c=FS.bm.BMStart[i]|0xDF;
if(c==0xDF)
returni*8+2;
c=FS.bm.BMStart[i]|0xEF;
if(c==0xEF)
returni*8+3;
c=FS.bm.BMStart[i]|0xF7;
if(c==0xF7)
returni*8+4;
c=FS.bm.BMStart[i]|0xFB;
if(c==0xFB)
returni*8+5;
c=FS.bm.BMStart[i]|0xFD;
if(c==0xFD)
returni*8+6;
c=FS.bm.BMStart[i]|0xFE;
if(c==0xFE)
returni*8+7;
returnFILEBLOCKCOU+1;
寻找第一个文件块地址
fileblockid文件块ID
返回文件块的地址
char*FindBlankFileBlock(unsignedfileblockid)
FileBlock*fblock=FS.head;
while(fblock->next!
=NULL)
if(fblock->FileBlockId==fileblockid)
returnfblock->FileBlockAddr;
else
fblock=fblock->next;
returnNULL;
得到当前时间的字符串
时间字符串的指针
void
voidGetCurrentTime(char*currtime)
chardbuffer[9];
chartbuffer[9];
_strdate(dbuffer);
_strtime(tbuffer);
strcpy(currtime,dbuffer);
strcat(currtime,"");
strcat(currtime,tbuffer);
更新文件索引
voidAddFileIndex(unsignedfileblockid,unsignedfilelevel,char*filename,char*parentname)
FS.FI.FIStart[FS.FI.FICount].FileBlockId=fileblockid;
FS.FI.FIStart[FS.FI.FICount].FileLevel=filelevel;
strcpy(FS.FI.FIStart[FS.FI.FICount].FileName,filename);
if(parentname==NULL)
memset(FS.FI.FIStart[FS.FI.FICount].ParentName,'\0',MAXFILENAMELEN);
strcpy(FS.FI.FIStart[FS.FI.FICount].ParentName,parentname);
FS.FI.FIStart[FS.FI.FICount].Index=FS.FI.FICount;
FS.FI.FIStart[FS.FI.FICount].effect=1;
FS.FI.FICount++;
更新位示图
voidUpdateBitMap(unsignedfileblockid)
//计复所在位示图的位置
intdirInBitmap=((int)(fileblockid/8));
intdirInChar=fileblockid%8;
char*c=&(FS.bm.BMStart[dirInBitmap]);
charxor;
switch(dirInChar)
case0:
xor=0x80;
break;
case1:
xor=0x40;
case2:
xor=0x20;
case3:
xor=0x10;
case4:
xor=0x08;
case5:
xor=0x04;
case6:
xor=0x02;
case7:
xor=0x01;
*c=*c^xor;
创建一个文件元素
acc文件元素可操作权限,filename文件元素名称,type文件元素类型,filecontent文件内容
返回一个文件元素的指针
FSElement*CreateFileElement(FEAccessacc,char*filename,FETypetype,char*filecontent,FSElement*parent)
//查找第一个空白文件块ID
unsignedblankFileBlockId=FindBlankFileBlockId();
if(blankFileBlockId>=FILEBLOCKCOU)
printf("未找到一个文件块的id\n");
//查找第一个空白块的地址
char*blank=FindBlankFileBlock(blankFileBlockId);
if(blank==NULL)
printf("未找到一个文件块的地址\n");
FSElement*fs=(FSElement*)blank;
fs->Access=acc;
fs->Creator=CS.CurrentUser;
GetCurrentTime(fs->CreateTime);
fs->FileBlockId=blankFileBlockId;
fs->FileLevel=CS.FileLevel;
strcpy(fs->FileName,filename);
strcpy(fs->LastModTime,fs->CreateTime);
fs->Type=type;
fs->parent=parent;
if(type==dir)
fs->FileElemLen=sizeof(FSElement);
fs->FileData=NULL;
fs->FileElemLen=(unsigned)strlen(filename);
fs->fileStu=closed;
fs->FileData=(char*)fs+sizeof(FSElement);
if(filecontent==NULL)
strcpy(fs->FileData,filecontent);
//更新索引
if(parent==NULL)
AddFileIndex(blankFileBlockId,CS.FileLevel,filename,NULL);
AddFileIndex(blankFileBlockId,CS.FileLevel,filename,parent->FileName);
//更新BITMAP
UpdateBitMap(blankFileBlockId);
returnfs;
创建文件块链表
datahead第一块数据的地址,blockcap一个文件块的大小,len链表的长度
返回链表的头指针
FileBlock*CreateFileBlockList(char*datahead,unsignedblockcap,unsignedlen)
if(datahead==NULL||len==0)
FileBlock*head;
FileBlock*pnew;
FileBlock*pold;
head=pold=pnew=(FileBlock*)malloc(sizeof(FileBlock));
for(unsignedi=0;i{pold->FileBlockId=i;pold->FileBlockCap=FILEBLOCKCAP;pold->FileBlockAddr=datahead+i*blockcap;memset(pold->FileBlockAddr,'\0',blockcap);if(i!=len-1){pnew=(FileBlock*)malloc(sizeof(FileBlock));}else{pnew=NULL;}pold->next=pnew;pold=pnew;}returnhead;}/**函数介绍:初始化模拟文件系统*输入参数:无*输出参数:无*返回值:true-初始化成功,false-初始化失败*/boolInitFileSys(){//初始化模拟的文件系统if((FS.FSStart=(char*)malloc(FILESYSCAP))==NULL){returnfalse;}FS.FileSystemCap=FILESYSCAP;FS.bm.BitMapLen=BITMAPLEN;FS.bm.BMStart=FS.FSStart;//设置位示图为未使用memset(FS.bm.BMStart,'\0',FS.bm.BitMapLen);//初始化文件系统索引FS.FI.FIStart=(FileIndexElement*)(FS.FSStart+BITMAPLEN);//因为是模拟系统,暂定一个文件或文件夹最多占用一个文件块,一个文件块只放一个文件元素FS.FI.FILen=sizeof(FileIndexElement)*FILEBLOCKCOU+sizeof(unsigned)*2;FS.FI.FICount=0;memset(FS.FI.FIStart,'\0',FS.FI.FILen);//初始化文件块FS.FileBlockCou=FILEBLOCKCOU;FS.head=CreateFileBlockList((FS.FSStart+FILESYSCAP-FILEBLOCKCAP*FILEBLOCKCOU),FILEBLOCKCAP,FS.FileBlockCou);//区域的后FILEBLOCKCAP*FILEBLOCKCOU个单元用来存储数据if(FS.head==NULL){returnfalse;} //初始化系统当前状态CS.CurrentUser.UserName=(char*)calloc(10,sizeof(char));strcpy(CS.CurrentUser.UserName,"man");CS.CurrentUser.ut=admin;CS.CurrParent=NULL;CS.FileLevel=0;CS.CurrentPath=(char*)calloc(1000,sizeof(char));//创建一个根目录base=CreateFileElement(pub,"root",dir,NULL,NULL);if(base==NULL){returnfalse;}else{returntrue;}}/**函数介绍:系统登录模块*输入参数:无*输出参数:无*返回值:true登录成功,false登录失败*/boolLogin(){charusername[10];charpassword[MAXPASSWORDLEN];intc;for(c=0;c{inti=0;memset(password,'\0',MAXPASSWORDLEN);memset(username,'\0',10);printf("login:");gets(username);printf("password:");while(i=0x0d){};password[strlen(password)-1]='\0';printf("\nusername:%s\npassword:%s\n",username,password);if((strcmp(username,"user1")==0&&strcmp(password,"user1")==0)||(strcmp(username,"user2")==0&&strcmp(password,"user2")==0)||(strcmp(username,"user3")==0&&strcmp(password,"user3")==0)||(strcmp(username,"user4")==0&&strcmp(password,"user4")==0)||(strcmp(username,"user5")==0&&strcmp(password,"user5")==0)||(strcmp(username,"user6")==0&&strcmp(password,"user6")==0)||(strcmp(username,"user7")==0&&strcmp(password,"user7")==0)||(strcmp(username,"user8")==0&&strcmp(password,"user8")==0)||(strcmp(username,"super")==0&&strcmp(password,"super")==0)){if(strcmp(username,"super")==0)//一个管理员{strcpy(CS.CurrentUser.UserName,username);CS.CurrentUser.ut=admin;}else{//memset(CS.CurrentUser.UserName,'\0',10);strcpy(CS.CurrentUser.UserName,username);CS.CurrentUser.ut=comm;}CS.FileLevel++;CS.CurrParent=base;strcpy(CS.CurrentPath,"\\");printf("\n欢迎使用模拟UNIX文件系统。\n");printf("\n%s%s\n$",username,CS.CurrentPath);break;}else{printf("\n用户名或密码错误,请重新输入。\n");}}if(c>=USERTESTLOGINCOU)//非法用户{printf("\n对不起,您不是该系统用户,按任意键退出系统。\n");returnfalse;}else{returntrue;}}/**函数介绍:命令分解*输入参数:command用户输入命令字,key关键字,path路径*输出参数:无*返回值:无*/voidFindCommKey(char*command,char*key,char*path){for(unsignedi=0;i{if(command[i]==''){i++;if(i{strcpy(path,command+i);}break;}if(i<=9){key[i]=command[i];}else{for(unsignedj=i;j{if(command[j]!=''){strcpy(path,command+j);break;}}break;}}strlwr(key);strlwr(path);}/**函数介绍:创建一个文件*输入参数:无*输出参数:无*返回值:无*/voidCreate(char*filename){if(strcmp(filename,"")==0){printf("对不起,文件名不能为空。\n");}else{CreateFileElement(protect,filename,file,NULL,CS.CurrParent);}printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:列出当前目录的文件和文件夹*输入参数:path路径*输出参数:无*返回值:无*/voidDir(char*path){chardisplay[1000];memset(display,'\0',1000);//查找显示内容for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)==0&&FS.FI.FIStart[i].FileLevel==CS.FileLevel&&FS.FI.FIStart[i].effect==1){strcat(display,FS.FI.FIStart[i].FileName);strcat(display,"\t\t");}}printf("\n%s\n",display);printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:打开当前目录的文件*输入参数:path路径*输出参数:无*返回值:无*/voidOpen(char*path){chardisplay[100];for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)
pold->FileBlockId=i;
pold->FileBlockCap=FILEBLOCKCAP;
pold->FileBlockAddr=datahead+i*blockcap;
memset(pold->FileBlockAddr,'\0',blockcap);
if(i!
=len-1)
pnew=(FileBlock*)malloc(sizeof(FileBlock));
pnew=NULL;
pold->next=pnew;
pold=pnew;
returnhead;
初始化模拟文件系统
true-初始化成功,false-初始化失败
boolInitFileSys()
//初始化模拟的文件系统
if((FS.FSStart=(char*)malloc(FILESYSCAP))==NULL)
returnfalse;
FS.FileSystemCap=FILESYSCAP;
FS.bm.BitMapLen=BITMAPLEN;
FS.bm.BMStart=FS.FSStart;
//设置位示图为未使用
memset(FS.bm.BMStart,'\0',FS.bm.BitMapLen);
//初始化文件系统索引
FS.FI.FIStart=(FileIndexElement*)(FS.FSStart+BITMAPLEN);
//因为是模拟系统,暂定一个文件或文件夹最多占用一个文件块,一个文件块只放一个文件元素
FS.FI.FILen=sizeof(FileIndexElement)*FILEBLOCKCOU+sizeof(unsigned)*2;
FS.FI.FICount=0;
memset(FS.FI.FIStart,'\0',FS.FI.FILen);
//初始化文件块
FS.FileBlockCou=FILEBLOCKCOU;
FS.head=CreateFileBlockList((FS.FSStart+FILESYSCAP-FILEBLOCKCAP*FILEBLOCKCOU),
FILEBLOCKCAP,FS.FileBlockCou);//区域的后FILEBLOCKCAP*FILEBLOCKCOU个单元用来存储数据
if(FS.head==NULL)
//初始化系统当前状态
CS.CurrentUser.UserName=(char*)calloc(10,sizeof(char));
strcpy(CS.CurrentUser.UserName,"man");
CS.CurrentUser.ut=admin;
CS.CurrParent=NULL;
CS.FileLevel=0;
CS.CurrentPath=(char*)calloc(1000,sizeof(char));
//创建一个根目录
base=CreateFileElement(pub,"root",dir,NULL,NULL);
if(base==NULL)
returntrue;
系统登录模块
true登录成功,false登录失败
boolLogin()
charusername[10];
charpassword[MAXPASSWORDLEN];
intc;
for(c=0;c{inti=0;memset(password,'\0',MAXPASSWORDLEN);memset(username,'\0',10);printf("login:");gets(username);printf("password:");while(i=0x0d){};password[strlen(password)-1]='\0';printf("\nusername:%s\npassword:%s\n",username,password);if((strcmp(username,"user1")==0&&strcmp(password,"user1")==0)||(strcmp(username,"user2")==0&&strcmp(password,"user2")==0)||(strcmp(username,"user3")==0&&strcmp(password,"user3")==0)||(strcmp(username,"user4")==0&&strcmp(password,"user4")==0)||(strcmp(username,"user5")==0&&strcmp(password,"user5")==0)||(strcmp(username,"user6")==0&&strcmp(password,"user6")==0)||(strcmp(username,"user7")==0&&strcmp(password,"user7")==0)||(strcmp(username,"user8")==0&&strcmp(password,"user8")==0)||(strcmp(username,"super")==0&&strcmp(password,"super")==0)){if(strcmp(username,"super")==0)//一个管理员{strcpy(CS.CurrentUser.UserName,username);CS.CurrentUser.ut=admin;}else{//memset(CS.CurrentUser.UserName,'\0',10);strcpy(CS.CurrentUser.UserName,username);CS.CurrentUser.ut=comm;}CS.FileLevel++;CS.CurrParent=base;strcpy(CS.CurrentPath,"\\");printf("\n欢迎使用模拟UNIX文件系统。\n");printf("\n%s%s\n$",username,CS.CurrentPath);break;}else{printf("\n用户名或密码错误,请重新输入。\n");}}if(c>=USERTESTLOGINCOU)//非法用户{printf("\n对不起,您不是该系统用户,按任意键退出系统。\n");returnfalse;}else{returntrue;}}/**函数介绍:命令分解*输入参数:command用户输入命令字,key关键字,path路径*输出参数:无*返回值:无*/voidFindCommKey(char*command,char*key,char*path){for(unsignedi=0;i{if(command[i]==''){i++;if(i{strcpy(path,command+i);}break;}if(i<=9){key[i]=command[i];}else{for(unsignedj=i;j{if(command[j]!=''){strcpy(path,command+j);break;}}break;}}strlwr(key);strlwr(path);}/**函数介绍:创建一个文件*输入参数:无*输出参数:无*返回值:无*/voidCreate(char*filename){if(strcmp(filename,"")==0){printf("对不起,文件名不能为空。\n");}else{CreateFileElement(protect,filename,file,NULL,CS.CurrParent);}printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:列出当前目录的文件和文件夹*输入参数:path路径*输出参数:无*返回值:无*/voidDir(char*path){chardisplay[1000];memset(display,'\0',1000);//查找显示内容for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)==0&&FS.FI.FIStart[i].FileLevel==CS.FileLevel&&FS.FI.FIStart[i].effect==1){strcat(display,FS.FI.FIStart[i].FileName);strcat(display,"\t\t");}}printf("\n%s\n",display);printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:打开当前目录的文件*输入参数:path路径*输出参数:无*返回值:无*/voidOpen(char*path){chardisplay[100];for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)
inti=0;
memset(password,'\0',MAXPASSWORDLEN);
memset(username,'\0',10);
printf("login:
");
gets(username);
printf("password:
while(i=0x0d){};password[strlen(password)-1]='\0';printf("\nusername:%s\npassword:%s\n",username,password);if((strcmp(username,"user1")==0&&strcmp(password,"user1")==0)||(strcmp(username,"user2")==0&&strcmp(password,"user2")==0)||(strcmp(username,"user3")==0&&strcmp(password,"user3")==0)||(strcmp(username,"user4")==0&&strcmp(password,"user4")==0)||(strcmp(username,"user5")==0&&strcmp(password,"user5")==0)||(strcmp(username,"user6")==0&&strcmp(password,"user6")==0)||(strcmp(username,"user7")==0&&strcmp(password,"user7")==0)||(strcmp(username,"user8")==0&&strcmp(password,"user8")==0)||(strcmp(username,"super")==0&&strcmp(password,"super")==0)){if(strcmp(username,"super")==0)//一个管理员{strcpy(CS.CurrentUser.UserName,username);CS.CurrentUser.ut=admin;}else{//memset(CS.CurrentUser.UserName,'\0',10);strcpy(CS.CurrentUser.UserName,username);CS.CurrentUser.ut=comm;}CS.FileLevel++;CS.CurrParent=base;strcpy(CS.CurrentPath,"\\");printf("\n欢迎使用模拟UNIX文件系统。\n");printf("\n%s%s\n$",username,CS.CurrentPath);break;}else{printf("\n用户名或密码错误,请重新输入。\n");}}if(c>=USERTESTLOGINCOU)//非法用户{printf("\n对不起,您不是该系统用户,按任意键退出系统。\n");returnfalse;}else{returntrue;}}/**函数介绍:命令分解*输入参数:command用户输入命令字,key关键字,path路径*输出参数:无*返回值:无*/voidFindCommKey(char*command,char*key,char*path){for(unsignedi=0;i{if(command[i]==''){i++;if(i{strcpy(path,command+i);}break;}if(i<=9){key[i]=command[i];}else{for(unsignedj=i;j{if(command[j]!=''){strcpy(path,command+j);break;}}break;}}strlwr(key);strlwr(path);}/**函数介绍:创建一个文件*输入参数:无*输出参数:无*返回值:无*/voidCreate(char*filename){if(strcmp(filename,"")==0){printf("对不起,文件名不能为空。\n");}else{CreateFileElement(protect,filename,file,NULL,CS.CurrParent);}printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:列出当前目录的文件和文件夹*输入参数:path路径*输出参数:无*返回值:无*/voidDir(char*path){chardisplay[1000];memset(display,'\0',1000);//查找显示内容for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)==0&&FS.FI.FIStart[i].FileLevel==CS.FileLevel&&FS.FI.FIStart[i].effect==1){strcat(display,FS.FI.FIStart[i].FileName);strcat(display,"\t\t");}}printf("\n%s\n",display);printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:打开当前目录的文件*输入参数:path路径*输出参数:无*返回值:无*/voidOpen(char*path){chardisplay[100];for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)
=0x0d){};
password[strlen(password)-1]='\0';
printf("\nusername:
%s\npassword:
%s\n",username,password);
if((strcmp(username,"user1")==0&&strcmp(password,"user1")==0)||
(strcmp(username,"user2")==0&&strcmp(password,"user2")==0)||
(strcmp(username,"user3")==0&&strcmp(password,"user3")==0)||
(strcmp(username,"user4")==0&&strcmp(password,"user4")==0)||
(strcmp(username,"user5")==0&&strcmp(password,"user5")==0)||
(strcmp(username,"user6")==0&&strcmp(password,"user6")==0)||
(strcmp(username,"user7")==0&&strcmp(password,"user7")==0)||
(strcmp(username,"user8")==0&&strcmp(password,"user8")==0)||
(strcmp(username,"super")==0&&strcmp(password,"super")==0))
if(strcmp(username,"super")==0)//一个管理员
strcpy(CS.CurrentUser.UserName,username);
//memset(CS.CurrentUser.UserName,'\0',10);
CS.CurrentUser.ut=comm;
CS.FileLevel++;
CS.CurrParent=base;
strcpy(CS.CurrentPath,"\\");
printf("\n欢迎使用模拟UNIX文件系统。
\n");
printf("\n%s%s\n$",username,CS.CurrentPath);
printf("\n用户名或密码错误,请重新输入。
if(c>=USERTESTLOGINCOU)//非法用户
printf("\n对不起,您不是该系统用户,按任意键退出系统。
命令分解
command用户输入命令字,key关键字,path路径
voidFindCommKey(char*command,char*key,char*path)
for(unsignedi=0;i{if(command[i]==''){i++;if(i{strcpy(path,command+i);}break;}if(i<=9){key[i]=command[i];}else{for(unsignedj=i;j{if(command[j]!=''){strcpy(path,command+j);break;}}break;}}strlwr(key);strlwr(path);}/**函数介绍:创建一个文件*输入参数:无*输出参数:无*返回值:无*/voidCreate(char*filename){if(strcmp(filename,"")==0){printf("对不起,文件名不能为空。\n");}else{CreateFileElement(protect,filename,file,NULL,CS.CurrParent);}printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:列出当前目录的文件和文件夹*输入参数:path路径*输出参数:无*返回值:无*/voidDir(char*path){chardisplay[1000];memset(display,'\0',1000);//查找显示内容for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)==0&&FS.FI.FIStart[i].FileLevel==CS.FileLevel&&FS.FI.FIStart[i].effect==1){strcat(display,FS.FI.FIStart[i].FileName);strcat(display,"\t\t");}}printf("\n%s\n",display);printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:打开当前目录的文件*输入参数:path路径*输出参数:无*返回值:无*/voidOpen(char*path){chardisplay[100];for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)
if(command[i]=='')
i++;
if(i{strcpy(path,command+i);}break;}if(i<=9){key[i]=command[i];}else{for(unsignedj=i;j{if(command[j]!=''){strcpy(path,command+j);break;}}break;}}strlwr(key);strlwr(path);}/**函数介绍:创建一个文件*输入参数:无*输出参数:无*返回值:无*/voidCreate(char*filename){if(strcmp(filename,"")==0){printf("对不起,文件名不能为空。\n");}else{CreateFileElement(protect,filename,file,NULL,CS.CurrParent);}printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:列出当前目录的文件和文件夹*输入参数:path路径*输出参数:无*返回值:无*/voidDir(char*path){chardisplay[1000];memset(display,'\0',1000);//查找显示内容for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)==0&&FS.FI.FIStart[i].FileLevel==CS.FileLevel&&FS.FI.FIStart[i].effect==1){strcat(display,FS.FI.FIStart[i].FileName);strcat(display,"\t\t");}}printf("\n%s\n",display);printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:打开当前目录的文件*输入参数:path路径*输出参数:无*返回值:无*/voidOpen(char*path){chardisplay[100];for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)
strcpy(path,command+i);
if(i<=9)
key[i]=command[i];
for(unsignedj=i;j{if(command[j]!=''){strcpy(path,command+j);break;}}break;}}strlwr(key);strlwr(path);}/**函数介绍:创建一个文件*输入参数:无*输出参数:无*返回值:无*/voidCreate(char*filename){if(strcmp(filename,"")==0){printf("对不起,文件名不能为空。\n");}else{CreateFileElement(protect,filename,file,NULL,CS.CurrParent);}printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:列出当前目录的文件和文件夹*输入参数:path路径*输出参数:无*返回值:无*/voidDir(char*path){chardisplay[1000];memset(display,'\0',1000);//查找显示内容for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)==0&&FS.FI.FIStart[i].FileLevel==CS.FileLevel&&FS.FI.FIStart[i].effect==1){strcat(display,FS.FI.FIStart[i].FileName);strcat(display,"\t\t");}}printf("\n%s\n",display);printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:打开当前目录的文件*输入参数:path路径*输出参数:无*返回值:无*/voidOpen(char*path){chardisplay[100];for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)
if(command[j]!
='')
strcpy(path,command+j);
strlwr(key);
strlwr(path);
创建一个文件
voidCreate(char*filename)
if(strcmp(filename,"")==0)
printf("对不起,文件名不能为空。
CreateFileElement(protect,filename,file,NULL,CS.CurrParent);
printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);
列出当前目录的文件和文件夹
path路径
voidDir(char*path)
chardisplay[1000];
memset(display,'\0',1000);
//查找显示内容
for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)==0&&FS.FI.FIStart[i].FileLevel==CS.FileLevel&&FS.FI.FIStart[i].effect==1){strcat(display,FS.FI.FIStart[i].FileName);strcat(display,"\t\t");}}printf("\n%s\n",display);printf("%s%s\n$",CS.CurrentUser.UserName,CS.CurrentPath);}/**函数介绍:打开当前目录的文件*输入参数:path路径*输出参数:无*返回值:无*/voidOpen(char*path){chardisplay[100];for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)
if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)==0&&
FS.FI.FIStart[i].FileLevel==CS.FileLevel&&FS.FI.FIStart[i].effect==1)
strcat(display,FS.FI.FIStart[i].FileName);
strcat(display,"\t\t");
printf("\n%s\n",display);
打开当前目录的文件
voidOpen(char*path)
chardisplay[100];
for(unsignedi=0;i{if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)
if(strcmp(FS.FI.FIStart[i].ParentName,CS.CurrParent->FileName)
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2