Linux显示文件树形结构信息的小程序


Linux显示文件树形结构信息的小程序:

  1. #include <stdio.h>   
  2. #include <sys/types.h>   
  3. #include <sys/stat.h>   
  4. #include <unistd.h>   
  5. #include <dirent.h>   
  6. #include <errno.h>   
  7. #include <stdlib.h>   
  8. #include <string.h>   
  9.   
  10. // 最大目录深度   
  11. #define MAX_DIR_DEPTH   64   
  12. #define __LEN           4   
  13. #define MAX_FILE_LEN    256   
  14. #define MAX_LEN         ((64 * __LEN) +  MAX_FILE_LEN)   
  15. #define MAX_PATH_LEN    (64 * 32)   
  16.   
  17. void print_dir(const char * file_name, int depth, char *show_c, struct dirent *pDirRes)  
  18. {  
  19.     int i;  
  20.     int pos = depth * __LEN;  
  21.       
  22.     show_c[pos - __LEN+1]  = '|';  
  23.     for(i = 0; i < __LEN-1;  i++) show_c[pos - i] = '-';  
  24.     show_c[pos+1] = '\0';  
  25.   
  26.     printf("%s%s\n", show_c, file_name);  
  27.     if(!pDirRes) {  
  28.         show_c[pos-__LEN + 1] = ' ';  
  29.     }  
  30.       
  31.     for(i = 0; i < __LEN-1;  i++) show_c[pos - i] = ' ';  
  32. }  
  33.   
  34. void clear_c(int depth, char *show_c )  
  35. {  
  36.     show_c[(depth-1) * __LEN + 1] = ' ';  
  37. }  
  38.   
  39. int is_last_dir(DIR *pDir)  
  40. {  
  41.     return 0;  
  42. }  
  43.   
  44. int __tree_dir(const char* dirname, int depth, char *show_c)  
  45. {  
  46.     DIR     *pDir;  
  47.     struct stat file_stat;  
  48.     struct dirent diren;  
  49.     struct dirent *pDirRes;  
  50.     char *tmpname;  
  51.   
  52.     if(depth >= MAX_DIR_DEPTH) return -1;  
  53.   
  54.     tmpname = malloc(MAX_PATH_LEN);  
  55.     if(!tmpname) return -1;  
  56.   
  57.     /* open dir */  
  58.     pDir = opendir(dirname);  
  59.     if(NULL == pDir){  
  60.         printf("%s %s\n", dirname , strerror(errno));   
  61.         goto ERR;  
  62.     }  
  63.   
  64.     do{                
  65.          if(readdir_r(pDir, &diren, &pDirRes)){  
  66.             closedir(pDir);  
  67.             printf("%s\n", strerror(errno));  
  68.             goto ERR;  
  69.         }  
  70.   
  71.         if(!pDirRes) { clear_c(depth, show_c); break; }  
  72.   
  73.         if(strcmp(".", diren.d_name) == 0 || strcmp("..", diren.d_name)==0) continue;  
  74.   
  75.         snprintf(tmpname, MAX_PATH_LEN, "%s/%s", dirname , diren.d_name);  
  76.         bzero(&file_stat, sizeof(file_stat));  
  77.   
  78.         if(-1 == stat(tmpname, &file_stat)){  
  79.             closedir(pDir);  
  80.             printf("%s\n", strerror(errno));  
  81.             goto ERR;  
  82.         }  
  83.   
  84.         print_dir(diren.d_name, depth, show_c, pDirRes);  
  85.   
  86.         if(S_ISDIR(file_stat.st_mode)){  
  87. //            if(is_last_dir(pDir)) clear(depth, show_c);   
  88.             __tree_dir(tmpname, depth+1, show_c);  
  89.         }  
  90.   
  91.     }while(NULL != pDirRes);  
  92.   
  93.     closedir(pDir);  
  94.     free(tmpname);  
  95.     return 0;  
  96.   
  97. ERR:  
  98.     free(tmpname);  
  99.     return -1;  
  100.   
  101. }  
  102.   
  103. int tree_dir(const char* path)  
  104. {  
  105.     char *show_c ;  
  106.     int ret;  
  107.   
  108.     show_c = malloc(MAX_LEN);  
  109.   
  110.     if(!show_c) return errno;  
  111.     memset(show_c, ' ', MAX_LEN);  
  112.   
  113.     ret = __tree_dir(path, 1, show_c);  
  114.   
  115.     free(show_c);  
  116.     return ret;  
  117. }  
  118.   
  119. #define VERSION    "version v1.0 : created by Dremi"   
  120.   
  121. int main(int argc, char**argv)  
  122. {  
  123.     if(argc < 2) return -1;  
  124.   
  125.     if(strcmp("-v", argv[1]) == 0){  
  126.         printf("%s\n", VERSION);  
  127.         return 0;  
  128.     }  
  129.   
  130.     return tree_dir(argv[1]);  
  131. }  

版本2:

  1. int __tree_dir(const char* dirname, int depth, char *show_c)  
  2. {  
  3.     DIR     *pDir;  
  4.     struct stat file_stat;  
  5.     struct dirent diren;  
  6.     struct dirent *pDirRes;  
  7.     char  *olddir;  
  8.   
  9.     if(depth >= MAX_DIR_DEPTH) return -1;  
  10.   
  11.     olddir= malloc(MAX_FILE_LEN);  
  12.     if(!olddir) return -1;  
  13.   
  14.     getcwd(olddir, MAX_FILE_LEN);  
  15.   
  16.     if(chdir(dirname)){  
  17.         free(olddir);  
  18.         printf("%s %s\n", dirname, strerror(errno));  
  19.         return -1;  
  20.     }  
  21.   
  22.     /* open dir */  
  23.     pDir = opendir("./");  
  24.     if(NULL == pDir){  
  25.         free(olddir);  
  26.         printf("%s %s\n", dirname , strerror(errno));   
  27.         return -1;  
  28.     }  
  29.   
  30.     do{                
  31.          if(readdir_r(pDir, &diren, &pDirRes)){  
  32.             closedir(pDir);  
  33.             free(olddir);  
  34.             printf("%s\n", strerror(errno));  
  35.             return -1;  
  36.         }  
  37.   
  38.         if(!pDirRes) { clear_c(depth, show_c); break; }  
  39.   
  40.         if(strcmp(".", diren.d_name) == 0 || strcmp("..", diren.d_name)==0) continue;  
  41.   
  42.         bzero(&file_stat, sizeof(file_stat));  
  43.   
  44.         if(-1 == stat(diren.d_name, &file_stat)){  
  45.             closedir(pDir);  
  46.             free(olddir);  
  47.             printf("%s\n", strerror(errno));  
  48.             return -1;  
  49.         }  
  50.   
  51.         print_dir(diren.d_name, depth, show_c, pDirRes);  
  52.   
  53.         if(S_ISDIR(file_stat.st_mode)){  
  54. //            if(is_last_dir(pDir)) clear(depth, show_c);   
  55.             __tree_dir(diren.d_name, depth+1, show_c);  
  56.         }  
  57.   
  58.     }while(NULL != pDirRes);  
  59.   
  60.     closedir(pDir);  
  61.     chdir(olddir);  
  62.     free(olddir);  
  63.     return 0;  
  64. }  

效果如下:

  1. |---main_x64  
  2. |---main_x86  
  3. |---test_asm  
  4. |   |---a.out  
  5. |   |---asm.c  
  6. |   |---get_reg.c  
  7. |---test_dll  
  8. |   |---main  
  9. |   |---mk_dll.sh  
  10. |   |---test_dll1.c  
  11. |   |---test_dll1.h  
  12. |   |---test_dll2.c  
  13. |   |---test_dll2.h  
  14. |   |---libtestdll1.so  
  15. |   |---libtestdll2.so  
  16. |   |---main.c  
  17. |---test_log  
  18. |   |---test_log.c  
  19. |   |---a.out  
  20. |---test1.c  
  21. |---test2.c  
  22. |---com_prog  

相关内容