9. Implement the tree in linux by yourself

running result:

Code:

  1 #include <stdio.h>
   2 #include <unistd.h>
   3 #include < string.h>
   4 #include <dirent.h>
   5 #include <sys/stat.h>
 6   7   void MyTree ( char szPath[ ], int deep)
   8 {
   9 // The directory is used to traverse 10      DIR * pDir;
 11 // It is used to store file information 12 struct dirent * pDent;
 13 // Subdirectory name 14 char szSubPath[PATH_MAX];
 15 16 // 
       
     
          
          
     Used to determine the type of the file 
17      struct stat stInfo;
 18      // Used to traverse the number of layers, the format output is 
19      int i;
 20      
21      // Open the current directory and get the directory list 
22      pDir = opendir(szPath);
 23      // If open Fail 
24      if (pDir == NULL)
 25      {
 26          perror( " Fail to opendir " );
 27          return ;
 28      }
 29      
30      // keep looping 
31      while ( 1 )
 32     {
 33          // Read the file information in turn 
34          pDent = readdir(pDir);
 35          // If the reading fails, the current recursion ends 
36          if (pDent == NULL)
 37          {
 38              break ;
 39          }
 40          
41          // If obtained File information
 42          
43          // Filter out the current directory (.) and the previous directory (..) 
44          if (strcmp(pDent->d_name, " . " )== 0 || strcmp(pDent->d_name, " . . " )== 0 )
 45          {
46              continue ;
 47          }
 48          
49          // Format output 
50          for (i= 0 ;i!=deep;i++ )
 51          {
 52              printf( " " );
 53          }
 54          
55          // If it is a directory file 
56          if (pDent- >d_type == DT_DIR)
 57          {
 58              // red 
59              printf( " ├── \033[0;31m%s\033[0m\n " ,pDent-> d_name);
 60             sprintf(szSubPath, " %s/%s " ,szPath,pDent-> d_name);
 61              MyTree(szSubPath,deep+ 1 );
 62          }
 63          else 
64          {
 65              stat(pDent->d_name,& stInfo);
 66              // Determine whether it is a normal file 
67              if (S_ISREG(stInfo.st_mode))
 68              {
 69                  // Determine whether there is execution permission 
70                  if (stInfo.st_mode & 0100 )
 71                  {
 72                      // Cyan 
73                     printf( " ├── \033[1;32m%s\033[0m\n " ,pDent-> d_name);
 74                  }
 75                  // Cannot execute file 
76                  else 
77                  {
 78                      printf( " ├── %s \n " ,pDent-> d_name);
 79                  }
 80              }
 81              // Is it a block file 
82              else  if (S_ISFIFO(stInfo.st_mode))
 83              {
 84                      printf( " ├── \033[1;42m%s \033[0m\n",pDent->d_name);
 85             }
 86             //其他文件
 87             else
 88             {
 89                 printf("├── %s\n",pDent->d_name);
 90             }
 91             
 92         }
 93     }
 94     closedir(pDir);
 95 }
 96 
 97 int main(int argc,char **argv)
 98 {
 99         //char szPath[PATH_MAX];
100         
101         
102         
103         if(argc == 2)
104         {
105             chdir(argv[1]);
106         }
107         
108         //getcwd(szPath,PATH_MAX);
109         //MyTree(szPath,0);
110         MyTree(argv[1],0);
111         
112         return 0;
113 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325271370&siteId=291194637
Recommended