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 shall 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.

       _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-2017 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().

       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-2017, <<ddiirreenntt..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  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.opengroup.org/unix/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                  2017                             DIRFD(3)
