S_ISDIR(m)에 관해
글쓴이: twins99 / 작성시간: 수, 2005/11/30 - 3:58오후
다음 코드를 봐 주세요.
책에 있는 예제 코드인데요, 실행시에 문제가 발생하네요.
일단 코드 올라갑니다.
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
void printdir(char *dir, int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp = opendir(dir)) == NULL){
fprintf(stderr,"cannot open directory: %s\n");
return ;
}
chdir(dir);
while((entry = readdir(dp))!= NULL){
stat(entry->d_name, &statbuf);
if(S_ISDIR(statbuf.st_mode)){
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) ==0)
continue;
printf("%*s%s/\n",depth,"",entry->d_name);
printdir(entry->d_name, depth+4);
}
else printf("%*s%s\n",depth, "", entry->d_name);
}
chdir("..");
closedir(dp);
}
int main()
{
char d[100] = {"/root/Desktop/Trash/dist/include/dom"};
printf("Directory scan of %s\n",d);
printdir(d,0);
printf("done.\n");
return 0;
}
실행시에 모든 서브 디렉토리를 보여주는 코드인데요,
directory인지 판별하는 S_ISDIR()에서 symbolic link는
구별을 못해 주네요.. statbuf.st_mode를 찍어보면
그냥 디렉토리랑 똑같은 mode값을 가집니다.
이거 어떻게 해결해야 하나요?? 미치겠습니다.
--추가합니다.
꼭 symbolic link파일이 아니고 일반 regular file도 directory
로 인식하는 경우가 있네요. 근데, random하게 그런게 아니라
특정 파일의 경우에만 그럽니다..이거 어떻게 해결해야 하나요?
Forums:


lstat의 멘페이지에서 발췌했습니다.[quote]The ls
lstat의 멘페이지에서 발췌했습니다.
감사합니다.
답변 감사드립니다.
댓글 달기