主要是模仿了linux的风格
没有处理close的问题…因为不知道怎么写
修改了原来的诸多错误 增加了一点点功能
看起来像是可以了(
//cyc
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
/*
空を眺めると
そこには僕が居て
*/
const int maxn = 1e5+5;
map<string,int> usr_privilege;
map<string,string> usr_password;
string password,username,current_location;
int privilege=-2;
int step_num=1;
map<string,int> file_location_phi;
vector<string> file_tree[maxn];
map<int,bool> isdir;
map<int,bool> isfile;
map<int,int> privilege_required;
map<int,string> file_contain;
map<int,int> forbid;
bool login_module()
{
cout<<"enter your username:";
cin>>username;
if(!usr_password.count(username))
{
cout<<"the user doesn't exist"<<endl;
return 0;
}
cout<<"enter your password:";
cin>>password;
if(password!=usr_password[username])
{
cout<<"wrong password"<<endl;
return 0;
}
return 1;
}
//create、open、close、read/write
//privilege -2 not login
//privilege 0 user read only
//privilege 1 user read and write
//users can not visit other users' path
//
//privilege 2 user visit protected files/paths
//privilege 3 admin login
//admin can set one path/file protected.
int main()
{
file_location_phi["/root"]=0;
isdir[0]=1;
file_tree[0].push_back("admin");
file_location_phi["/root/admin"] = 1;
isdir[1]=1;
//cout<<file_location_phi.count("/root/admin")<<endl;
usr_password["admin"]="12345";
usr_privilege["admin"]=3;
JUMP1:
while(true)
{
privilege=-2;
if(login_module())
{
privilege=usr_privilege[username];
current_location="/root/"+username;
break;
}
}
privilege=usr_privilege[username];
string cmd;
while(true)
{
cout<<username<<"@"<<current_location<<"#";
int current_num=file_location_phi[current_location];
cin>>cmd;
if(cmd=="pwd"){
cout<<current_location<<endl;
}
else if(cmd == "exit")
{
goto JUMP1;
}
if(cmd=="ls")
{
int phi_location=file_location_phi[current_location];
for(string it:file_tree[phi_location])
{
string Scheck=current_location+"/"+it;
int Icheck=file_location_phi[Scheck];
if(!forbid[Icheck])cout<<it<<" ";
}
cout<<endl;
}
else if(cmd=="cd") //need to add different users
{
string to_location;
cin>>to_location;
if(to_location[0]!='/')
{
to_location=current_location+"/"+to_location;
}
string abs_loc,user;
abs_loc=to_location;
int len=abs_loc.size();
int pos1=-1;
int pos2=len;
for(int i=1; i<len; i++)
{
if(abs_loc[i]=='/')
{
if(pos1==-1)pos1=i;
else if(pos2==len)pos2=i;
else break;
}
}
len=pos2-pos1-1;
user=abs_loc.substr(pos1+1,len);
if(user!=username&&privilege<3)
{
cout<<"Not privileged to visit!"<<endl;
continue;
}
if(!file_location_phi.count(to_location))
{
cout<<"No such file or directory"<<endl;
}
else
{
int phi_location;
phi_location=file_location_phi[to_location];
if(privilege<privilege_required[phi_location])
{
cout<<"Not privileged!"<<endl;
}
else if(isdir[phi_location])
{
current_location=to_location;
}
else
{
cout<<to_location<<" Not a directory"<<endl;
}
}
}
else if(cmd=="touch")
{
if(privilege<1)
{
cout<<"Not privileged!"<<endl;
continue;
}
string new_filename;
cin>>new_filename;
string abs_filename=current_location+"/"+new_filename;
if(file_location_phi.count(abs_filename))
{
int Icheck=file_location_phi[abs_filename];
if(isfile[Icheck]){
cout<<"file already exists"<<endl;
continue;
}
else{
isfile[Icheck]=1;
forbid[Icheck]=0;
continue;
}
}
step_num++;
isdir[step_num]=0;
isfile[step_num]=1;
file_tree[current_num].push_back(new_filename);
file_location_phi[abs_filename]=step_num;
}
else if(cmd=="mkdir")
{
if(privilege<1)
{
cout<<"Not privileged!"<<endl;
continue;
}
string new_pathname;
cin>>new_pathname;
string abs_pathname=current_location+"/"+new_pathname;
if(file_location_phi.count(abs_pathname))
{
int Icheck=file_location_phi[abs_pathname];
if(isdir[Icheck]){
cout<<"path already exists"<<endl;
continue;}
else{
isdir[Icheck]=1;
forbid[Icheck]=0;
continue;
}
}
step_num++;
isdir[step_num]=1;
isfile[step_num]=0;
file_tree[current_num].push_back(new_pathname);
file_location_phi[abs_pathname]=step_num;
}
else if(cmd=="read")
{
string filename;
cin>>filename;
if(filename[0]!='/')
{
filename=current_location+"/"+filename;
}
int phi_location;
phi_location=file_location_phi[filename];
if(!file_location_phi.count(filename))
{
cout<<"No such file or directory"<<endl;
}
else if(privilege<privilege_required[phi_location])
{
cout<<"Not privileged to read"<<endl;
}
else
{
if(isfile[phi_location])cout<<file_contain[phi_location]<<endl;
else cout<<"Not a file"<<endl;
}
}
else if(cmd=="write")
{
string filename;
cin>>filename;
if(filename[0]!='/')
{
filename=current_location+"/"+filename;
}
int phi_location;
phi_location=file_location_phi[filename];
if(!file_location_phi.count(filename))
{
cout<<"No such file or directory"<<endl;
}
else if(privilege<privilege_required[phi_location])
{
cout<<"Not privileged to write"<<endl;
}
else
{
if(isfile[phi_location]){
cout<<file_contain[phi_location]<<endl;
string overwrite;
cin>>overwrite;
file_contain[phi_location]=overwrite;
}
else cout<<"Not a file"<<endl;
}
}
//only for the admin who can reset the privilege
else if(cmd=="protect")
{
string filepath;
cin>>filepath;
if(filepath[0]!='/')filepath=current_location+"/"+filepath;
if(!file_location_phi.count(filepath))
{
cout<<"path doesn't exist"<<endl;
}
else if(privilege<3)
{
cout<<"not privileged"<<endl;
}
else
{
int phi_location;
phi_location=file_location_phi[filepath];
privilege_required[phi_location]=2;
}
}
else if(cmd=="add_user")
{
string name,psw;
int privi;
cin>>name>>psw>>privi;
usr_privilege[name]=privi;
usr_password[name]=psw;
step_num++;
string now_location="/root/"+name;
file_tree[0].push_back(name);
file_location_phi[now_location] = step_num;
isdir[step_num]=1;
isfile[step_num]=0;
cout<<name<<" "<<privi<<endl;
}
else if(cmd=="rm")
{
string to_location;
cin>>to_location;
if(to_location[0]!='/')
{
to_location=current_location+"/"+to_location;
}
string abs_loc,user;
abs_loc=to_location;
int len=abs_loc.size();
int pos1=-1;
int pos2=len;
for(int i=1; i<len; i++)
{
if(abs_loc[i]=='/')
{
if(pos1==-1)pos1=i;
else if(pos2==len)pos2=i;
else break;
}
}
len=pos2-pos1-1;
user=abs_loc.substr(pos1+1,len);
if(user!=username&&privilege<3)
{
cout<<"Not privileged!"<<endl;
continue;
}
if(!file_location_phi.count(to_location))
{
cout<<"No such file"<<endl;
}
else
{
int phi_location;
phi_location=file_location_phi[to_location];
if(privilege<privilege_required[phi_location])
{
cout<<"Not privileged!"<<endl;
}
else
{
isfile[phi_location]=0;
isdir[phi_location]=0;
forbid[phi_location]=1;
}
}
}
else
{
cout<<cmd<<" is not a command"<<endl;
}
}
}