Blame man3/readdir.3

Packit 7cfc04
.\" Copyright (C) 1993 David Metcalfe (david@prism.demon.co.uk)
Packit 7cfc04
.\" and Copyright (C) 2008, 2016 Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" References consulted:
Packit 7cfc04
.\"     Linux libc source code
Packit 7cfc04
.\"     Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
Packit 7cfc04
.\"     386BSD man pages
Packit 7cfc04
.\" Modified Sat Jul 24 16:09:49 1993 by Rik Faith (faith@cs.unc.edu)
Packit 7cfc04
.\" Modified 11 June 1995 by Andries Brouwer (aeb@cwi.nl)
Packit 7cfc04
.\" Modified 22 July 1996 by Andries Brouwer (aeb@cwi.nl)
Packit 7cfc04
.\" 2007-07-30 Ulrich Drepper <drepper@redhat.com>, mtk:
Packit 7cfc04
.\"     Rework discussion of nonstandard structure fields.
Packit 7cfc04
.\"
Packit 7cfc04
.TH READDIR 3  2017-09-15 "" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
readdir \- read a directory
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <dirent.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "struct dirent *readdir(DIR *" dirp );
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.BR readdir ()
Packit 7cfc04
function returns a pointer to a \fIdirent\fP structure
Packit 7cfc04
representing the next directory entry in the directory stream pointed
Packit 7cfc04
to by \fIdirp\fP.
Packit 7cfc04
It returns NULL on reaching the end of the directory stream or if
Packit 7cfc04
an error occurred.
Packit 7cfc04
.PP
Packit 7cfc04
In the glibc implementation, the
Packit 7cfc04
.I dirent
Packit 7cfc04
structure is defined as follows:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
struct dirent {
Packit 7cfc04
    ino_t          d_ino;       /* Inode number */
Packit 7cfc04
    off_t          d_off;       /* Not an offset; see below */
Packit 7cfc04
    unsigned short d_reclen;    /* Length of this record */
Packit 7cfc04
    unsigned char  d_type;      /* Type of file; not supported
Packit 7cfc04
                                   by all filesystem types */
Packit 7cfc04
    char           d_name[256]; /* Null-terminated filename */
Packit 7cfc04
};
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
The only fields in the
Packit 7cfc04
.I dirent
Packit 7cfc04
structure that are mandated by POSIX.1 are
Packit 7cfc04
.IR d_name
Packit 7cfc04
and
Packit 7cfc04
.IR d_ino .
Packit 7cfc04
The other fields are unstandardized, and not present on all systems;
Packit 7cfc04
see NOTES below for some further details.
Packit 7cfc04
.PP
Packit 7cfc04
The fields of the
Packit 7cfc04
.I dirent
Packit 7cfc04
structure are as follows:
Packit 7cfc04
.TP
Packit 7cfc04
.I d_ino
Packit 7cfc04
This is the inode number of the file.
Packit 7cfc04
.TP
Packit 7cfc04
.I d_off
Packit 7cfc04
The value returned in
Packit 7cfc04
.I d_off
Packit 7cfc04
is the same as would be returned by calling
Packit 7cfc04
.BR telldir (3)
Packit 7cfc04
at the current position in the directory stream.
Packit 7cfc04
Be aware that despite its type and name, the
Packit 7cfc04
.I d_off
Packit 7cfc04
field is seldom any kind of directory offset on modern filesystems.
Packit 7cfc04
.\" https://lwn.net/Articles/544298/
Packit 7cfc04
Applications should treat this field as an opaque value,
Packit 7cfc04
making no assumptions about its contents; see also
Packit 7cfc04
.BR telldir (3).
Packit 7cfc04
.TP
Packit 7cfc04
.I d_reclen
Packit 7cfc04
This is the size (in bytes) of the returned record.
Packit 7cfc04
This may not match the size of the structure definition shown above;
Packit 7cfc04
see NOTES.
Packit 7cfc04
.TP
Packit 7cfc04
.I d_type
Packit 7cfc04
This field contains a value indicating the file type,
Packit 7cfc04
making it possible to avoid the expense of calling
Packit 7cfc04
.BR lstat (2)
Packit 7cfc04
if further actions depend on the type of the file.
Packit 7cfc04
.IP
Packit 7cfc04
When a suitable feature test macro is defined
Packit 7cfc04
.RB ( _DEFAULT_SOURCE
Packit 7cfc04
on glibc versions since 2.19, or
Packit 7cfc04
.BR _BSD_SOURCE
Packit 7cfc04
on glibc versions 2.19 and earlier),
Packit 7cfc04
glibc defines the following macro constants for the value returned in
Packit 7cfc04
.IR d_type :
Packit 7cfc04
.RS
Packit 7cfc04
.TP 12
Packit 7cfc04
.B DT_BLK
Packit 7cfc04
This is a block device.
Packit 7cfc04
.TP
Packit 7cfc04
.B DT_CHR
Packit 7cfc04
This is a character device.
Packit 7cfc04
.TP
Packit 7cfc04
.B DT_DIR
Packit 7cfc04
This is a directory.
Packit 7cfc04
.TP
Packit 7cfc04
.B DT_FIFO
Packit 7cfc04
This is a named pipe (FIFO).
Packit 7cfc04
.TP
Packit 7cfc04
.B DT_LNK
Packit 7cfc04
This is a symbolic link.
Packit 7cfc04
.TP
Packit 7cfc04
.B DT_REG
Packit 7cfc04
This is a regular file.
Packit 7cfc04
.TP
Packit 7cfc04
.B DT_SOCK
Packit 7cfc04
This is a UNIX domain socket.
Packit 7cfc04
.TP
Packit 7cfc04
.B DT_UNKNOWN
Packit 7cfc04
The file type could not be determined.
Packit 7cfc04
.RE
Packit 7cfc04
.IP
Packit 7cfc04
Currently,
Packit 7cfc04
.\" kernel 2.6.27
Packit 7cfc04
.\" The same sentence is in getdents.2
Packit 7cfc04
only some filesystems (among them: Btrfs, ext2, ext3, and ext4)
Packit 7cfc04
have full support for returning the file type in
Packit 7cfc04
.IR d_type .
Packit 7cfc04
All applications must properly handle a return of
Packit 7cfc04
.BR DT_UNKNOWN .
Packit 7cfc04
.TP
Packit 7cfc04
.I d_name
Packit 7cfc04
This field contains the null terminated filename.
Packit 7cfc04
.IR "See NOTES" .
Packit 7cfc04
.PP
Packit 7cfc04
The data returned by
Packit 7cfc04
.BR readdir ()
Packit 7cfc04
may be overwritten by subsequent calls to
Packit 7cfc04
.BR readdir ()
Packit 7cfc04
for the same directory stream.
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success,
Packit 7cfc04
.BR readdir ()
Packit 7cfc04
returns a pointer to a
Packit 7cfc04
.I dirent
Packit 7cfc04
structure.
Packit 7cfc04
(This structure may be statically allocated; do not attempt to
Packit 7cfc04
.BR free (3)
Packit 7cfc04
it.)
Packit 7cfc04
.PP
Packit 7cfc04
If the end of the directory stream is reached, NULL is returned and
Packit 7cfc04
.I errno
Packit 7cfc04
is not changed.
Packit 7cfc04
If an error occurs, NULL is returned and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
To distinguish end of stream and from an error, set
Packit 7cfc04
.I errno
Packit 7cfc04
to zero before calling
Packit 7cfc04
.BR readdir ()
Packit 7cfc04
and then check the value of
Packit 7cfc04
.I errno
Packit 7cfc04
if NULL is returned.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B EBADF
Packit 7cfc04
Invalid directory stream descriptor \fIdirp\fP.
Packit 7cfc04
.SH ATTRIBUTES
Packit 7cfc04
For an explanation of the terms used in this section, see
Packit 7cfc04
.BR attributes (7).
Packit 7cfc04
.TS
Packit 7cfc04
allbox;
Packit 7cfc04
lb lb lb
Packit 7cfc04
l l l.
Packit 7cfc04
Interface	Attribute	Value
Packit 7cfc04
T{
Packit 7cfc04
.BR readdir ()
Packit 7cfc04
T}	Thread safety	MT-Unsafe race:dirstream
Packit 7cfc04
.TE
Packit 7cfc04
.sp 1
Packit 7cfc04
.PP
Packit 7cfc04
In the current POSIX.1 specification (POSIX.1-2008),
Packit 7cfc04
.BR readdir ()
Packit 7cfc04
is not required to be thread-safe.
Packit 7cfc04
However, in modern implementations (including the glibc implementation),
Packit 7cfc04
concurrent calls to
Packit 7cfc04
.BR readdir ()
Packit 7cfc04
that specify different directory streams are thread-safe.
Packit 7cfc04
In cases where multiple threads must read from the same directory stream,
Packit 7cfc04
using
Packit 7cfc04
.BR readdir ()
Packit 7cfc04
with external synchronization is still preferable to the use of the deprecated
Packit 7cfc04
.BR readdir_r (3)
Packit 7cfc04
function.
Packit 7cfc04
It is expected that a future version of POSIX.1
Packit 7cfc04
.\" FIXME .
Packit 7cfc04
.\" http://www.austingroupbugs.net/view.php?id=696
Packit 7cfc04
will require that
Packit 7cfc04
.BR readdir ()
Packit 7cfc04
be thread-safe when concurrently employed on different directory streams.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
Packit 7cfc04
.SH NOTES
Packit 7cfc04
A directory stream is opened using
Packit 7cfc04
.BR opendir (3).
Packit 7cfc04
.PP
Packit 7cfc04
The order in which filenames are read by successive calls to
Packit 7cfc04
.BR readdir ()
Packit 7cfc04
depends on the filesystem implementation;
Packit 7cfc04
it is unlikely that the names will be sorted in any fashion.
Packit 7cfc04
.PP
Packit 7cfc04
Only the fields
Packit 7cfc04
.I d_name
Packit 7cfc04
and (as an XSI extension)
Packit 7cfc04
.I d_ino
Packit 7cfc04
are specified in POSIX.1.
Packit 7cfc04
.\" POSIX.1-2001, POSIX.1-2008
Packit 7cfc04
Other than Linux, the
Packit 7cfc04
.I d_type
Packit 7cfc04
field is available mainly only on BSD systems.
Packit 7cfc04
The remaining fields are available on many, but not all systems.
Packit 7cfc04
Under glibc,
Packit 7cfc04
programs can check for the availability of the fields not defined
Packit 7cfc04
in POSIX.1 by testing whether the macros
Packit 7cfc04
.BR _DIRENT_HAVE_D_NAMLEN ,
Packit 7cfc04
.BR _DIRENT_HAVE_D_RECLEN ,
Packit 7cfc04
.BR _DIRENT_HAVE_D_OFF ,
Packit 7cfc04
or
Packit 7cfc04
.B _DIRENT_HAVE_D_TYPE
Packit 7cfc04
are defined.
Packit 7cfc04
.\"
Packit 7cfc04
.SS The d_name field
Packit 7cfc04
The
Packit 7cfc04
.I dirent
Packit 7cfc04
structure definition shown above is taken from the glibc headers,
Packit 7cfc04
and shows the
Packit 7cfc04
.I d_name
Packit 7cfc04
field with a fixed size.
Packit 7cfc04
.PP
Packit 7cfc04
.IR Warning :
Packit 7cfc04
applications should avoid any dependence on the size of the
Packit 7cfc04
.I d_name
Packit 7cfc04
field.
Packit 7cfc04
POSIX defines it as
Packit 7cfc04
.IR "char\ d_name[]",
Packit 7cfc04
a character array of unspecified size, with at most
Packit 7cfc04
.B NAME_MAX
Packit 7cfc04
characters preceding the terminating null byte (\(aq\\0\(aq).
Packit 7cfc04
.PP
Packit 7cfc04
POSIX.1 explicitly notes that this field should not be used as an lvalue.
Packit 7cfc04
The standard also notes that the use of
Packit 7cfc04
.I sizeof(d_name)
Packit 7cfc04
is incorrect; use
Packit 7cfc04
.IR strlen(d_name)
Packit 7cfc04
instead.
Packit 7cfc04
(On some systems, this field is defined as
Packit 7cfc04
.IR char\ d_name[1] !)
Packit 7cfc04
By implication, the use
Packit 7cfc04
.IR "sizeof(struct dirent)"
Packit 7cfc04
to capture the size of the record including the size of
Packit 7cfc04
.IR d_name
Packit 7cfc04
is also incorrect.
Packit 7cfc04
.PP
Packit 7cfc04
Note that while the call
Packit 7cfc04
.PP
Packit 7cfc04
    fpathconf(fd, _PC_NAME_MAX)
Packit 7cfc04
.PP
Packit 7cfc04
returns the value 255 for most filesystems,
Packit 7cfc04
on some filesystems (e.g., CIFS, Windows SMB servers),
Packit 7cfc04
the null-terminated filename that is (correctly) returned in
Packit 7cfc04
.I d_name
Packit 7cfc04
can actually exceed this size.
Packit 7cfc04
In such cases, the
Packit 7cfc04
.I d_reclen
Packit 7cfc04
field will contain a value that exceeds the size of the glibc
Packit 7cfc04
.I dirent
Packit 7cfc04
structure shown above.
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR getdents (2),
Packit 7cfc04
.BR read (2),
Packit 7cfc04
.BR closedir (3),
Packit 7cfc04
.BR dirfd (3),
Packit 7cfc04
.BR ftw (3),
Packit 7cfc04
.BR offsetof (3),
Packit 7cfc04
.BR opendir (3),
Packit 7cfc04
.BR readdir_r (3),
Packit 7cfc04
.BR rewinddir (3),
Packit 7cfc04
.BR scandir (3),
Packit 7cfc04
.BR seekdir (3),
Packit 7cfc04
.BR telldir (3)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.