该程序不知为何会在readdir中出现段错误,当遍历到较深的目录或者目录较长时会出现段错误,该程序是在虚拟机上跑的不知道是不是因为是虚拟机的事,以为之前在物理机上跑时没有遇见过。
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <linux/stat.h>
#include <dirent.h>
//多层目录遍历,此处的目录路径最好为绝对路径
int ls_search_file(char *path)
{
char tmp[64] = {0};
DIR *dir;
struct dirent *dt;
struct stat sta;
dir = opendir(path);
if (dir == NULL)
return -1;
perror("opendir");
while((dt = readdir(dir)) != NULL) {
if (strcmp(dt->d_name, ".") == 0 || strcmp(dt->d_name, "..") == 0)
continue;
puts(dt->d_name);
sprintf(tmp, "%s/%s", path, dt->d_name); //设置为绝对路径,非绝对路径时,lstat文件获取文件属性时失败,导致S_ISDIR计算错误
lstat(tmp, &sta); //注意此处最后不要用dt->d_type判断文件类型,部分文件系统不支持,详细参考man手册
if (S_ISDIR(sta.st_mode)) {//S_ISDIR提取文件属性是否是一个目录,即7种文件类型C D - S P B L
ls_search_file(tmp);
}
}
closedir(dir);
return 0;
}
//指定目录层遍历,只能遍历一层,无法递归遍历,测试使用telldir和seekdir进行遍历位置定位
int l1_search_file(char *path)
{
DIR *dir;
struct dirent *dt;
struct stat sta;
long offset;
int i = 0;
dir = opendir(path);
if (dir == NULL)
return -1;
while((dt = readdir(dir)) != NULL) {
if (strcmp(dt->d_name, ".") == 0 || strcmp(dt->d_name, "..") == 0)
continue;
puts(dt->d_name);
if (i++ == 2)
offset = telldir(dir); //得到当前打开的目录位置
}
seekdir(dir, offset); //修改目录位置,并以新的位置遍历显示目录
//注意:seekdir和readdir不要用在用一个while中,会使seekdir失去作用,通过seekdir修改的dir会被readdir重新修改回去
while((dt = readdir(dir)) != NULL) {
if (strcmp(dt->d_name, ".") == 0 || strcmp(dt->d_name, "..") == 0)
continue;
puts(dt->d_name);
}
closedir(dir);
return 0;
}
int main(int argc, char *argv[])
{
//ls_search_file("/root");
ls_search_file("/root/.vim/bundle/taglist.vim/.git/objects/pack");
printf("-------------------------\n");
//l1_search_file("/root/aa");
return 0;
}