symbolic link file 찾는 프로그램을 만드는데 출력이 안되네요

boa3159의 이미지

#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;
}

주석처리한 부분을 활성화 하여 컴파일하면 출력이 안되고
굵은 글시 부분을 활성화 하여 컴파일하면 심볼릭링크가 출력이 됩니다.
주석처리한부분에서 무엇이 문제인지 잘모르겠네요
많은 조언 부탁드립니다 ^^

ymir의 이미지

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 』

boa3159의 이미지

우선 감사하다는 말씀부터 드릴께요^^
이제 리눅스 수업을 듣기 시작한지 2주가 다되어 기본적인 명령어 구현 프로그램을
만들어보고있습니다.
ymir님께서 적어주신 코드로 수정해서 해보아도 잘안되는데요..
path는 main에서 선언하고 해도 잘안되는데 어디서 잘못된건지 고민중에있습니다
포기하지 않겠습니다 ㅋㅋ

ymir의 이미지

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 』

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.