zoukankan      html  css  js  c++  java
  • ls 操作命令 -l/-R和rm -r dir 功能实现

      
     1 ls -R
     2 #include <sys/stat.h>
     3 #include <dirent.h>
     4 #include <fcntl.h>
     5 #include <stdbool.h>
     6 #include <stdio.h>
     7 
     8 
     9 int do_ls(const char *dir)
    10 {
    11     char dir_name[128];
    12     DIR *dirp;
    13     struct dirent *dp;
    14     struct stat dir_stat;
    15 
    16     if ( 0 != access(dir, F_OK) ) 
    17     {
    18         return 0;
    19     }
    20 
    21     if ( 0 > stat(dir, &dir_stat) ) 
    22     {
    23         perror("get directory stat error");
    24         return -1;
    25     }
    26 
    27 
    28      if ( S_ISDIR(dir_stat.st_mode) )
    29      {
    30          dirp = opendir(dir);
    31          printf("%s:
    ",dir);
    32         int count = 0;
    33          while ( (dp=readdir(dirp)) != NULL ) 
    34          {
    35         
    36             ++count;
    37             if ( (0 == strcmp(".", dp->d_name)) || (0 == strcmp("..", dp->d_name)) ) {
    38             continue;
    39         }
    40             printf("%s	",dp->d_name);
    41             if(5 == count)
    42             {
    43                 printf("
    ");
    44                 count = 0;
    45             }
    46         
    47         }
    48         printf("
    ---
    ");
    49         rewinddir(dirp);
    50 
    51         while ( (dp=readdir(dirp)) != NULL ) 
    52          {
    53             if ( (0 == strcmp(".", dp->d_name)) || (0 == strcmp("..", dp->d_name)) ) {
    54             continue;
    55         }
    56             
    57         char buf[100] = {};
    58         sprintf(buf,"%s/%s",dir,dp->d_name);
    59         do_ls(buf);
    60         
    61         }
    62      }
    63 
    64 }
    65 int main()
    66 {
    67     do_ls(".");
    68 }
    
    
    
    rm -r
    1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <sys/types.h> 4 #include <sys/stat.h> 5 #include <unistd.h> 6 #include <fcntl.h> 7 #include <dirent.h> 8 #include <libgen.h> 9 #include <string.h> 10 void rmr(char* path) 11 { 12 DIR* dir = opendir(path); 13 if(dir == NULL) 14 perror("opendir"),exit(-1); 15 struct dirent* ent; 16 char buf[256]; 17 while((ent=readdir(dir))) 18 { 19 if(ent->d_type == 4) 20 { 21 if(strcmp(ent->d_name,".")==0||strcmp(ent->d_name,"..")==0) 22 continue; 23 sprintf(buf,"%s/%s",path,ent->d_name); 24 25 rmr(buf); 26 27 } 28 if(ent->d_type == 8) 29 { 30 sprintf(buf,"%s/%s",path,ent->d_name); 31 if(remove(buf)!=0) perror("remove"),exit(-1); 32 } 33 } 34 if(rmdir(path)!=0) perror("rmdir"),exit(-1); 35 } 36 int main(int argc,char* argv[]) 37 { 38 if(argc != 2) 39 { 40 printf("Usage:%s directory name",basename(argv[0])); 41 exit(-1); 42 } 43 44 rmr(argv[1]); 45 printf("rm -r %s success. ",argv[1]); 46 return 0; 47 }
      1 ls -l
      2 #include <stdio.h>
      3 #include <sys/types.h>
      4 #include <dirent.h>
      5 #include <sys/stat.h>
      6 #include <pwd.h>
      7 #include <grp.h>
      8 #include <unistd.h>
      9 
     10 void show_file_info(char* filename, struct stat* info_p)
     11 {
     12     char* uid_to_name(), *ctime(), *gid_to_name(), *filemode();
     13     void mode_to_letters();
     14     char modestr[11];
     15 
     16     mode_to_letters(info_p->st_mode, modestr);
     17 
     18     printf("%s", modestr);
     19     printf(" %4d", (int) info_p->st_nlink);
     20     printf(" %-8s", uid_to_name(info_p->st_uid));
     21     printf(" %-8s", gid_to_name(info_p->st_gid));
     22     printf(" %8ld", (long) info_p->st_size);
     23     printf(" %.12s", 4 + ctime(&info_p->st_mtime));
     24        struct passwd *curr;
     25     curr = getpwuid(getuid());
     26 
     27     if(curr->pw_gid == info_p->st_gid && info_p->st_mode & S_IXGRP )
     28     {
     29         printf("33[0;32m");
     30     }
     31 
     32     if(curr->pw_gid == info_p->st_gid && curr->pw_uid == info_p->st_uid && info_p->st_mode & S_IXUSR )
     33     {
     34         printf("33[0;32m");
     35     }
     36 
     37     if(info_p->st_mode & S_IXOTH)
     38     {
     39         printf("33[0;32m");
     40     }
     41 
     42     printf(" %s
    ",filename);
     43     printf("33[0m");
     44 }
     45 
     46 void mode_to_letters(int mode, char str[])
     47 {
     48     strcpy(str,"----------");
     49 
     50     if (S_ISDIR(mode))
     51     {
     52         str[0] = 'd';
     53     }
     54 
     55     if (S_ISCHR(mode))
     56     {
     57         str[0] = 'c';
     58     }
     59 
     60     if (S_ISBLK(mode))
     61     {
     62         str[0] = 'b';
     63     }
     64 
     65     if ((mode & S_IRUSR))
     66     {
     67         str[1] = 'r';
     68     }
     69 
     70     if ((mode & S_IWUSR))
     71     {
     72         str[2] = 'w';
     73     }
     74 
     75     if ((mode & S_IXUSR))
     76     {
     77         str[3] = 'x';
     78     }
     79 
     80     if ((mode & S_IRGRP))
     81     {
     82         str[4] = 'r';
     83     }
     84 
     85     if ((mode & S_IWGRP))
     86     {
     87         str[5] = 'w';
     88     }
     89 
     90     if ((mode & S_IXGRP))
     91     {
     92         str[6] = 'x';
     93     }
     94 
     95     if ((mode & S_IROTH))
     96     {
     97         str[7] = 'r';
     98     }
     99 
    100     if ((mode & S_IWOTH))
    101     {
    102         str[8] = 'w';
    103     }
    104 
    105     if ((mode & S_IXOTH))
    106     {
    107         str[9] = 'x';
    108     }
    109 }
    110 
    111 char* uid_to_name(uid_t uid)
    112 {
    113     struct passwd* getpwuid(),* pw_ptr;
    114     static char numstr[10];
    115 
    116     if((pw_ptr = getpwuid(uid)) == NULL)
    117     {
    118         sprintf(numstr,"%d",uid);
    119 
    120         return numstr;
    121     }
    122     else
    123     {
    124         return pw_ptr->pw_name;
    125     }
    126 }
    127 
    128 char* gid_to_name(gid_t gid)
    129 {
    130     struct group* getgrgid(),* grp_ptr;
    131     static char numstr[10];
    132 
    133     if(( grp_ptr = getgrgid(gid)) == NULL)
    134     {
    135         sprintf(numstr,"%d",gid);
    136         return numstr;
    137     }
    138     else
    139     {
    140         return grp_ptr->gr_name;
    141     }
    142 }
    143 void do_ls(char dirname[])
    144 {
    145     DIR* dir_ptr;
    146     struct dirent* direntp;
    147 
    148     if ((dir_ptr = opendir(dirname)) == NULL)
    149     {
    150         fprintf(stderr, "ls2: cannot open %s 
    ", dirname);
    151     }
    152     else
    153     {
    154         while ((direntp = readdir(dir_ptr)) != NULL)
    155         {
    156             dostat(direntp->d_name);
    157         }
    158 
    159         close(dir_ptr);
    160     }
    161 }
    162 
    163 void dostat(char* filename)
    164 {
    165     struct stat info;
    166 
    167     if (stat(filename, &info) == -1)
    168     {
    169         perror(filename);
    170     }
    171     else
    172     {
    173         show_file_info(filename, &info);
    174     }
    175 }
    176 
    177 int main(int ac,char* av[])
    178 {
    179     if(ac == 1)
    180     {
    181         do_ls(".");
    182     }
    183     else
    184     {
    185         while(--ac)
    186         {
    187             printf("%s: 
    ",++*av);
    188             do_ls(*av);
    189         }
    190     }
    191 }
  • 相关阅读:
    Eureka相关相关接口和代码位置
    Zookeeper(4)---ZK集群部署和选举
    Zookeeper(3)---java客户端的使用
    Zookeeper(2)---节点属性、监听和权限
    玩转百度地图API(地图,坐标,标记,添加控件,2D图,混合图,智能搜索,地址解析器,信息窗口)
    HTML+CSS系列:CSS选择器(标签、ID、类、通配符、后代、子元素、并集、伪类)
    Git系列:常用命令
    Linux系列:快捷键、目录结构、用户目录
    mybatis-plus系统化学习之更新-AR-主键-service
    mybatis-plus系统化学习之查询专题
  • 原文地址:https://www.cnblogs.com/mingyoujizao/p/9358156.html
Copyright © 2011-2022 走看看