symbolic link file 찾는 프로그램을 만드는데 출력이 안되네요
글쓴이: boa3159 / 작성시간: 목, 2008/02/14 - 10:41오전
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
DIR *pdir;
struct dirent *pde;
// struct stat buf;
if((pdir = opendir(argv[1])) < 0){
perror("opendir");
exit(1);
}
while((pde = readdir(pdir)) != NULL){
// lstat(pde->d_type, &buf);
// if(S_ISLNK(buf.st_mode)){
// printf(" SYMBOLIC LINK : %s\n", pde->d_name);
if(pde->d_type==10){
printf(" SYMBOLIC LINK : %s\n", pde->d_name);
}
}
closedir(pdir);
return 0;
}
주석처리한 부분을 활성화 하여 컴파일하면 출력이 안되고
굵은 글시 부분을 활성화 하여 컴파일하면 심볼릭링크가 출력이 됩니다.
주석처리한부분에서 무엇이 문제인지 잘모르겠네요
많은 조언 부탁드립니다 ^^
Forums:
음...
lstat 에 path 가 잘못 들어갔군요..
그리고 단순 파일명이 아니라 절대/상대 경로로 입력되도록 해야 합니다.
snprintf(path, BUFSIZ, "%s/%s", argv[1], pde->d_name);
lstat(path, &buf);
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
답변해주셔서 감사합니다 ^^
우선 감사하다는 말씀부터 드릴께요^^
이제 리눅스 수업을 듣기 시작한지 2주가 다되어 기본적인 명령어 구현 프로그램을
만들어보고있습니다.
ymir님께서 적어주신 코드로 수정해서 해보아도 잘안되는데요..
path는 main에서 선언하고 해도 잘안되는데 어디서 잘못된건지 고민중에있습니다
포기하지 않겠습니다 ㅋㅋ
음...
CWD - dir - suba
` subb
와 같은 디렉토리 구조가 있다고 할 때..
CWD 에서 a.out dir 과 같이 실행을 하게 되면..
argv[1] 에는 "dir" 이 들어갑니다..
opendir("dir") 후에.. readdir() 을 실행하게 되면...
pde->d_name 에는 "suba", "subb" 와 같이 이름만 들어갑니다..
결국 lstat(pde->d_name, &buf) 을 불러봐야..
현재 디렉토리에서는 존재하지 않는 디렉토리가 되는 셈이므로..
lstat() 이 찾지 못하는 겁니다.. (lstat() 의 리턴값을 확인해 보세요)
예시하신 코드는 모든 하위 디렉토리를 탐색하는 것이 아니라서..
snprintf(path, BUFSIZ, "%s/%s", argv[1], pde->d_name);
lstat(path, &buf);
와 같이.. 간단하게 입력 디렉토리를 넣어 full path 를 만들면..
lstat 의 path 에는 "dir/suba" 라는 이름이 들어가겠죠..
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』
댓글 달기