📄 unix.cpp
字号:
typedef struct sys
{
int i;
char dect[20];
char memo[20];
char name[10];
struct sys *next;
}sys;
typedef struct User
{
char name[10];
char password[10];
}User;
typedef struct Files
{
int num;
char name[10];
int p;
int length;
char mode[20];
char fadect[20];
struct Files *next;
}Files;
#include <string.h>
#include <iostream.h>
#include <iomanip.h>
//系统初始提供的两个用户名和密码
User defaultUser[2] =
{
"user1","123",
"user2","456"
};
//全局变量
User *user;
Files *file;
sys *myfile;
int nUserNo=0;
int no;
int fnode=0;
char current[20]; //当前目录
char predect[20]; //上级目录
char nowdir[200]; //当前绝对路径
int used;
//验证用户名和密码
bool checkUser(char* username, char* password)
{
int i;
for(i=0; i<8; i++)
{
if( (strcmp(username,defaultUser[i].name)==0)
&& (strcmp(password,defaultUser[i].password)==0)
)
return true;
}
return false;
}
//创建文件
sys* createFile()
{
int L=0;
sys *ps1;
sys *pend1;
ps1=new sys;
ps1->i=0;
strcpy(ps1->name,"root");
myfile=NULL;
pend1=ps1;
while(L!=1)
{
L=L+1;
if (myfile==NULL)
myfile=ps1;
else
pend1->next =ps1;
pend1=ps1;
ps1=new sys;
ps1->i =1;
strcpy(ps1->name ,"lin");
}
pend1->next=NULL;
delete ps1;
return(myfile);
}
//查看帮助信息
void help(void)
{
cout<<"********************************帮助信息**********************************"<< endl;
cout<<"dir--显示文件和目录"<<endl;
cout<<"create--创建新文件和编辑文件"<<endl;
cout<<"cd--改变当前目录"<<endl;
cout<<"mkdir--创建新目录"<<endl;
cout<<"open--打开显示文件"<<endl;
cout<<"delete--删除文件"<<endl;
cout<<"logout--退出系统"<<endl;
cout<< "__"<<endl;
}
//获取文件长度
long filelength=0;
void getlen(sys *myfile,char filename[30])
{
sys *dect=myfile;
filelength=0;
while(myfile)
{
if ((strcmp(myfile->name,filename)==0)&&(myfile->i>1)&&(strcmp(myfile->dect,current)==0))
{
filelength=filelength+strlen(myfile->memo);
}
myfile=myfile->next ;
}
}
//创建新文件------------------------------------
Files* create()
{
int n=0;
Files *ps; //临时节点
Files *pend; //最后一个i节点
ps=new Files;
ps->num=0;
strcpy(ps->name,"..");
file=NULL;
pend=ps;
while(n!=1)
{
n=n+1;
if (file==NULL)
file=ps;
else
pend->next =ps;
pend=ps;
ps=new Files;
ps->num =1;
strcpy(ps->name ,"..");
}
pend->next=NULL;
delete ps;
return(file);
}
//读文件------------------------------------
void read(sys *myfile,char filename[30])
{
sys *dect=myfile;
filelength=0;
while(myfile)
{
if ((strcmp(myfile->name,filename)==0)&&(myfile->i>1)&&(strcmp(myfile->dect,current)==0))
{
cout<<myfile->memo<<endl;
filelength=filelength+strlen(myfile->memo);
}
myfile=myfile->next ;
}
cout<<endl<<endl;
}
//写文件------------------------------------
void write(struct sys *myfile,struct sys *wrfile)
{
if (myfile==NULL)
{
myfile=wrfile;
wrfile->next=NULL;
return;
}
if (myfile->i >wrfile->i )
{
wrfile->next =myfile;
myfile=wrfile;
return;
}
struct sys *p=myfile;
while(p->next)
{
p=p->next ;
}
p->next=wrfile;
wrfile->next =NULL;
}
//查找文件------------------------------------
int searchFile(sys *myfile,char filename[30])
{
while(myfile)
{
if (strcmp(myfile->name,filename)==0)
{
return 1;
}
myfile=myfile->next ;
}
return 0;
}
//显示===================================================================
void display(Files *file)
{
int fno=0;
int dno=0;
int fcount=0;
Files *dect=file;
cout<<endl;
while(file)
{
if (file->num!=0)
{
if (file->p==0&&strcmp(file->fadect,predect)==0)
{
getlen(myfile,file->name);
cout<<setiosflags(ios::left)<<setw(12)<<file->name<<setiosflags(ios::left)<<setw(20)<<":File"<<endl;
fcount=fcount+filelength;
fno=fno+1;
}
}
file=file->next;
}
while(dect)
{
if ((dect->num!=0)&&(strcmp(dect->fadect,predect)==0))
{
if (dect->p==1)
{
cout<<setiosflags(ios::left)<<setw(12)<<dect->name<<setiosflags(ios::left)<<setw(20)<<":DIR"<<endl;
dno=dno+1;
}
}
dect=dect->next;
}
int free;
free =32256 - used;
cout<<endl;
cout<<setiosflags(ios::left)<<setw(25)<<"File Number:"<<setiosflags(ios::left)<<setw(20)<<fno
<<used<<" bytes"<<endl;
cout<<setiosflags(ios::left)<<setw(25)<<"Directorie Number:"<<setiosflags(ios::left)<<setw(20)<<dno
<<free<<" bytes free"<<endl;
}
//加入文件节点------------------------------------
void insert( Files *file, Files *newfile)
{
if (file==NULL)
{
file=newfile;
newfile->next=NULL;
return;
}
if (file->num > newfile->num )
{
newfile->next =file;
file=newfile;
return;
}
Files *p=file;
while(p->next)
{
p=p->next ;
}
p->next=newfile;
newfile->next =NULL;
}
//删除文件------------------------------------
void deletef(Files *file,char name[20])
{
Files *p;
if(!file)
{
cout<<"ERROR: can not delete a dectory! "<<endl<<endl;return;
}
if (strcmp(file->name,name)==0)
{
if (file->p ==0)
{
p=file;
file=file->next ;
used -= file->length;
cout<<endl;
delete p;
return;
}
}
for(Files *pg=file;pg->next ;pg=pg->next )
{
if (strcmp(pg->next->name ,name)==0)
{
used -= pg->next->length;
if (pg->next->p ==0)
{
p=pg->next ;
pg->next =p->next ;
cout<<endl;
delete p;
return;
}
}
}
cout<<"ERROR: file not found. "<<endl<<endl;
}
//改变目录------------------------------------
void cd(Files *file,char dect[20])
{
while(file)
{
if((strcmp(file->name,dect)==0)&&(file->p==1))
{
strcpy(predect,current);
strcpy(current,dect);
strcat(nowdir, dect);
strcat(nowdir, "/");
return;
}
file=file->next ;
}
}
//入口函数main()
int main()
{
char username[20];
char password[20];
Files *addfile;
//初始界面
cout<<"******************************************************************"<<endl;
cout<<"* 欢迎使用模拟UNIX文件系统 *"<<endl;
cout<<"* *"<<endl;
cout<<"* *"<<endl;
cout<<"* 用户名:user1/user2 密码:123/456 *"<<endl;
cout<<"******************************************************************"<<endl<<endl;
//输入用户名和密码
while(true)
{
cout<<"用户名: ";
cin>>username;
cout<<"密码 : ";
cin>>password;
if(checkUser(username, password))
{
break;
}
else
{
cout<<"用户名或密码错误,请重新输入!"<<endl;
}
}
cout<<endl<<"登陆成功,欢迎使用模拟UNIX文件系统!(使用help命令查看帮助信息)"<<endl<<endl;
file=create();
myfile=createFile();
strcpy(current,"/");
strcpy(predect,"..");
strcpy(nowdir,"/");
used =0;
begin:
char command[10];
cout<<"["<<username<<"]: "<<nowdir<<">";
cin>>command;
//列出文件和目录:dir
if (strcmp(command,"dir")==0)
{
display(file);
goto begin;
}
//打开文件:open
if (strcmp(command,"open")==0)
{
char filename[30];
cin>>filename;
int r=searchFile(myfile,filename);
if(r==1)
{
read(myfile,filename);
}
else
{
cout<<"No such file!"<<endl;
}
goto begin;
}
//查看帮助信息:help
if (strcmp(command,"help")==0)
{
help();
goto begin;
}
//删除文件:delete
if (strcmp(command,"delete")==0)
{
char delname[20];
cout<<"请输入要删除的目录名或者文件名:";
cin>>delname;
deletef(file,delname);
goto begin;
}
//改变目录:cd
char dect[20];
if (strcmp(command,"cd")==0)
{
cin>>dect;
if (strcmp(dect,"/")==0)
{
strcpy(current,"/");
strcpy(predect,"..");
strcpy(nowdir,"/");
goto begin;
}
else
{
cd(file,dect);
goto begin;
}
}
//新建目录:mkdir
if (strcmp(command,"mkdir")==0)
{
no=no+1;
addfile=new Files;
addfile->num =no;
cin>>addfile->name;
addfile->p =1;
addfile->length =strlen(addfile->name);
strcpy(addfile->fadect,predect);
insert(file,addfile);
cout<<"Make Directory Successful!"<<endl;
goto begin;
}
//创建文件:create
if (strcmp(command,"create")==0)
{
fnode=fnode+1;
sys *alterfile;
alterfile=new sys;
alterfile->i =fnode;
cout<<"请输入文件名:";
cin>>alterfile->name ;
int r;
r=searchFile(myfile,alterfile->name);
if (r!=1)
{
no=no+1;
addfile=new Files;
char filen[30];
addfile->num =no;
strcpy(addfile->mode,"rw-r-");
strcpy(addfile->name ,alterfile->name);
strcpy(filen,addfile->name);
addfile->p =0;
addfile->length =0;
strcpy(addfile->fadect,predect);
sys *appfile;
appfile=new sys;
fnode=fnode+1;
appfile->i =fnode;
strcpy(appfile->name,addfile->name);
strcpy(appfile->dect,current);
cout<<"请输入文件大小:";
cin>>appfile->memo ;
write(myfile,appfile);
addfile->length =strlen(appfile->memo);
used += addfile->length;
insert(file,addfile);
goto begin;
}
cin>>alterfile->memo ;
used += strlen(alterfile->memo);
strcpy(alterfile->dect,current);
write(myfile,alterfile);
cout<<"File Successful!"<<endl;
goto begin;
}
//退出系统:logout
if (strcmp(command,"logout")==0)
{
return;
}
cout<<"找不到这个命令,请输入help查看帮助信息!"<<endl<<endl;
goto begin;
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -