「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);
}