linux下实现一个简单的shell

实现了四个简单的功能pwd rm ls copy

#include
#include//标准C++函数库,主要用于字符串处理
#include//基本系统数据类型
#include//文件状态
#include//文件操作函数
#include//标准库函数
#include//文件控制
#include//定义关于时间的函数
#include//队列的定义
#include//文件树漫游
#include//UNIX系统服务的函数
#include 
#include
#include
#include 
using namespace std;

void printpath();
char *inode_to_name(int);
int getinode(char *);
char p[200];
void pwd();       

int rm(std::string);

void d_ls(  const char *);

int readWriteFile(string,string);

int main(int argc, char *argv[])
{
   cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<>>>>>Welcome to Myshell<<<<<<"< "<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<>str;      //enter the command
      if(str == "pwd"){
      printpath();
      putchar('\n');
      chdir(p);
      memset(p,0,200);
      }
      if(str == "rm") {
	string a;
	cin>>a;
        rm(a);
      }
      if(str == "pwd1") {
         pwd();
      }
      if(str == "ls") {
       char a[100];
       cout<<"请输入目录:";
       cin>>a;
       d_ls(a);
      }
       if(str == "copy") {
      string a,b;
cout<<"Original document :";
cin>>a;
cout<<"new document :";
cin>>b;
readWriteFile(a,b);
	}
    }
    
}
//readwritefile
int readWriteFile(string srcFile, string dstFile)
{
    
    ifstream in(srcFile.c_str());
    if (!in.is_open())
    {
        cout << "open src File  Error opening file" << endl;
        return -1;
    }

    ofstream out(dstFile.c_str());
    if(!out.is_open())
    {
        cout << "open dst File  Error opening file" << endl;
        return -1;
    }

    char* buf = new char[5120];
    while (!in.eof())
    {
        in.read(buf, 5120);
        cout << buf << endl;
        out.write(buf, 5120);
    }

    in.close();
    out.close();
    return 0;
}
//ls
void d_ls(  const char *Dir){

       
	DIR *dir;
	struct dirent *rent;
	if(dir =opendir(Dir))
	{
		while((rent=readdir(dir))!=NULL)
		{
                  cout<d_name<d_ino == inode){
			str = (char *)malloc(strlen(dirt->d_name)*sizeof(char));
			strcpy(str,dirt->d_name);
			return str;
		}
	}
}

 

你可能感兴趣的:(linux)