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

📄 os.cpp

📁 操作系统接口:兼容Unix 和Linux 命令接口。 为Windows 操作系统建立一个兼容Unix 命令的命令接口; 实现命令包括ls,cat,cp,rm,mv,ren,mkdir,rmdi
💻 CPP
字号:
#include "windows.h"
#include "iostream"
#include "conio.h"
#include "string"
#include "iomanip"
#include "fstream"
using namespace std;

#define PRINTHELP		"ls        显示一个目录中的文件和子目录。\n"\
						"cd        显示当前目录的名称或将其更改。\n"\
						"cat       打开文件并显示内容\n"\
						"mkdir     创建目录。\n"\
						"mv        将文件从一个目录移到另一个目录。\n"\
						"cp        复制文件和目录树。\n"\
						"ren       重命名文件。\n"\
						"exit      退出命令解释程序。\n"\
						"help      提供帮助信息。\n"\
						"rm        删除至少一个文件。\n"\
						"rmdir     删除空目录。\n"\
						"命令 +? 显示命令帮助\n"
#define PRINTCATHELP "打开文件并显示内容\n"\
						"cat [drive:][path]dirname1"\

#define	PRINTMVHELP	"将文件从一个目录移到另一个目录。\n"\
						"移动:mv	[drive:][path]dirname1 destination\n"\
						"重命名:mv	[drive:][path]dirname1 dirname2\n"\
						

#define PRINTCPHELP	"复制文件和目录树。\n"\
						"cp	source	[destination]\n"\
						"  source       指定要复制的文件\n"\
						"  destination  指定新文件的位置或名称。\n"\
						

#define PRINTLSHELP	"显示目录中的文件和子目录列表。\n"\
						"ls [drive:][path][filename] [+?]\n"\
						"    [drive:][path][filename]\n"\
						"       显示要列出的驱动器、目录或文件。\n"\
					

#define PRINTCDHELP "显示当前目录名或改变当前目录。\n"\
						"cd	[drive:][path] [+?]\n"\
						"cd	[..]\n"\
						"	..      指定要改成父目录。\n"\
					
#define PRINTMKDIRHELP "创建目录。\n"\
						"mkdir [drive:][path]\n"\
						

#define PRINTRMDIRHELP "删除目录。\n"\
						

#define PRINTRMHELP "删除文件。\n"\
						"rm [drive:][path][filename]\n"\
						
#define PRINTRENHELP "重命名文件。\n\n"\
						"ren  [drive:][path]filename1 filename2.\n\n"\
						"不能为目标文件指定新的驱动器或路径。\n"\
					


void ls(string *lsinput,int lscount)
{
	if(lscount == 1)
	{
		HANDLE	handle;
		char fc[50];
		FILETIME lpCreationTime,lpLastAccessTime,lpLastWriteTime;
		SYSTEMTIME stime;
		WIN32_FIND_DATA* fd = new WIN32_FIND_DATA();

		if(!::GetCurrentDirectory(50,fc))
			cout<<"路径可能不存在!"<<endl;
		strcat(fc,"\\*.*");
		if((handle = FindFirstFile(fc, fd)) != INVALID_HANDLE_VALUE){
			do{
				HANDLE hDir = CreateFile(fd->cFileName,GENERIC_READ,FILE_SHARE_READ | FILE_SHARE_DELETE,NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,  NULL);  
				if(GetFileTime(hDir,  &lpCreationTime,  &lpLastAccessTime,  &lpLastWriteTime)){  
					FILETIME  ftime;  
					FileTimeToLocalFileTime(&lpLastWriteTime,  &ftime);  //转换成本地时间  
					FileTimeToSystemTime(&ftime,  &stime);  //转换成系统时间格式  
				}
				cout<<setfill('0');
				cout<<stime.wYear<<"-"<<setw(2)<<stime.wMonth<<"-"<<setw(2)<<stime.wDay<<"  "<<setw(2)<<stime.wMinute<<":"<<setw(2)<<stime.wMilliseconds;
				cout<<"  "<<fd->cFileName<<endl;
				CloseHandle(hDir);
				}while (FindNextFile(handle, fd));
		}
		FindClose(handle);
	}else if(lscount == 2){
		if(lsinput[1] == "?" )
			cout<<PRINTLSHELP<<endl;
		else{
			HANDLE	handle;
			char fc[50],fe[50];
			FILETIME lpCreationTime,lpLastAccessTime,lpLastWriteTime;
			SYSTEMTIME stime;
			WIN32_FIND_DATA* fd = new WIN32_FIND_DATA();
			if(!::GetCurrentDirectory(50,fc))
			{
				cout<<"路径可能不存在!"<<endl;
				return;
			}
			if(!SetCurrentDirectory(lsinput[1].c_str()))
			{
				cout<<"路径可能不存在!"<<endl;
				return;
			}
			if(!::GetCurrentDirectory(50,fe))
			{
				cout<<"路径可能不存在!"<<endl;
				return;
			}
			strcat(fe,"\\*.*");
			if((handle = FindFirstFile(fe, fd)) != INVALID_HANDLE_VALUE){
				do{
					HANDLE hDir = CreateFile(fd->cFileName,GENERIC_READ,FILE_SHARE_READ | FILE_SHARE_DELETE,NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,  NULL);  
					if(GetFileTime(hDir,  &lpCreationTime,  &lpLastAccessTime,  &lpLastWriteTime)){  
						FILETIME  ftime;  
						FileTimeToLocalFileTime(&lpLastWriteTime,  &ftime);  //  转换成本地时间  
						FileTimeToSystemTime(&ftime,  &stime);  //  转换成系统时间格式  
					}
					cout<<setfill('0');
					cout<<stime.wYear<<"-"<<setw(2)<<stime.wMonth<<"-"<<setw(2)<<stime.wDay<<"  "<<setw(2)<<stime.wMinute<<":"<<setw(2)<<stime.wMilliseconds;
					cout<<"  "<<fd->cFileName<<endl;
					CloseHandle(hDir);
					}while (FindNextFile(handle, fd));
			}
			FindClose(handle);
			if(!SetCurrentDirectory(fc))
				cout<<"路径可能不存在!"<<endl;
		}
	}
}

string cat()
{
	string file_name1;
	cout<<"请输入完整源文件名(包括路径和后缀名):";
	cin>>file_name1;
	ifstream infile(file_name1.c_str(),ios::in);
	if (!infile)
		{
		cerr<<"无法打开文件 "<<file_name1.c_str()<<" !"<<endl;
		}
    cout<<endl;
    char f[1000];
	infile.getline(f,1000,EOF);
	cout<<f<<endl;
	infile.close();
	return f;
}

void cp(string *cpinput,int cpcount)
{
	if(cpcount == 2)
	{
		if(cpinput[1] == "?" )
		{
			cout<<PRINTCPHELP<<endl;
		}
	}
	else if(cpcount == 3)
	{
		if(!::CopyFile(cpinput[1].c_str(),cpinput[2].c_str(),1))
			cout<<"文件或目录可能不存在!"<<endl;
		else cout<<"复制成功"<<endl;
	}
}

void rm(string *rminput,int rmcount)
{
	if(rmcount == 2)
		if(rminput[1] == "?")
			cout<<PRINTRMHELP<<endl;
		else if(!DeleteFile(rminput[1].c_str()))
			cout<<"文件可能不存在!"<<endl;
		else cout<<"删除成功"<<endl;
}

void mv(string *Moveinput,int Movecount)
{
	if(Movecount == 2)
	{
		if(Moveinput[1] == "?")
		{
			cout<<PRINTMVHELP<<endl;
		}
		else{
			if(!DeleteFile(Moveinput[1].c_str()))
				cout<<"文件可能不存在!"<<endl;
		
		}
	}
	else if(Movecount == 3)
	{
		if(!::MoveFile(Moveinput[1].c_str(),Moveinput[2].c_str()))
			cout<<"文件或目录可能不存在!"<<endl;
		else
				cout<<"成功"<<endl;
	}
}

void ren(string *reninput,int rencount)
{
	if(rencount == 2)
	{
		if(reninput[1] == "?")
		{
			cout<<PRINTRENHELP<<endl;
		}
	}
	if(rencount == 3)
	{
		if(!::MoveFile(reninput[1].c_str(),reninput[2].c_str()))
			cout<<"目标文件已存在或源文件不存在!"<<endl;
		else cout<<"重命名成功"<<endl;
	}
}

void mkdir(string *mkdirinput,int mkdircount)
{
	if(mkdircount != 2)
		cout<<"命令语法不正确!"<<endl;
	else if(mkdircount == 2){
		if(mkdirinput[1] == "?")
			cout<<PRINTMKDIRHELP<<endl;
		else if(::CreateDirectory(mkdirinput[1].c_str(),NULL) == 0)
			cout<<"路径不对或目录已存在!"<<endl;
		else cout<<" 创建目录成功"<<endl;
	}
}

void rmdir(string *rmdirinput,int rmdircount)
{
	if(rmdircount == 2)
		if(rmdirinput[1] == "?")
			cout<<PRINTRMDIRHELP<<endl;
		else if(!RemoveDirectory(rmdirinput[1].c_str()))
			cout<<"文件夹可能不存在或文件夹不为空!"<<endl;
		else cout<<"删除目录成功"<<endl;
}

void help(string *helpinput,int helpcount)
{
	if(helpcount == 1)
		cout<<PRINTHELP<<endl;
	else cout<<"命令语法不正确!"<<endl;
}

void cd(string *cdinput,int cdcount)
{
	char fc[50];
	if(cdcount == 1){
		if(!::GetCurrentDirectory(50,fc))
			cout<<"路径可能不存在!"<<endl;
		cout<<fc<<endl<<endl;
	}else if(cdcount == 2){
		if(cdinput[1] == "?")
			cout<<PRINTCDHELP<<endl;
		else if(!SetCurrentDirectory(cdinput[1].c_str()))
				cout<<"路径可能不存在!"<<endl;
	}
}

void exit(string *exitinput,int exitcount)
{
	int i=0;
	string s;
	while(exitcount--){
		s+=exitinput[i];
		s+=" ";
		i++;
	}
	if(s == "exit " || s == "exit ")
		exit(1);
	else system(s.c_str());
}

int main(int argc, char* argv[])
{
	cout<<"Microsoft Windows XP [版本 5.1.2600]\n"<<"(C) 版权所有 1985-2001 Microsoft Corp.\n\n";
	cout<<"               ==****************************************=="<<endl;
	cout<<"               ||*****显示一个目录中的文件和子目录      ls||"<<endl;
	cout<<"               ||*****显示当前目录的名称或将其更改      cd||"<<endl;
	cout<<"               ||*****打开文件并显示内容               cat||"<<endl;
	cout<<"               ||*****创建目录                       mkdir||"<<endl;
	cout<<"               ||*****将文件从一个目录移到另一个目录    mv||"<<endl;
	cout<<"               ||*****复制文件和目录树                  cp||"<<endl;
	cout<<"               ||*****重命名文件                       ren||"<<endl;
	cout<<"               ||*****退出命令解释程序                exit||"<<endl;
	cout<<"               ||*****提供帮助信息                    help||"<<endl;
	cout<<"               ||*****删除至少一个文件                  rm||"<<endl;
	cout<<"               ||*****删除空目录                     rmdir||"<<endl;
	cout<<"               ||*****显示命令帮助                 命令+?||"<<endl;
	cout<<"               ==****************************************=="<<endl;
		
	HANDLE hFile;
	string *str = new string[10];
	hFile = ::CreateFile("Mycmd.exe",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
	char Current[50];
	GetCurrentDirectory(50,Current);
	cout<<Current<<">";
	char ch;
	int count=0;
	int mark;
	while(1){
		mark=0;
		for(int i=0; i<10; i++)
			str[i] = "";
		scanf("%c",&ch);
		int strcount=0;
		while(ch != '\n'){
			mark=1;
			if(ch != ' '){
				while(ch != ' ' && ch != '\n'){
					str[strcount] += ch;
					scanf("%c",&ch);
				}
				strcount++;
				if(ch == '\n')
					goto L;
			}
			scanf("%c",&ch);
		}
		if(mark == 0)
			goto P;
L:
		if(str[0] == "ls" || str[0] == "ls")
			ls(str,strcount);
		if(str[0] == "cat" || str[0] == "cat")
			cat();
		else if(str[0] == "cp" || str[0] == "cp")
			cp(str,strcount);
		else if(str[0] == "rm" || str[0] == "rm")
			rm(str,strcount);
		else if(str[0] == "mv" || str[0] == "mv")
			mv(str,strcount);
		else if(str[0] == "ren" || str[0] == "ren")
			ren(str,strcount);
		else if(str[0] == "rmdir" || str[0] == "rmdir")
			rmdir(str,strcount);
		else if(str[0] == "mkdir" || str[0] == "mkdir")
			mkdir(str,strcount);
		else if(str[0] == "cd" || str[0] == "cd")
			cd(str,strcount);
		else if(str[0] == "help" || str[0] == "help")
			help(str,strcount);
		else exit(str,strcount);
P:
		::GetCurrentDirectory(50,Current);
		cout<<Current<<">";
	}
    system("pause");
	return 0;
}

⌨️ 快捷键说明

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