Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2148962
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: C/C++

2011-11-01 21:26:53

1. LINUX下目录遍历搜索文件程序,支持嵌套目录
  1. /*
  2. 编译:
  3. dir:dir.c
  4.     gcc -o $@ $<
  5. */
  6.     
  7. #include    <stdio.h>
  8. #include    <string.h>
  9. #include    <sys/types.h>
  10. #include    <dirent.h>
  11. #include     <sys/stat.h>

  12. int do_search_dir(char *path);
  13. int do_check_dir(char *fullpath, char* truefullpath);
  14. void usage(char *apps);
  15. int count = 0;

  16. int
  17. main(int argc,char **argv)
  18. {     
  19.     char fullpath[1024]={0};
  20.     if(argc != 2)
  21.     {
  22.         usage(argv[0]);
  23.         return -1;
  24.     }
  25.     if( -1 ==do_check_dir(argv[1], fullpath) )
  26.         return -1;
  27.           do_search_dir(fullpath);
  28.     printf("\nThe total number of files is %d in the directory [%s].\n\n", count , fullpath);
  29.     return 0;
  30. }

  31. //return -1 search fail
  32. //return 0 search ok
  33. int
  34. do_search_dir(char *path)
  35. {
  36.     DIR *dir;
  37.     struct dirent *s_dir;
  38.     struct stat file_stat;    
  39.     char currfile[1024]={0};
  40.     int len = strlen(path);
  41.     if(path[len-1] != '/')
  42.     {
  43.         path[len] = '/';
  44.         path[len+1] = 0;
  45.     }
  46.     printf("%s\n",path);
  47.     if( (dir=opendir(path)) == NULL)
  48.     {
  49.         printf("opendir(path) error.\n");
  50.         return -1;
  51.     }
  52.           while((s_dir=readdir(dir))!=NULL)
  53.     {
  54.               if((strcmp(s_dir->d_name,".")==0)||(strcmp(s_dir->d_name,"..")==0))
  55.             continue;
  56.               sprintf(currfile,"%s%s",path,s_dir->d_name);
  57.               stat(currfile,&file_stat);
  58.               if(S_ISDIR(file_stat.st_mode))
  59.                   do_search_dir(currfile);
  60.               else
  61.                   printf("%-32s\tOK\n",currfile);
  62.             count++;
  63.             //
  64.             //添加针对此文件的操作
  65.             //
  66.           }
  67.           closedir(dir);
  68.     return 0;
  69. }

  70. void
  71. usage(char * apps)
  72. {
  73.     printf("Directory to search for files.\n\n");
  74.     printf("Usage: %s dirpath\n\n",apps);
  75. }

  76. //return -1 directory error
  77. //args:
  78. //input     -->fullpath
  79. //output     -->truefullpath
  80. int
  81. do_check_dir(char *fullpath, char* truefullpath)
  82. {
  83.     DIR *dir;
  84.     int pathlen, i ,k;
  85.     if( (dir=opendir(fullpath)) == NULL)
  86.     {
  87.         printf("opendir fullpath error.\nMaybe dir error.\n");
  88.         return -1;
  89.     }
  90.     closedir(dir);
  91.     pathlen = strlen(fullpath);
  92.     if( pathlen-1 != 0)        // 路径长度不为1 的目录
  93.     {
  94.         if( fullpath[pathlen-1] == '/' )    // 最后字节是'/'
  95.         {    
  96.             for(i=pathlen-1; i>=0 ; i--)
  97.             {
  98.                 if( fullpath[i-1] != '/' && fullpath[i] == '/')
  99.                     break;
  100.             }    
  101.             fullpath[i+1] = 0;
  102.         }
  103.         else        // 最后字节不是'/'
  104.         {
  105.             fullpath[pathlen] = '/';
  106.             fullpath[pathlen+1] = 0;
  107.         }
  108.     }
  109.     else     // 路径长度为1 的目录
  110.     {
  111.         if(fullpath[pathlen-1] != '/')
  112.         {
  113.             fullpath[pathlen] = '/';
  114.             fullpath[pathlen+1] = 0;
  115.         }
  116.         else
  117.             fullpath[pathlen] = 0;
  118.     }
  119.     strcpy(truefullpath,fullpath);
  120.     return 0;
  121. }



阅读(2562) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~