mount_setattr.2: Minor tweaks to Christian's patch
[man-pages.git] / man3 / readdir_r.3
blobc53adafe4cf625c9d029e46804480f596d9f1791
1 .\" Copyright (C) 2008, 2016 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 2016 Florian Weimer <fweimer@redhat.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH READDIR_R 3  2021-03-22 "" "Linux Programmer's Manual"
27 .SH NAME
28 readdir_r \- read a directory
29 .SH SYNOPSIS
30 .nf
31 .B #include <dirent.h>
32 .PP
33 .BI "int readdir_r(DIR *restrict " dirp ", struct dirent *restrict " entry ,
34 .BI "              struct dirent **restrict " result );
35 .fi
36 .PP
37 .RS -4
38 Feature Test Macro Requirements for glibc (see
39 .BR feature_test_macros (7)):
40 .RE
41 .PP
42 .BR readdir_r ():
43 .nf
44     _POSIX_C_SOURCE
45         || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
46 .fi
47 .SH DESCRIPTION
48 This function is deprecated; use
49 .BR readdir (3)
50 instead.
51 .PP
52 The
53 .BR readdir_r ()
54 function was invented as a reentrant version of
55 .BR readdir (3).
56 It reads the next directory entry from the directory stream
57 .IR dirp ,
58 and returns it in the caller-allocated buffer pointed to by
59 .IR entry .
60 For details of the
61 .IR dirent
62 structure, see
63 .BR readdir (3).
64 .PP
65 A pointer to the returned buffer is placed in
66 .IR *result ;
67 if the end of the directory stream was encountered,
68 then NULL is instead returned in
69 .IR *result .
70 .PP
71 It is recommended that applications use
72 .BR readdir (3)
73 instead of
74 .BR readdir_r ().
75 Furthermore, since version 2.24, glibc deprecates
76 .BR readdir_r ().
77 The reasons are as follows:
78 .IP * 3
79 On systems where
80 .BR NAME_MAX
81 is undefined, calling
82 .BR readdir_r ()
83 may be unsafe because the interface does not allow the caller to specify
84 the length of the buffer used for the returned directory entry.
85 .IP *
86 On some systems,
87 .BR readdir_r ()
88 can't read directory entries with very long names.
89 When the glibc implementation encounters such a name,
90 .BR readdir_r ()
91 fails with the error
92 .B ENAMETOOLONG
93 .IR "after the final directory entry has been read" .
94 On some other systems,
95 .BR readdir_r ()
96 may return a success status, but the returned
97 .IR d_name
98 field may not be null terminated or may be truncated.
99 .IP *
100 In the current POSIX.1 specification (POSIX.1-2008),
101 .BR readdir (3)
102 is not required to be thread-safe.
103 However, in modern implementations (including the glibc implementation),
104 concurrent calls to
105 .BR readdir (3)
106 that specify different directory streams are thread-safe.
107 Therefore, the use of
108 .BR readdir_r ()
109 is generally unnecessary in multithreaded programs.
110 In cases where multiple threads must read from the same directory stream,
111 using
112 .BR readdir (3)
113 with external synchronization is still preferable to the use of
114 .BR readdir_r (),
115 for the reasons given in the points above.
116 .IP *
117 It is expected that a future version of POSIX.1
118 .\" FIXME .
119 .\" http://www.austingroupbugs.net/view.php?id=696
120 will make
121 .BR readdir_r ()
122 obsolete, and require that
123 .BR readdir (3)
124 be thread-safe when concurrently employed on different directory streams.
125 .SH RETURN VALUE
127 .BR readdir_r ()
128 function returns 0 on success.
129 On error, it returns a positive error number (listed under ERRORS).
130 If the end of the directory stream is reached,
131 .BR readdir_r ()
132 returns 0, and returns NULL in
133 .IR *result .
134 .SH ERRORS
136 .B EBADF
137 Invalid directory stream descriptor \fIdirp\fP.
139 .B ENAMETOOLONG
140 A directory entry whose name was too long to be read was encountered.
141 .SH ATTRIBUTES
142 For an explanation of the terms used in this section, see
143 .BR attributes (7).
144 .ad l
147 allbox;
148 lbx lb lb
149 l l l.
150 Interface       Attribute       Value
152 .BR readdir_r ()
153 T}      Thread safety   MT-Safe
157 .sp 1
158 .SH CONFORMING TO
159 POSIX.1-2001, POSIX.1-2008.
160 .SH SEE ALSO
161 .BR readdir (3)