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

📄 shell.cpp

📁 这是一个linux下的Shell.有命令历史和命令提示
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	char* str;	drip = opendir(path);	if(drip)	{  		while ((dp = readdir (drip)) != 0)    		{			if (fnmatch (p, dp->d_name, 0) == 0 &&( dp->d_type == type || type == ALL))			{				if(dp->d_type == 4)					str = new char[strlen(path) + strlen(dp->d_name) + 2];				else					str = new char[strlen(path) + strlen(dp->d_name) + 1];				strcpy(str,path);				strcat(str,dp->d_name);				if(dp->d_type == 4)					strcat(str,"/");		 						dirproc(str);					o->inset(str);			}		}		closedir(drip);		return o;	}	return 0;}/*********************************************************************/	/*********************************************************************/char* Shell::_findfirst(List<char> path,char* p_subdir,int type){	path.reset();	char* temp;	char* o;	while((temp = path.getCur()) != 0)	{		o = _findfirst(temp,p_subdir,type);		if(o)		{			dirproc(o);				return o;		}	}	return 0;}/*********************************************************************//*********************************************************************/char* Shell::_findfirst(char* path,char* p_subdir,int type){	if(!path|| !p_subdir)		return (char*)0;	if(find(p_subdir,'/') == -1)		return findfirst(path,p_subdir,type); 		int bork_p_subdir = find(p_subdir,'/');	char* s = new char[bork_p_subdir + 1];	strncpy(s,p_subdir,bork_p_subdir);	s[bork_p_subdir] = 0;		List<char>* d = findall(path,s,DIR_);	if(!d)	{		delete s;		return (char*)0;	}	else	{		d->reset();		char* temp;		while((temp = d->getCur()) != 0)		{			char* str = _findfirst(temp,p_subdir + bork_p_subdir + 1,type);			if(str)				return str;		}		return 0;			}}/*********************************************************************//*********************************************************************/List<char>* Shell::_findall(char* path,char*p_subdir,int type){	List<char>* o = new List<char>;	_findall(path,p_subdir,o,type);	return o;}/*********************************************************************//*********************************************************************/List<char>* Shell::_findall(char* path,char*p_subdir,List<char>* o,int type){	if(!path|| !p_subdir)		return 0;	if(find(p_subdir,'/') == -1)	{		List<char>* temp =  findall(path,p_subdir,type);		o->inset(temp);			return temp;	}	int bork_p_subdir = find(p_subdir,'/');	char* s = new char[bork_p_subdir + 1];	strncpy(s,p_subdir,bork_p_subdir);	s[bork_p_subdir] = 0;		List<char>* d = findall(path,s,DIR_);	if(!d)		delete s;	else	{		d->reset();		char* temp;		while((temp = d->getCur()) != 0)			_findall(temp,p_subdir + bork_p_subdir + 1,o,type);			d->clear();		delete d;	}	return 0;}/*********************************************************************//*********************************************************************/List<char>* Shell::_findall(List<char>path,char* p_subdir,int type){	List<char>* o = new List<char>;	List<char>* work;	path.reset();	char* temp;	while((temp = path.getCur())!= 0)	{		work = _findall(temp,p_subdir,type);		o->inset(work);	}	return o;}/*********************************************************************//*********************************************************************/List<char>* Shell::findAll(char* s,int type, bool curFlag){if(!s)		return 0;	List<char>* o = new List<char>;	if(s[0] == '/')	{		if(s[1] == 0)		{			o->inset("/");			return o;		}		else		{			if(type == DIR_)			{				int len = strlen(s);				if(s[len-1] == '/')						s[len-1] = 0;			}			return _findall("/",s + 1,type);		}	}	else 	{		if(type == DIR_)		{			int len = strlen(s);			if(s[len-1]== '/')					s[len-1] = 0;		}		if((s[0] == '~' && s[1] == '/'))			return _findall(path_userhome,s+2,type);		else  if(curFlag)		{					List<char>* o = _findall(path_cur,s,type);			if(!o)				return 0;			else			{				char** buf = o->getbuf();				char** temp = buf;				List<char>* r = new List<char>;				int len_cur = (int)strlen(path_cur);				while(*temp)				{					if((int)strlen(*temp) == len_cur)					{						if(!(s[0] = '*' && s[1] == 0))							r->inset(".", -1);					}						else if((int)strlen(*temp) < len_cur)					{						if(!(s[0] = '*' && s[1] == 0))										r->inset("..", -1);					}					else 						r->inset(*temp+len_cur,-1);					temp++;				}				free__(buf);				delete o;				return r;			}							}		else			return 0;	}}/*********************************************************************//*********************************************************************/char* Shell::findFirst(char* s,int type){	if(!s)		return 0;	if(s[0] == '/')	{		if(s[1] == 0)		{			char* a = new char[2];			a[0] = '/';			a[1] = 0;			return a;		}		else		{			if(type == DIR_)			{				int len = strlen(s);				if(s[len-1]== '/')						s[len-1] = 0;			}			return _findfirst("/",s + 1,type);		}	}	else 	{		if(type == DIR_)		{			int len = strlen(s);			if(s[len-1]== '/')					s[len-1] = 0;		}				if(s[0] == '~')		{			if(s[1] == 0)			{				char* a = new char[strlen(path_userhome)+1];				strcpy(a,path_userhome);				return a;			}			else if(s[1] == '/')				return _findfirst(path_userhome,s + 2,type);		}		return _findfirst(path_cur,s,type);			}}/*********************************************************************//*********************************************************************/char* Shell::findFirstExecFile(char* s){	if(!s)		return 0;	List<char>* o;	o = findAll(s,NORMALFILE, false);	if(o)	{		o->reset();		char* work;		while((work = o->getCur())!=0)		{			if(access(work,X_OK) == 0)			{					o->clear();				delete o;				return work;			}		}	}	List<char>* o1;	o1 = _findall(PATH,s,NORMALFILE);	if(o1)	{		o1->reset();		char* work;		while((work = o1->getCur())!=0)		{			if(access(work,X_OK) == 0)			{				o1->clear();				delete o1;					return work;			}		}	}	List<char>* o2;	o2 = _findall(path_cur, s, NORMALFILE);	if(o2)	{		o2->reset();		char* work;		while((work = o2->getCur())!=0)		{			if(access(work,X_OK) == 0)			{				o2->clear();				delete o2;					return work;			}		}	}		//no executeable file.	//find the first matchable file by the same setuence	if(o)	{		o->reset();		char* work = o->getCur();		o1->clear();		delete o;		return work;	}	if(o1)	{		o1->reset();		char* work = o1->getCur();		o1->clear();		delete o1;		return work;	}	else if(o2)	{		o2->reset();		char* work = o2->getCur();		o2->clear();		delete o2;		return work;	}	return 0;}/*********************************************************************//*********************************************************************/List<char>* Shell::findAllExecFile(char* s){	if(!s)		return 0;	List<char>* o;	List<char>* r = new List<char>;	o = findAll(s,NORMALFILE, false);	if(o)	{		o->reset();		char* work;		while((work = o->getCur())!=0)		{			if(access(work,X_OK) == 0)			{					r->inset(work);				delete work;			}		}		delete o;	}	o = _findall(PATH,s,NORMALFILE);	if(o)	{		o->reset();		char* work;		while((work = o->getCur())!=0)		{			if(access(work,X_OK) == 0)			{				r->inset(work);				delete work;			}		}		delete o;		}	o = _findall(path_cur, s, NORMALFILE);	if(o)	{		o->reset();		char* work;		while((work = o->getCur())!=0)		{			if(access(work,X_OK) == 0)			{				r->inset(work);				delete work;			}		}		delete o;	}	return r;}/*********************************************************************/void Shell::_cd(char* p){	if(access(p,R_OK)==0)	{		if(0 == chdir(p))		{			delete path_cur;			path_cur = new char[strlen(p)+1];			strcpy(path_cur,p);			int i;			for(i = strlen(path_cur)-2; i>=0 && path_cur[i] != '/'; i--){};			if(path_cur[0] == '/' && path_cur[1] == 0)			{				path_cur_last[0] = path_cur_last[1] = '/';				path_cur_last[2] = 0; 			}			else				strcpy(path_cur_last,path_cur+i+1);		}		else			cout<<"lover: cd: "<<p<<": can't enter the diretory"<<endl;	}	else		cout<<"lover: cd: no the competence"<<endl;}void Shell::_pwd(void){	puts(path_cur);}void Shell::_his(int num){	commandbuf.setSize(num);}void Shell::setPath (int num, char **pp) {	PATH.clear();	PATH.inset(pp);}void Shell::addPath(char* path){	PATH.inset(path);}void Shell::subPath(char* path){	PATH.del(path);}void Shell::show(void){	write(1,"[",1);	write(1,name_user,strlen(name_user));	write(1,"@localhost ",strlen("@localhost "));	write(1,path_cur_last,strlen(path_cur_last)-1);	write(1,"]",1);	write(1,&tipchar,1);	write(1," ",1);}void Shell::fresh(void){	delete command;}void Shell::exit__(void){	char* work = new char[strlen("/bin/lover/")+strlen(name_user) + 1];	strcpy(work,"/bin/lover/");	strcat(work,name_user);	int fid = open(work,O_WRONLY|O_TRUNC|O_CREAT);					//save the PATH in the file.	if(fid >= 0)	{		write(fid,&tipchar,1);		write(fid,"\n",1);		PATH.show(fid);		close(fid);	}	delete work;	work = new char[strlen("/bin/lover/") + strlen(name_user) + strlen("commandbuf") + 1];	strcpy(work, "/bin/lover/");	strcat(work, name_user);	strcat(work, "commandbuf");	fid = open(work,O_WRONLY|O_TRUNC|O_CREAT);					//save the commandbuf in the file.	if(fid)		commandbuf.show(fid, true);						//revers save for read simple.	struct termios* a = new struct termios[sizeof(struct termios)];	ioctl(0,TCGETS,a);	a->c_lflag |= ECHO;	ioctl(0,TCSETS,a);	delete a;}void Shell::idle (void) {} void Shell::recoverIOenv(void){	ioctl(0,TCSETS,save);}

⌨️ 快捷键说明

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