我在使用“if(S_IFDIR(stbuf.st_mode))”行时遇到了一些问题。这是测试要递归到的目录的正确方法吗?目前的功能似乎对 1 或 2 个循环正确,然后失败和分段错误。
我已经尝试了以下方法,并且可能更多作为条件。
S_ISDIR(st_mode)
((st_mode & ST_IFMT) == S_IFDIR)
S_IFDIR(stbuf.st_mode)
我已经包含了整个功能,因为我担心问题可能出在其他地方。
void getFolderContents(char *source, int temp){
struct stat stbuf;
int isDir;
dirPnt = opendir(source);
if(dirPnt != NULL){
while(entry = readdir(dirPnt)){
char *c = entry->d_name;
if(strcmp(entry->d_name, cwd) == 0 || strcmp(entry->d_name, parent) == 0){
}
else{
stat(entry->d_name, &stbuf);
printf("%i %i ", S_IFMT, stbuf.st_mode);
if(S_IFDIR(stbuf.st_mode)){ //Test DIR or file
printf("DIR: %s\n", entry->d_name);
getFolderContents(entry->d_name, 0);
}
printf("FILE: %s\n", entry->d_name);
}
}
closedir(dirPnt);
}