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

📄 myrm.cpp

📁 unix下的一个小程序,可实现rm
💻 CPP
字号:
// 0212111班  021016 (7号) 张亮 
//程序格式: ./myrm [-irv] --  [[file1] [file2] ...] 

#include  <iostream>
#include  <sys/types.h>
#include  <sys/stat.h>
#include  <dirent.h>
#include  <string.h>
using namespace std;

void myrm(int*,char*);
int main(int argc,char*argv[])
{
    int para[3];//para[0]:i,para[1]:v,para[2]:r
    para[0]=0;para[1]=0;para[2]=0;
    int signal=1;
    for(int xa=1;xa<argc;xa++)
    {
       if (argv[xa][0]=='-')
       {
         int xb=strlen(argv[xa])-1;
         for(int xi=1;xi<=xb;xi++)
         {
            if ( argv[xa][xi]=='i') para[0]=1;
             else if (argv[xa][xi]=='v') para[1]=1;
             else if (argv[xa][xi]=='r') para[2]=1;
             else if (argv[xa][xi]=='-') {signal=xa+1; break;}//-- ,quit
         }
       }
       if (signal>1) break;//--,quit
    }
    char path[argc-signal][30];
    for(int xj=0;xj<(argc-signal);xj++)
    {
        strcpy(path[xj],argv[xj+signal]);
        cout<<path[xj]<<endl;
    }
        
    //主体
    
    for(int xi=0;xi<(argc-signal);xi++)
    {  
       struct stat st;
       int ret=stat(path[xi],&st); 
       if(S_ISDIR(st.st_mode))
       {
            if (para[2]==1) //递归删除
            {
                if (para[0]==1) //交互
                {
                   cout<<"remove the dir ,y or n"<<endl;
                   char x; cin>>x; cout<<endl;   
                   if (x=='y')         
                   {
                      myrm(para,path[xi]);
                      if (para[1]==1) cout<<path[xi]<<endl;//v
                      ret=rmdir(path[xi]);                      
                      if (ret<0) cout<<"cannot remove the dir"<<endl;
                   }
                   else if (x=='n') continue;
                     else  {cout<<"input uncorrectly"<<endl; continue;}
                }
        	else {myrm(para,path[xi]); if (para[1]==1)cout<<path[xi]<<endl;//非交互
                      ret=rmdir(path[xi]); if (ret<0)cout<<"cannot remove the dir"<<endl;
                     }
            }       
       }
       else {ret=unlink(path[xi]);if (ret<0) cout<<"cannot remove the file"<<endl;}//删除文件
    }
  return 0;    
}

void myrm(int* parameter,char* path)
{  
   DIR *dirp;
   dirp=opendir(path);
   struct dirent *dp;
   while(dp=readdir(dirp))
   { cout<<dp->d_name<<endl; 
     if(strcmp(dp->d_name, ".") && strcmp(dp->d_name, ".."))
     {
      char temp[30];
      strcpy(temp,path);
      strcat(temp,"/");
      strcat(temp,dp->d_name);
      struct stat st;
      stat(temp,&st);
      if (S_ISDIR(st.st_mode)) {
           myrm(parameter,temp);
           if (parameter[0]==1)
             {
               cout<<"rm the dir?"<<endl; char x; cin>>x;
               if (x=='y')
                 {
                  if (parameter[1]==1) cout<<temp<<endl;
                  int ret= rmdir(temp); 
                  if( ret<0) cout<<"cannot remove the dir"<<endl;
                 }   
             }   
           else {if (parameter[1]==1) cout<<temp<<endl;
                 int ret=rmdir(temp);
                 if (ret<0) cout<<"cannot remove the dir"<<endl;
                }
      }
      else 
           {
            if (parameter[0]==1){
               cout<<"rm the file?"<<endl;char x; cin>>x;
               if (x=='y'){
                  if (parameter[1]==1) cout<<temp<<endl;
                  int ret=unlink(temp);
                  if (ret<0)cout<<"cannot remove the file"<<endl;
                  }
               }
            else {if (parameter[1]==1) cout<<temp<<endl;
                  int ret=unlink(temp);
                  if (ret<0)cout<<"cannot remove the file"<<endl;
                 }
           }
     }
   }
   closedir(dirp);
}
   

    
    
    

⌨️ 快捷键说明

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