Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
.\"	$NetBSD: directory.3,v 1.43 2021/02/17 23:51:04 dholland Exp $
.\"
.\" Copyright (c) 1983, 1991, 1993
.\"	The Regents of the University of California.  All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 3. Neither the name of the University nor the names of its contributors
.\"    may be used to endorse or promote products derived from this software
.\"    without specific prior written permission.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\"     @(#)directory.3	8.1 (Berkeley) 6/4/93
.\"
.Dd February 17, 2021
.Dt DIRECTORY 3
.Os
.Sh NAME
.Nm fdopendir ,
.Nm opendir ,
.Nm readdir ,
.Nm readdir_r ,
.Nm telldir ,
.Nm seekdir ,
.Nm rewinddir ,
.Nm closedir ,
.Nm dirfd
.Nd directory operations
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In dirent.h
.Ft DIR *
.Fn opendir "const char *filename"
.Ft DIR *
.Fn fdopendir "int fd"
.Ft struct dirent *
.Fn readdir "DIR *dirp"
.Ft int
.Fn readdir_r "DIR * restrict dirp" "struct dirent * restrict entry" "struct dirent ** restrict result"
.Ft long
.Fn telldir "DIR *dirp"
.Ft void
.Fn seekdir "DIR *dirp" "long loc"
.Ft void
.Fn rewinddir "DIR *dirp"
.Ft int
.Fn closedir "DIR *dirp"
.Ft int
.Fn dirfd "DIR *dirp"
.Sh DESCRIPTION
The type
.Vt DIR
represents a directory stream;
an ordered sequence of all directory entries in a particular directory.
The purpose of the
.Vt DIR
structure is similar to that of the
.Vt FILE
structure maintained by the
.Xr stdio 3
library functions.
.Sh FUNCTIONS
The following standard directory operations are defined.
.Bl -tag -width XXX
.It Fn opendir "filename"
The
.Fn opendir
function opens the directory named by
.Fa filename
and associates a directory stream with it.
The directory stream is positioned at the first entry.
Upon successful completion, a pointer to
.Vt DIR
type is returned.
Otherwise,
.Fn opendir
returns
.Dv NULL .
.It Fn fdopendir "fd"
The
.Fn fdopendir
function associates a directory stream with the directory file descriptor
.Fa fd .
The file offset associated with
.Fa fd
at the time of the call determines which entries are returned.
.Pp
Upon failure,
.Fn fdopendir
returns
.Dv NULL .
Otherwise 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
.Fn closedir ,
.Fn readdir ,
.Fn readdir_r ,
.Fn rewinddir ,
the behavior is undefined.
The file descriptor can be closed by calling
.Fn closedir .
.It Fn readdir "dirp"
The
.Fn readdir
function returns a pointer to the directory entry at the current position
in the directory stream specified by
.Fa dirp ,
and positions the directory stream at the next entry.
It returns
.Dv NULL
upon reaching the end of the directory or detecting an invalid
.Fn seekdir
operation.
The returned structure is described in
.Xr dirent 3 .
.Pp
The returned pointer to the
.Em dirent
structure points to data which may be overwritten by another call to
.Fn readdir
on the same directory stream.
This data is not however overwritten by another call to
.Fn readdir
on a different directory stream.
.It Fn readdir_r "dirp" "entry" "result"
The
.Fn readdir_r
function
provides the same functionality as
.Fn readdir ,
but the caller must provide a directory
.Fa entry
buffer to store the results in.
If the read succeeds,
.Fa result
is pointed at the
.Fa entry ;
upon reaching the end of the directory
.Fa result
is set to
.Dv NULL .
The
.Fn readdir_r
function
returns 0 on success or an error number to indicate failure.
.Pp
Like
.Fn readdir ,
the
.Fn readdir_r
function may buffer several directory entries per actual read operation.
Both functions mark for update the
.Em st_atime
field (see
.Xr stat 2 )
of the directory each time the directory is actually read.
.It Fn telldir "dirp"
The
.Fn telldir
function returns the current location associated
with the directory stream specified by
.Fa dirp .
.Pp
If the most recent operation on the particular directory stream was a
.Fn seekdir ,
the directory position returned from
.Fn telldir
is the same as
.Fa loc
supplied as an argument to the
.Fn seekdir
call.
.It Fn seekdir "dirp" "loc"
The
.Fn seekdir
function sets the position of the next
.Fn readdir
operation on the directory stream specified by
.Fa dirp .
The value of
.Fa loc
should come from a previous call to
.Fn telldir
using the same directory stream.
.Pp
The new position reverts to the one associated
with the directory stream when the
.Fn telldir
operation was performed.
Values returned by
.Fn telldir
are good only for the lifetime of the
.Vt DIR
pointer,
.Fa dirp ,
from which they are derived.
If the directory is closed and then reopened, the
.Fn telldir
value cannot be re-used.
.It Fn rewinddir "dirp"
The
.Fn rewinddir
function resets the position of the named directory
stream to the beginning of the directory.
It also causes the directory stream to refer to the
current state of the corresponding directory, as if a call to
.Fn opendir
was made.
It is not specified whether this refers to the ``corresponding directory''
by name or by underlying object.
(These can differ if
.Xr rename 2
has been used.)
.\" Note: currently the underlying fd is reopened if and only if
.\" __DTF_READALL is in effect, which is true for union mounts and
.\" nfs; documenting that exactly seems inadvisable since it might
.\" change.  -- dholland 20210217
.Pp
If
.Fa dirp
does not refer to a valid directory stream, the behavior is undefined.
.It Fn closedir "dirp"
The
.Fn closedir
function closes the directory stream
and frees the structure associated with the
.Fa dirp
pointer,
returning 0 on success and \-1 on failure.
.It Fn dirfd "dirp"
The
.Fn dirfd
function returns the integer file descriptor
associated with the directory stream specified by
.Fa dirp .
Upon failure,
.Fn dirfd
returns \-1.
The returned file descriptor should not be closed by
.Xr close 2 ,
it will be released when
.Fa dirp
is closed with
.Fn closedir .
.Pp
The rationale of
.Fn dirfd
is to provide a mechanism by which a file descriptor
can be obtained for the use of the
.Xr fchdir 2
function.
.El
.Sh EXAMPLES
Sample code which searches a directory for entry
.Dq name
is:
.Bd -literal -offset indent
len = strlen(name);
dirp = opendir(".");
if (dirp != NULL) {
	while ((dp = readdir(dirp)) != NULL)
		if (dp->d_namlen == len &&
		    !strcmp(dp->d_name, name)) {
			(void)closedir(dirp);
			return (FOUND);
		}
	(void)closedir(dirp);
}
return (NOT_FOUND);
.Ed
.Sh COMPATIBILITY
The described directory operations have traditionally been problematic
in terms of portability.
A good example is the semantics of
.Sq \&.
(dot) and
.Sq \&..
(dot-dot).
Based on historical implementations,
the rules about file descriptors apply to directory streams as well.
The
.St -p1003.1-2008
standard no longer mandates that directory streams be
implemented by using file descriptors.
.Pp
The following additional remarks can be noted from the
.St -p1003.1-2008
standard.
.Bl -bullet -offset 2n
.It
If the type
.Vt DIR
is implemented using a file descriptor,
like in
.Nx ,
applications should be able to open only
.Dv OPEN_MAX
files and directories.
Otherwise the limit is left as unspecified.
.It
When a file descriptor is used to implement the directory stream, the
.Fn closedir
function behaves as if the
.Dv FD_CLOEXEC
had been set for the file descriptor.
In other words, it is mandatory that
.Fn closedir
deallocates the file descriptor.
.It
If directory streams are not implemented by using file descriptors,
functions such as
.Fn dirfd
may fail with
.Er ENOTSUP .
.It
If a file is removed from or added to the directory
after the most recent call to
.Fn opendir
or
.Fn rewinddir ,
it is unspecified whether a subsequent call to
.Fn readdir
returns an entry for that file.
.It
When using the function
.Fn seekdir ,
note that if the value of
.Fa loc
was not obtained from an earlier call to
.Fn telldir ,
or if a call to
.Fn rewinddir
occurred between the calls to
.Fn telldir
and
.Fn seekdir ,
the results of any subsequent call to
.Fn readdir
are unspecified, possibly resulting in undefined behavior.
.It
After a call to
.Xr fork 2 ,
either the parent or child (but not both) can continue processing the
directory stream using
.Fn readdir ,
.Fn rewinddir ,
or
.Fn seekdir .
However, if both the parent and child processes use these functions,
the result is undefined.
.El
.Sh ERRORS
.\"
.\" XXX: The errors should be enumerated.
.\"
All described functions may set
.Vt errno
to indicate the error.
.Sh SEE ALSO
.Xr close 2 ,
.Xr lseek 2 ,
.Xr open 2 ,
.Xr read 2 ,
.Xr dirent 3
.Sh STANDARDS
The
.Fn opendir ,
.Fn readdir ,
.Fn rewinddir
and
.Fn closedir
functions conform to
.St -p1003.1-90 .
The other functions conform to
.St -p1003.1-2008 .
.Sh HISTORY
The
.Fn opendir ,
.Fn readdir ,
.Fn telldir ,
.Fn seekdir ,
.Fn rewinddir ,
.Fn closedir ,
and
.Fn dirfd
functions appeared in
.Bx 4.2 .
The
.Fn fdopendir
function appeared in
.Nx 6.0 .