fchdir.. 제 사용법이 잘못 된 것인지?

gauri123의 이미지

if((dp=opendir(경로명))!=NULL)
fchdir(dp->d_fd);

이런 형태인데요... 맨페이지를 보면 분명히 이렇게 될 것 같은데 안되네요...
제가 무엇을 착각하고 있나요?

왼쪽에 찾기(Google)을 봐도 kldp에는 문서두개, google에는 맨페이지만 가득 나오네요...

gamdora의 이미지

질문에 대한 답은 되지 못하겠지만 그냥 작업 디렉토리를 바꾸시려는 것이라면

간단하게 chdir(경로명);으로 쓰셔도 됩니다.

gauri123의 이미지

그러나 제가 하려고 하는 일이 chdir로는 되지를 않아서 fchdir을 한 번 써 보려고 했습니다.
혹시 fchdir과 chdir의 기능상 차이점이 무엇인지 아시는 분이 계시다면
조언 부탁드리고 싶습니다.

타인을 자기 자신처럼 존경할수 있고, 자기가 하고 싶다고 생각하는 것을 타인에게 할 수 있다면, 그 사람은 참된 사랑을 알고 있는 사람이다. 그리고 세상에는 그 이상 가는 사람은 없다.

litdream의 이미지

답은 아니겠지만, 제가 잠시 맨페이지에서 발췌한 두가지인데, 도움이 되시길.

fchdir() is identical to chdir(); the only difference is that the directory is given as an open file descriptor.

...
The prototype for fchdir() is only available if _BSD_SOURCE is defined (either explicitly, or implicitly, by not defining _POSIX_SOURCE or compiling with the -ansi flag).

그리고, 실제로 unistd.h 에 __USE_BSD 라는 flag 을 요구하는군요?

#if defined __USE_BSD || defined __USE_XOPEN_EXTENDED
/* Change the process's working directory to the one FD is open on. */
extern int fchdir (int __fd) __THROW;
#endif

삽질의 대마왕...

삽질의 대마왕...

gauri123의 이미지

오픈 파일 디스크립터를 받아서 사용 한다는 '용법' 상의 차이는 있어도,

기능상의 차이는 없는 것이라는 확신을 주시는듯...

그렇다면 굳이 저 함수를 사용하려 할 필요는 없는 것이네요..

타인을 자기 자신처럼 존경할수 있고, 자기가 하고 싶다고 생각하는 것을 타인에게 할 수 있다면, 그 사람은 참된 사랑을 알고 있는 사람이다. 그리고 세상에는 그 이상 가는 사람은 없다.

익명사용자의 이미지

일단 기능은 같습니다. 하지만 하는 행동은 다소 틀립니다.
일단 chdir은 parent process 와 child process 두곳에서 동시에 디렉토리를 바꿉니다.
그리고 이에따라 당연히 race condition이 생기죠.
그래서 fchdir을 쓰면 그 race condition을 해결할수있습니다.

cinsk의 이미지

DIR 타입은 내부를 알 수 없는 타입입니다. 따라서 호환성이 높은 코드를 쓰기 위해서 이 내부 멤버를 쓰는 것은 바람직하지 않습니다. BSD 확장기능으로, dirfd()를 써서 DIR type 변수에서 file descriptor를 얻을 수 있긴 합니다.

따라서 호환성이 높게 프로그램을 작성하려면 readdir()을 써서 디렉토리 이름을 얻고 chdir(2)이나 fchdir(2)을 쓰면 됩니다.

그리고 chdir(2)이 child process의 현재 디렉토리를 바꿔 주지는 않습니다.

fchdir()은 chdir()에 비해 상대적으로 늦게 POSIX에 추가된 함수입니다. 특별한 경우가 아니면 chdir()을 쓰는 것이 더 호환성을 높이는데 도움을 줍니다.

--
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Korean Ver: http://www.cinsk.org/cfaqs/

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.