⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 本科作业虚拟linux操作系统下的文教系统
💻 CPP
字号:


#include "DiNode.h"

#include "HDblock.h"
#include "Nfile.h"
#include "User.h"

User user1("root","123456",1);
User user2("shuidao","123456",2);

User* pcurUser=&user1;//debug

#include "HardDevice.h"

#include "conio.h"

#include <iostream>
#include <string>
using namespace std;

void Exception(string msg);





////////////////////////////////////=====环境变量定义

string COMPUTER="Minix";
string PWD="/";//根目录

char LOGTAG='$';//提示符

HardDevice hd0;//虚拟硬盘

Index_File *curPath;






///////////////////////////////////======子函数声明

int login(string username,string passname);

void shell(string cmd);

void help();


int create(char**vec);
int open(char**vec);
int read(char**vec);
int write(char**vec);
int close(char**vec);
int deletefile(char**vec);
int mkdir(char**vec);
int cd(char**vec);
int dir(char**vec);
int pwd(char**vec);
int logout();





//============================================








void main()
{
	
	while(1)
	{
		system("cls");
		cout<<"\tHello ! Welcome to use minix ! \n\t\t\t\t-----powered by panglong"
		<<"\n\tNote: Whenever enter 'help' to get help."
		<<endl<<"\t\tFor testing  you can use 'root/123456' to login  as root."<<endl;
		
		//string pwd="/";//根目录
		int log=0;//未登录
		string username;
		string password;

		while(!log)
		{
			cout<<"login:";
			cin>>username;
			if(strcmp(username.c_str(),"exit")==0)
			{
				//do something
				exit(0);
			}
			//cout<<endl;
			cout<<"password:";
			cin>>password;
			if(login(username,password))
			{
				system("cls");
				cout<<"\tHello ! Welcome to use minix ! \n\t\t\t\t-----powered by panglong"
				<<"\n\tNote: Whenever enter 'help' to get help."<<endl;
				cout<<"Login successful. Welcome "<<username<<" !"<<endl;
				break;
			}
			else
			{
				cout<<"Not login :username or password wrong . \nplease try again or enter 'exit' for username to leave. "<<endl;
			}
		}


		curPath=hd0.getRoot();
     	string cmd1; 
		getline(cin,cmd1);

		while(true)
		{
			cout<<username<<"@"<<COMPUTER<<":"<<PWD<<LOGTAG<<":";

			string cmd;
			getline(cin,cmd) ;

			if(strcmp(cmd.c_str(),"exit")==0)
			{
				//do somthing 
				//exit(0);
				break; 
			}
			if(strcmp(cmd.c_str(),"logout")==0)
			{
				logout();

				break;
			}
			if(strcmp(cmd.c_str(),"help")==0)
			{
				help();
				getline(cin,cmd);
				continue;
			}

			shell(cmd);
		}


	}

}


///////////////=============================================


int login(string username,string passname)
{
	//debug
	if(username==user1.getName()&&passname==user1.getPass())
	{
		pcurUser=&user1;
		LOGTAG='#';
		return 1;

	}
	else if(username==user2.getName()&&passname==user2.getPass())
	{
		LOGTAG='$';
		pcurUser=&user2;
		return 1;
	}

	else return 0;
}

void help()
{
	cout<<"Some help information here ."<<endl;
	cout<<"You can use commands bellew:"<<endl
		<<"=='cd' "<<endl
		<<"=='dir'or 'ls'"<<endl
		<<"=='mkdir dirname'"<<endl
		<<"=='pwd'"<<endl
		<<"=='create  filename'"<<endl
		<<"=='close  filename'"<<endl
		<<"=='open  filename'"<<endl
		<<"=='write  filename  string'"<<endl
		<<"=='read  filename'"<<endl
		<<"=='delete  filename'"<<endl
		<<"=='logout'"<<endl
		<<"=='exit'"<<endl



		<<"Press any keys to to return ! "<<endl;
	getch();



}






void shell(string cmd)
{
	static char* vec[20];
	char cmdline[255];
	strncpy(cmdline,cmd.c_str(),cmd.length() );
	cmdline[cmd.length()]='\0';
	vec[0]=strtok(cmdline," ,\t\n");

	if(vec[0]==NULL)
	{
		return;
	}

	int i=0;
	while(i++<20)
	{
		vec[i]=strtok(NULL," ,\t\n");
		if(vec[i]==NULL)
		{
			break;
		}
	}

	if(strcmp(vec[0],"create")==0)//create
	{
		create(vec);
		return;
	}

	if(strcmp(vec[0],"open")==0)//open
	{
		open(vec);
		return;
	}

	if(strcmp(vec[0],"read")==0)//read
	{
		read(vec);
		return;
	}

	if(strcmp(vec[0],"write")==0)//write
	{
		write(vec);
		return;
	}

	if(strcmp(vec[0],"close")==0)//close
	{
		close(vec);
		return;
	}

	if(strcmp(vec[0],"delete")==0)//delete
	{
		deletefile(vec);
		return;
	}
	if(strcmp(vec[0],"mkdir")==0)//mkdir
	{
		mkdir(vec);
		return;
	}

	if(strcmp(vec[0],"cd")==0)//cd
	{
		cd(vec);
		return;
	}

	if(strcmp(vec[0],"ls")==0)//ls
	{
		dir(vec);
		return;
	}

	if(strcmp(vec[0],"dir")==0)//dir
	{
		dir(vec);
		return;
	}

	if(strcmp(vec[0],"pwd")==0)//pwd
	{
		pwd(vec);
		return;
	}

	//string str="Command Not Found Exception !";
	Exception("Command Not Found Exception : "+cmd);

}


void Exception(string msg)
{
	cout<<msg<<endl;
}


int create(char**vec)
{
	if(vec[1]!=0&&vec[2]==0)
	{
			//debug

		//HDblock*p=hd0.getFreeBlock();


		curPath->addFile(
			new Nfile(vec[1],new DiNode(COMMONFILE,hd0.getFreeBlock(),pcurUser->getUid())
			,curPath)) ;



		cout<<curPath->getByName(vec[1])->getName()
			<<" :create successful ."<<endl;

	
		return 1;

	}
	else
	{
		string str(vec[0]);
		Exception("wrong paremers for "+str);
		return 0;
	}
}


int open(char **vec)
{
	if(pcurUser->Isexist(vec[1]) )
	{
		Exception("The file is already open!");
		return 0;
	}

	if(Nfile*fp=curPath->getByName(vec[1]))//打开成功
	{
		pcurUser->addFile(fp);
		Exception("Open successful !");
		return 1;
	}
	else
	{
		Exception("File not found !");
	}


	
	return 0;
}

int read(char**vec)
{
	if(Nfile*fp=pcurUser->getFileByName(vec[1]))//打开成功
	{
		cout<<fp->read()<<endl;
		return 1;
	}
	else
	{
		Exception("file not open yet !");
	}
	return 0;
}

int write(char**vec)//write  filename content ......
{
	if(Nfile*p=pcurUser->getFileByName(vec[1]))
	{
		if(p->getType()==INDEXFILE)
		{
			Exception("can not write to indexfile !");
			return 0;
		}
		p->write(vec[2]);
		return 1;
	}
	else
	{
		Exception("file not open yet !");
	}
	return 0;
}

int close(char**vec)
{
	if(Nfile*p=pcurUser->getFileByName(vec[1]))
	{
		//p->write(vec[2]);
		pcurUser->closeFileByName(p->getName());
		std::cout<<"close successful !"<<std::endl;
		return 1;
	}
	else
	{
		Exception("file not open yet ! or you may have closed it before ! ");
	}
	return 0;
}

int deletefile(char**vec)
{

	if(vec[1]==NULL||vec[2]!=NULL)
	{
		Exception("this command should take 1 parameter !");
		return 0;
	}


	Nfile*ptem=NULL;
	if(ptem=pcurUser->getFileByName(vec[1]))
	{
		if(pcurUser->getName()=="root"||pcurUser->getUid()==ptem->getUid())
		{
			pcurUser->closeFileByName(ptem->getName());

			ptem->getParent()->removeFile(ptem);
			return 1;
		}
		else
		{
			Exception(" you do not have permits to delete this file !");
			return 0;
		}
	}
	else
	{
		Exception("it seems that you have not open this file yet !");
		return 0;
	}

	return 0;
}

int mkdir(char**vec)
{
	if(vec[1]!=0&&vec[2]==0)
	{
		curPath->addFile(new Index_File (vec[1],hd0.getFreeBlock(),pcurUser->getUid(),curPath)) ;
	
		return 1;
	}

	return 0;
}

int cd(char**vec)
{
	Index_File*ptem=NULL;
	if(ptem=curPath->cd(vec[1]))
	{

		if(ptem==curPath->getParent())
		{
			PWD=PWD.substr(0,PWD.find_last_of('/'));
		}
		else if(ptem->getParent()==curPath)
		{
			PWD.append("/");
			PWD.append(ptem->getName());
		}




		curPath=ptem;
		return 1;
	}

	Exception("can not cd to the path; the path may not exists !");
	return 0;
}

int dir(char**vec)
{
	if(vec[1]==0)//无参数
	{
		curPath->listAllFile();
		return 1;
	}
/*	if(vec[2]==0)//一个参数
	{
		if(vec[1][0]!='-'&&!curPath->getByName(vec[1]))
		{
			Exception("File not found !");
			return 0;
		}
		curPath->listAllFile(vec[1]);
		return 1;
	}
	*/
	Exception("Sorry Not surported parameters!");//debug
	return 0;
}



int logout()
{
	pcurUser->closeAllFiles();
	
	return 0;
}


int pwd(char**vec)
{
	if(vec[1]!=NULL)
	{
		Exception("pwd is not surport parameters");
		return 0;
	}

	std::cout<<PWD<<std::endl;
	return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -