DIRFD(3)                   POSIX Programmer's Manual                  DIRFD(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.


NNAAMMEE
       dirfd -- extract the file descriptor used by a DIR stream

SSYYNNOOPPSSIISS
       #include <dirent.h>

       int dirfd(DIR *_d_i_r_p);

DDEESSCCRRIIPPTTIIOONN
       The _d_i_r_f_d() function shall return a file descriptor referring to the
       same directory as the _d_i_r_p argument. This file descriptor shall be
       closed by a call to _c_l_o_s_e_d_i_r().  If any attempt is made to close the
       file descriptor, or to modify the state of the associated description,
       other than by means of _c_l_o_s_e_d_i_r(), _r_e_a_d_d_i_r(), _r_e_a_d_d_i_r___r(), _r_e_w_i_n_d_d_i_r(),
       or _s_e_e_k_d_i_r(), the behavior is undefined.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, the _d_i_r_f_d() function shall return an
       integer which contains a file descriptor for the stream pointed to by
       _d_i_r_p.  Otherwise, it shall return -1 and may set _e_r_r_n_o to indicate the
       error.

EERRRROORRSS
       The _d_i_r_f_d() function may fail if:

       EEIINNVVAALL The _d_i_r_p argument does not refer to a valid directory stream.

       EENNOOTTSSUUPP
              The implementation does not support the association of a file
              descriptor with a directory.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
       None.

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The _d_i_r_f_d() function is intended to be a mechanism by which an
       application may obtain a file descriptor to use for the _f_c_h_d_i_r()
       function.

RRAATTIIOONNAALLEE
       This interface was introduced because the Base Definitions volume of
       POSIX.1-2008 does not make public the DDIIRR data structure. Applications
       tend to use the _f_c_h_d_i_r() function on the file descriptor returned by
       this interface, and this has proven useful for security reasons; in
       particular, it is a better technique than others where directory names
       might change.

       The description uses the term ``a file descriptor'' rather than ``the
       file descriptor''. The implication intended is that an implementation
       that does not use an _f_d for _o_p_e_n_d_i_r() could still _o_p_e_n() the directory
       to implement the _d_i_r_f_d() function. Such a descriptor must be closed
       later during a call to _c_l_o_s_e_d_i_r().

       An implementation that does not support file descriptors referring to
       directories may fail with [[EENNOOTTSSUUPP]].

       If it is necessary to allocate an _f_d to be returned by _d_i_r_f_d(), it
       should be done at the time of a call to _o_p_e_n_d_i_r().

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _c_l_o_s_e_d_i_r(), _f_c_h_d_i_r(), _f_d_o_p_e_n_d_i_r(), _f_i_l_e_n_o(), _o_p_e_n(), _r_e_a_d_d_i_r()

       The Base Definitions volume of POSIX.1-2008, <<ddiirreenntt..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                             DIRFD(3)
