1DIRFD(3P)                  POSIX Programmer's Manual                 DIRFD(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       dirfd — extract the file descriptor used by a DIR stream
14

SYNOPSIS

16       #include <dirent.h>
17
18       int dirfd(DIR *dirp);
19

DESCRIPTION

21       The dirfd() function shall return a file descriptor  referring  to  the
22       same  directory  as  the  dirp  argument. This file descriptor shall be
23       closed by a call to closedir().  If any attempt is made  to  close  the
24       file  descriptor, or to modify the state of the associated description,
25       other than by means of closedir(), readdir(), readdir_r(), rewinddir(),
26       or seekdir(), the behavior is undefined.
27

RETURN VALUE

29       Upon  successful completion, the dirfd() function shall return an inte‐
30       ger which contains a file descriptor for the stream pointed to by dirp.
31       Otherwise, it shall return −1 and may set errno to indicate the error.
32

ERRORS

34       The dirfd() function may fail if:
35
36       EINVAL The dirp argument does not refer to a valid directory stream.
37
38       ENOTSUP
39              The  implementation  does  not support the association of a file
40              descriptor with a directory.
41
42       The following sections are informative.
43

EXAMPLES

45       None.
46

APPLICATION USAGE

48       The dirfd() function is intended to be a mechanism by which an applica‐
49       tion may obtain a file descriptor to use for the fchdir() function.
50

RATIONALE

52       This  interface  was  introduced because the Base Definitions volume of
53       POSIX.1‐2008 does not make public the DIR data structure.  Applications
54       tend  to  use  the fchdir() function on the file descriptor returned by
55       this interface, and this has proven useful  for  security  reasons;  in
56       particular,  it is a better technique than others where directory names
57       might change.
58
59       The description uses the term ``a file descriptor'' rather  than  ``the
60       file  descriptor''.  The implication intended is that an implementation
61       that does not use an fd for opendir() could still open() the  directory
62       to  implement  the  dirfd()  function. Such a descriptor must be closed
63       later during a call to closedir().
64
65       An implementation that does not support file descriptors  referring  to
66       directories may fail with [ENOTSUP].
67
68       If  it  is  necessary  to  allocate an fd to be returned by dirfd(), it
69       should be done at the time of a call to opendir().
70

FUTURE DIRECTIONS

72       None.
73

SEE ALSO

75       closedir(), fchdir(), fdopendir(), fileno(), open(), readdir()
76
77       The Base Definitions volume of POSIX.1‐2008, <dirent.h>
78
80       Portions of this text are reprinted and reproduced in  electronic  form
81       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
82       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
83       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
84       cal and Electronics Engineers,  Inc  and  The  Open  Group.   (This  is
85       POSIX.1-2008  with  the  2013  Technical Corrigendum 1 applied.) In the
86       event of any discrepancy between this version and the original IEEE and
87       The  Open Group Standard, the original IEEE and The Open Group Standard
88       is the referee document. The original Standard can be  obtained  online
89       at http://www.unix.org/online.html .
90
91       Any  typographical  or  formatting  errors that appear in this page are
92       most likely to have been introduced during the conversion of the source
93       files  to  man page format. To report such errors, see https://www.ker
94       nel.org/doc/man-pages/reporting_bugs.html .
95
96
97
98IEEE/The Open Group                  2013                            DIRFD(3P)
Impressum