FDOPENDIR(3)               POSIX Programmer's Manual              FDOPENDIR(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
       fdopendir, opendir -- open directory associated with file descriptor

SSYYNNOOPPSSIISS
       #include <dirent.h>

       DIR *fdopendir(int _f_d);
       DIR *opendir(const char *_d_i_r_n_a_m_e);

DDEESSCCRRIIPPTTIIOONN
       The _f_d_o_p_e_n_d_i_r() function shall be equivalent to the _o_p_e_n_d_i_r() function
       except that the directory is specified by a file descriptor rather than
       by a name. The file offset associated with the file descriptor at the
       time of the call determines which entries are returned.

       Upon successful return from _f_d_o_p_e_n_d_i_r(), the file descriptor is under
       the control of the system, and 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. Upon calling _c_l_o_s_e_d_i_r() the file
       descriptor shall be closed.

       It is unspecified whether the FD_CLOEXEC flag will be set on the file
       descriptor by a successful call to _f_d_o_p_e_n_d_i_r().

       The _o_p_e_n_d_i_r() function shall open a directory stream corresponding to
       the directory named by the _d_i_r_n_a_m_e argument. The directory stream is
       positioned at the first entry. If the type DDIIRR is implemented using a
       file descriptor, applications shall only be able to open up to a total
       of {OPEN_MAX} files and directories.

       If the type DDIIRR is implemented using a file descriptor, the descriptor
       shall be obtained as if the O_DIRECTORY flag was passed to _o_p_e_n().

RREETTUURRNN VVAALLUUEE
       Upon successful completion, these functions shall return a pointer to
       an object of type DDIIRR.  Otherwise, these functions shall return a null
       pointer and set _e_r_r_n_o to indicate the error.

EERRRROORRSS
       The _f_d_o_p_e_n_d_i_r() function shall fail if:

       EEBBAADDFF  The _f_d argument is not a valid file descriptor open for reading.

       EENNOOTTDDIIRR
              The descriptor _f_d is not associated with a directory.

       The _o_p_e_n_d_i_r() function shall fail if:

       EEAACCCCEESS Search permission is denied for the component of the path prefix
              of _d_i_r_n_a_m_e or read permission is denied for _d_i_r_n_a_m_e.

       EELLOOOOPP  A loop exists in symbolic links encountered during resolution of
              the _d_i_r_n_a_m_e argument.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a component of a pathname is longer than
              {NAME_MAX}.

       EENNOOEENNTT A component of _d_i_r_n_a_m_e does not name an existing directory or
              _d_i_r_n_a_m_e is an empty string.

       EENNOOTTDDIIRR
              A component of _d_i_r_n_a_m_e names an existing file that is neither a
              directory nor a symbolic link to a directory.

       The _o_p_e_n_d_i_r() function may fail if:

       EELLOOOOPP  More than {SYMLOOP_MAX} symbolic links were encountered during
              resolution of the _d_i_r_n_a_m_e argument.

       EEMMFFIILLEE All file descriptors available to the process are currently
              open.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a pathname exceeds {PATH_MAX}, or pathname
              resolution of a symbolic link produced an intermediate result
              with a length that exceeds {PATH_MAX}.

       EENNFFIILLEE Too many files are currently open in the system.

       _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
   OOppeenn aa DDiirreeccttoorryy SSttrreeaamm
       The following program fragment demonstrates how the _o_p_e_n_d_i_r() function
       is used.


           #include <dirent.h>
           ...
               DIR *dir;
               struct dirent *dp;
           ...
               if ((dir = opendir (".")) == NULL) {
                   perror ("Cannot open .");
                   exit (1);
               }

               while ((dp = readdir (dir)) != NULL) {
           ...

   FFiinndd AAnndd OOppeenn aa FFiillee
       The following program searches through a given directory looking for
       files whose name does not begin with a dot and whose size is larger
       than 1 MiB.


           #include <stdio.h>
           #include <dirent.h>
           #include <fcntl.h>
           #include <sys/stat.h>
           #include <stdint.h>
           #include <stdlib.h>
           #include <unistd.h>

           int
           main(int argc, char *argv[])
           {
               struct stat statbuf;
               DIR *d;
               struct dirent *dp;
               int dfd, ffd;

               if ((d = fdopendir((dfd = open("./tmp", O_RDONLY)))) == NULL) {
                   fprintf(stderr, "Cannot open ./tmp directory\n");
                   exit(1);
               }
               while ((dp = readdir(d)) != NULL) {
                   if (dp->d_name[0] == '.')
                       continue;
                   /* there is a possible race condition here as the file
                    * could be renamed between the readdir and the open */
                   if ((ffd = openat(dfd, dp->d_name, O_RDONLY)) == -1) {
                       perror(dp->d_name);
                       continue;
                   }
                   if (fstat(ffd, &statbuf) == 0 && statbuf.st_size > (1024*1024)) {
                       /* found it ... */
                       printf("%s: %jdK\n", dp->d_name,
                           (intmax_t)(statbuf.st_size / 1024));
                   }
                   close(ffd);
               }
               closedir(d); // note this implicitly closes dfd
               return 0;
           }

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The _o_p_e_n_d_i_r() function should be used in conjunction with _r_e_a_d_d_i_r(),
       _c_l_o_s_e_d_i_r(), and _r_e_w_i_n_d_d_i_r() to examine the contents of the directory
       (see the EXAMPLES section in _r_e_a_d_d_i_r()).  This method is recommended
       for portability.

RRAATTIIOONNAALLEE
       The purpose of the _f_d_o_p_e_n_d_i_r() function is to enable opening files in
       directories other than the current working directory without exposure
       to race conditions. Any part of the path of a file could be changed in
       parallel to a call to _o_p_e_n_d_i_r(), resulting in unspecified behavior.

       Based on historical implementations, the rules about file descriptors
       apply to directory streams as well. However, this volume of
       POSIX.1-2017 does not mandate that the directory stream be implemented
       using file descriptors. The description of _c_l_o_s_e_d_i_r() clarifies that if
       a file descriptor is used for the directory stream, it is mandatory
       that _c_l_o_s_e_d_i_r() deallocate the file descriptor. When a file descriptor
       is used to implement the directory stream, it behaves as if the
       FD_CLOEXEC had been set for the file descriptor.

       The directory entries for dot and dot-dot are optional. This volume of
       POSIX.1-2017 does not provide a way to test _a _p_r_i_o_r_i for their
       existence because an application that is portable must be written to
       look for (and usually ignore) those entries. Writing code that presumes
       that they are the first two entries does not always work, as many
       implementations permit them to be other than the first two entries,
       with a ``normal'' entry preceding them. There is negligible value in
       providing a way to determine what the implementation does because the
       code to deal with dot and dot-dot must be written in any case and
       because such a flag would add to the list of those flags (which has
       proven in itself to be objectionable) and might be abused.

       Since the structure and buffer allocation, if any, for directory
       operations are defined by the implementation, this volume of
       POSIX.1-2017 imposes no portability requirements for erroneous program
       constructs, erroneous data, or the use of unspecified values such as
       the use or referencing of a _d_i_r_p value or a ddiirreenntt structure value
       after a directory stream has been closed or after a _f_o_r_k() or one of
       the _e_x_e_c function calls.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _c_l_o_s_e_d_i_r(), _d_i_r_f_d(), _f_s_t_a_t_a_t(), _o_p_e_n(), _r_e_a_d_d_i_r(), _r_e_w_i_n_d_d_i_r(),
       _s_y_m_l_i_n_k()

       The Base Definitions volume of POSIX.1-2017, <<ddiirreenntt..hh>>, <<ssyyss__ttyyppeess..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                         FDOPENDIR(3)
