Blame man-pages-posix-2013-a/man3p/fdopendir.3p

Packit 7cfc04
'\" et
Packit 7cfc04
.TH FDOPENDIR "3P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
Packit 7cfc04
.SH PROLOG
Packit 7cfc04
This manual page is part of the POSIX Programmer's Manual.
Packit 7cfc04
The Linux implementation of this interface may differ (consult
Packit 7cfc04
the corresponding Linux manual page for details of Linux behavior),
Packit 7cfc04
or the interface may not be implemented on Linux.
Packit 7cfc04
Packit 7cfc04
.SH NAME
Packit 7cfc04
fdopendir, opendir
Packit 7cfc04
\(em open directory associated with file descriptor
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.LP
Packit 7cfc04
.nf
Packit 7cfc04
#include <dirent.h>
Packit 7cfc04
.P
Packit 7cfc04
DIR *fdopendir(int \fIfd\fP);
Packit 7cfc04
DIR *opendir(const char *\fIdirname\fP);
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
\fIfdopendir\fR()
Packit 7cfc04
function shall be equivalent to the
Packit 7cfc04
\fIopendir\fR()
Packit 7cfc04
function except that the directory is specified by a file descriptor
Packit 7cfc04
rather than by a name. The file offset associated with the file
Packit 7cfc04
descriptor at the time of the call determines which entries are
Packit 7cfc04
returned.
Packit 7cfc04
.P
Packit 7cfc04
Upon successful return from
Packit 7cfc04
\fIfdopendir\fR(),
Packit 7cfc04
the file descriptor is under the control of the system, and if any
Packit 7cfc04
attempt is made to close the file descriptor, or to modify the state of
Packit 7cfc04
the associated description, other than by means of
Packit 7cfc04
\fIclosedir\fR(),
Packit 7cfc04
\fIreaddir\fR(),
Packit 7cfc04
\fIreaddir_r\fR(),
Packit 7cfc04
\fIrewinddir\fR(),
Packit 7cfc04
or
Packit 7cfc04
\fIseekdir\fR(),
Packit 7cfc04
the behavior is undefined. Upon calling
Packit 7cfc04
\fIclosedir\fR()
Packit 7cfc04
the file descriptor shall be closed.
Packit 7cfc04
.P
Packit 7cfc04
It is unspecified whether the FD_CLOEXEC flag will be set on the file
Packit 7cfc04
descriptor by a successful call to
Packit 7cfc04
\fIfdopendir\fR().
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fIopendir\fR()
Packit 7cfc04
function shall open a directory stream corresponding to the directory
Packit 7cfc04
named by the
Packit 7cfc04
.IR dirname
Packit 7cfc04
argument. The directory stream is positioned at the first entry. If
Packit 7cfc04
the type
Packit 7cfc04
.BR DIR
Packit 7cfc04
is implemented using a file descriptor, applications shall only be
Packit 7cfc04
able to open up to a total of
Packit 7cfc04
{OPEN_MAX}
Packit 7cfc04
files and directories.
Packit 7cfc04
.P
Packit 7cfc04
If the type
Packit 7cfc04
.BR DIR
Packit 7cfc04
is implemented using a file descriptor, the descriptor shall be
Packit 7cfc04
obtained as if the O_DIRECTORY flag was passed to
Packit 7cfc04
\fIopen\fR().
Packit 7cfc04
.SH "RETURN VALUE"
Packit 7cfc04
Upon successful completion, these functions shall return
Packit 7cfc04
a pointer to an object of type
Packit 7cfc04
.BR DIR .
Packit 7cfc04
Otherwise, these functions shall return a null pointer and set
Packit 7cfc04
.IR errno
Packit 7cfc04
to indicate the error.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
The
Packit 7cfc04
\fIfdopendir\fR()
Packit 7cfc04
function shall fail if:
Packit 7cfc04
.TP
Packit 7cfc04
.BR EBADF
Packit 7cfc04
The
Packit 7cfc04
.IR fd
Packit 7cfc04
argument is not a valid file descriptor open for reading.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOTDIR
Packit 7cfc04
The descriptor
Packit 7cfc04
.IR fd
Packit 7cfc04
is not associated with a directory.
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fIopendir\fR()
Packit 7cfc04
function shall fail if:
Packit 7cfc04
.TP
Packit 7cfc04
.BR EACCES
Packit 7cfc04
Search permission is denied for the component of the path prefix of
Packit 7cfc04
.IR dirname
Packit 7cfc04
or read permission is denied for
Packit 7cfc04
.IR dirname .
Packit 7cfc04
.TP
Packit 7cfc04
.BR ELOOP
Packit 7cfc04
A loop exists in symbolic links encountered during resolution of the
Packit 7cfc04
.IR dirname
Packit 7cfc04
argument.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENAMETOOLONG
Packit 7cfc04
.br
Packit 7cfc04
The length of a component of a pathname is longer than
Packit 7cfc04
{NAME_MAX}.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOENT
Packit 7cfc04
A component of
Packit 7cfc04
.IR dirname
Packit 7cfc04
does not name an existing directory or
Packit 7cfc04
.IR dirname
Packit 7cfc04
is an empty string.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENOTDIR
Packit 7cfc04
A component of
Packit 7cfc04
.IR dirname
Packit 7cfc04
names an existing file that is neither a directory nor a symbolic link
Packit 7cfc04
to a directory.
Packit 7cfc04
.br
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
\fIopendir\fR()
Packit 7cfc04
function may fail if:
Packit 7cfc04
.TP
Packit 7cfc04
.BR ELOOP
Packit 7cfc04
More than
Packit 7cfc04
{SYMLOOP_MAX}
Packit 7cfc04
symbolic links were encountered during resolution of the
Packit 7cfc04
.IR dirname
Packit 7cfc04
argument.
Packit 7cfc04
.TP
Packit 7cfc04
.BR EMFILE
Packit 7cfc04
All file descriptors available to the process are currently open.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENAMETOOLONG
Packit 7cfc04
.br
Packit 7cfc04
The length of a pathname exceeds
Packit 7cfc04
{PATH_MAX},
Packit 7cfc04
or pathname resolution of a symbolic link produced an intermediate
Packit 7cfc04
result with a length that exceeds
Packit 7cfc04
{PATH_MAX}.
Packit 7cfc04
.TP
Packit 7cfc04
.BR ENFILE
Packit 7cfc04
Too many files are currently open in the system.
Packit 7cfc04
.LP
Packit 7cfc04
.IR "The following sections are informative."
Packit 7cfc04
.SH EXAMPLES
Packit 7cfc04
.SS "Open a Directory Stream"
Packit 7cfc04
.P
Packit 7cfc04
The following program fragment demonstrates how the
Packit 7cfc04
\fIopendir\fR()
Packit 7cfc04
function is used.
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
#include <dirent.h>
Packit 7cfc04
\&...
Packit 7cfc04
    DIR *dir;
Packit 7cfc04
    struct dirent *dp;
Packit 7cfc04
\&...
Packit 7cfc04
    if ((dir = opendir (".")) == NULL) {
Packit 7cfc04
        perror ("Cannot open .");
Packit 7cfc04
        exit (1);
Packit 7cfc04
    }
Packit 7cfc04
.P
Packit 7cfc04
    while ((dp = readdir (dir)) != NULL) {
Packit 7cfc04
\&...
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.SS "Find And Open a File"
Packit 7cfc04
.P
Packit 7cfc04
The following program searches through a given directory looking
Packit 7cfc04
for files whose name does not begin with a dot and whose size is
Packit 7cfc04
larger than 1 MiB.
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <dirent.h>
Packit 7cfc04
#include <fcntl.h>
Packit 7cfc04
#include <sys/stat.h>
Packit 7cfc04
#include <stdint.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <unistd.h>
Packit 7cfc04
.P
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    struct stat statbuf;
Packit 7cfc04
    DIR *d;
Packit 7cfc04
    struct dirent *dp;
Packit 7cfc04
    int dfd, ffd;
Packit 7cfc04
.P
Packit 7cfc04
    if ((d = fdopendir((dfd = open("./tmp", O_RDONLY)))) == NULL) {
Packit 7cfc04
        fprintf(stderr, "Cannot open ./tmp directory\en");
Packit 7cfc04
        exit(1);
Packit 7cfc04
    }
Packit 7cfc04
    while ((dp = readdir(d)) != NULL) {
Packit 7cfc04
        if (dp->d_name[0] == '.')
Packit 7cfc04
            continue;
Packit 7cfc04
        /* there is a possible race condition here as the file
Packit 7cfc04
         * could be renamed between the readdir and the open */
Packit 7cfc04
        if ((ffd = openat(dfd, dp->d_name, O_RDONLY)) == -1) {
Packit 7cfc04
            perror(dp->d_name);
Packit 7cfc04
            continue;
Packit 7cfc04
        }
Packit 7cfc04
        if (fstat(ffd, &statbuf) == 0 && statbuf.st_size > (1024*1024)) {
Packit 7cfc04
            /* found it ... */
Packit 7cfc04
            printf("%s: %jdK\en", dp->d_name,
Packit 7cfc04
                (intmax_t)(statbuf.st_size / 1024));
Packit 7cfc04
        }
Packit 7cfc04
        close(ffd);
Packit 7cfc04
    }
Packit 7cfc04
    closedir(d); // note this implicitly closes dfd
Packit 7cfc04
    return 0;
Packit 7cfc04
}
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.SH "APPLICATION USAGE"
Packit 7cfc04
The
Packit 7cfc04
\fIopendir\fR()
Packit 7cfc04
function should be used in conjunction with
Packit 7cfc04
\fIreaddir\fR(),
Packit 7cfc04
\fIclosedir\fR(),
Packit 7cfc04
and
Packit 7cfc04
\fIrewinddir\fR()
Packit 7cfc04
to examine the contents of the directory (see the EXAMPLES section in
Packit 7cfc04
.IR "\fIreaddir\fR\^(\|)").
Packit 7cfc04
This method is recommended for portability.
Packit 7cfc04
.SH RATIONALE
Packit 7cfc04
The purpose of the
Packit 7cfc04
\fIfdopendir\fR()
Packit 7cfc04
function is to enable opening files in directories other than the
Packit 7cfc04
current working directory without exposure to race conditions. Any part
Packit 7cfc04
of the path of a file could be changed in parallel to a call to
Packit 7cfc04
\fIopendir\fR(),
Packit 7cfc04
resulting in unspecified behavior.
Packit 7cfc04
.P
Packit 7cfc04
Based on historical implementations, the rules about file descriptors
Packit 7cfc04
apply to directory streams as well. However, this volume of POSIX.1\(hy2008 does not
Packit 7cfc04
mandate that the directory stream be implemented using file
Packit 7cfc04
descriptors. The description of
Packit 7cfc04
\fIclosedir\fR()
Packit 7cfc04
clarifies that if a file descriptor is used for the directory stream,
Packit 7cfc04
it is mandatory that
Packit 7cfc04
\fIclosedir\fR()
Packit 7cfc04
deallocate the file descriptor. When a file descriptor is used to
Packit 7cfc04
implement the directory stream, it behaves as if the FD_CLOEXEC
Packit 7cfc04
had been set for the file descriptor.
Packit 7cfc04
.P
Packit 7cfc04
The directory entries for dot
Packit 7cfc04
and dot-dot
Packit 7cfc04
are optional. This volume of POSIX.1\(hy2008 does not provide a way to test
Packit 7cfc04
.IR "a priori"
Packit 7cfc04
for their existence because an application that is portable must be
Packit 7cfc04
written to look for (and usually ignore) those entries. Writing code
Packit 7cfc04
that presumes that they are the first two entries does not always work,
Packit 7cfc04
as many implementations permit them to be other than the first two
Packit 7cfc04
entries, with a ``normal'' entry preceding them. There is negligible
Packit 7cfc04
value in providing a way to determine what the implementation does
Packit 7cfc04
because the code to deal with dot and dot-dot must be written in any
Packit 7cfc04
case and because such a flag would add to the list of those flags
Packit 7cfc04
(which has proven in itself to be objectionable) and might be abused.
Packit 7cfc04
.P
Packit 7cfc04
Since the structure and buffer allocation, if any, for directory
Packit 7cfc04
operations are defined by the implementation, this volume of POSIX.1\(hy2008 imposes no
Packit 7cfc04
portability requirements for erroneous program constructs, erroneous
Packit 7cfc04
data, or the use of unspecified values such as the use or referencing
Packit 7cfc04
of a
Packit 7cfc04
.IR dirp
Packit 7cfc04
value or a
Packit 7cfc04
.BR dirent
Packit 7cfc04
structure value after a directory stream has been closed or after a
Packit 7cfc04
\fIfork\fR()
Packit 7cfc04
or one of the
Packit 7cfc04
.IR exec
Packit 7cfc04
function calls.
Packit 7cfc04
.SH "FUTURE DIRECTIONS"
Packit 7cfc04
None.
Packit 7cfc04
.SH "SEE ALSO"
Packit 7cfc04
.IR "\fIclosedir\fR\^(\|)",
Packit 7cfc04
.IR "\fIdirfd\fR\^(\|)",
Packit 7cfc04
.IR "\fIfstatat\fR\^(\|)",
Packit 7cfc04
.IR "\fIopen\fR\^(\|)",
Packit 7cfc04
.IR "\fIreaddir\fR\^(\|)",
Packit 7cfc04
.IR "\fIrewinddir\fR\^(\|)",
Packit 7cfc04
.IR "\fIsymlink\fR\^(\|)"
Packit 7cfc04
.P
Packit 7cfc04
The Base Definitions volume of POSIX.1\(hy2008,
Packit 7cfc04
.IR "\fB<dirent.h>\fP",
Packit 7cfc04
.IR "\fB<sys_types.h>\fP"
Packit 7cfc04
.SH COPYRIGHT
Packit 7cfc04
Portions of this text are reprinted and reproduced in electronic form
Packit 7cfc04
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
Packit 7cfc04
-- Portable Operating System Interface (POSIX), The Open Group Base
Packit 7cfc04
Specifications Issue 7, Copyright (C) 2013 by the Institute of
Packit 7cfc04
Electrical and Electronics Engineers, Inc and The Open Group.
Packit 7cfc04
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
Packit 7cfc04
event of any discrepancy between this version and the original IEEE and
Packit 7cfc04
The Open Group Standard, the original IEEE and The Open Group Standard
Packit 7cfc04
is the referee document. The original Standard can be obtained online at
Packit 7cfc04
http://www.unix.org/online.html .
Packit 7cfc04
Packit 7cfc04
Any typographical or formatting errors that appear
Packit 7cfc04
in this page are most likely
Packit 7cfc04
to have been introduced during the conversion of the source files to
Packit 7cfc04
man page format. To report such errors, see
Packit 7cfc04
https://www.kernel.org/doc/man-pages/reporting_bugs.html .