| 1 |
'\" t |
| 2 |
.\" Copyright 2002, Andries E. Brouwer <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 6 |
.\" |
| 7 |
.TH dirfd 3 2026-02-08 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
dirfd \- get directory stream file descriptor |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <sys/types.h> |
| 16 |
.B #include <dirent.h> |
| 17 |
.P |
| 18 |
.BI "int dirfd(DIR *" dirp ); |
| 19 |
.fi |
| 20 |
.P |
| 21 |
.RS -4 |
| 22 |
Feature Test Macro Requirements for glibc (see |
| 23 |
.BR feature_test_macros (7)): |
| 24 |
.RE |
| 25 |
.P |
| 26 |
.BR dirfd (): |
| 27 |
.nf |
| 28 |
/* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200809L |
| 29 |
|| /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE |
| 30 |
.fi |
| 31 |
.SH DESCRIPTION |
| 32 |
The function |
| 33 |
.BR dirfd () |
| 34 |
returns the file descriptor associated with the directory stream |
| 35 |
.IR dirp . |
| 36 |
.P |
| 37 |
This file descriptor is the one used internally by the directory stream. |
| 38 |
As a result, it is useful only for functions which do not depend on |
| 39 |
or alter the file position, such as |
| 40 |
.BR fstat (2) |
| 41 |
and |
| 42 |
.BR fchdir (2). |
| 43 |
It will be automatically closed when |
| 44 |
.BR closedir (3) |
| 45 |
is called. |
| 46 |
.SH RETURN VALUE |
| 47 |
On success, |
| 48 |
.BR dirfd () |
| 49 |
returns a file descriptor (a nonnegative integer). |
| 50 |
On error, \-1 is returned, and |
| 51 |
.I errno |
| 52 |
is set to indicate the error. |
| 53 |
.SH ERRORS |
| 54 |
POSIX.1-2008 specifies two errors, |
| 55 |
neither of which is returned by the current |
| 56 |
.\" glibc 2.8 |
| 57 |
implementation. |
| 58 |
.TP |
| 59 |
.B EINVAL |
| 60 |
.I dirp |
| 61 |
does not refer to a valid directory stream. |
| 62 |
.TP |
| 63 |
.B ENOTSUP |
| 64 |
The implementation does not support the association of a file |
| 65 |
descriptor with a directory. |
| 66 |
.SH ATTRIBUTES |
| 67 |
For an explanation of the terms used in this section, see |
| 68 |
.BR attributes (7). |
| 69 |
.TS |
| 70 |
allbox; |
| 71 |
lbx lb lb |
| 72 |
l l l. |
| 73 |
Interface Attribute Value |
| 74 |
T{ |
| 75 |
.na |
| 76 |
.nh |
| 77 |
.BR dirfd () |
| 78 |
T} Thread safety MT-Safe |
| 79 |
.TE |
| 80 |
.SH STANDARDS |
| 81 |
POSIX.1-2008. |
| 82 |
.SH HISTORY |
| 83 |
4.3BSD-Reno (not in 4.2BSD). |
| 84 |
.\" It is present in libc5 (since 5.1.2) and in glibc 2. |
| 85 |
.SH SEE ALSO |
| 86 |
.BR open (2), |
| 87 |
.BR openat (2), |
| 88 |
.BR closedir (3), |
| 89 |
.BR opendir (3), |
| 90 |
.BR readdir (3), |
| 91 |
.BR rewinddir (3), |
| 92 |
.BR scandir (3), |
| 93 |
.BR seekdir (3), |
| 94 |
.BR telldir (3) |