DIRFD(3P)
π DIRFD(3P) π
okay bestie, welcome to DIRFD(3P), straight out of the POSIX Programmer's Manual β¨ this one's a lil cutie from the 3P chapter and she's here to hand you a file descriptor like it's a friendship bracelet π«Ά
πΈ PROLOG πΈ
so like, real talk first: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so pls go check the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface might not even be implemented on Linux at all π no cap.
β¨ NAME β¨
dirfd β extract the file descriptor used by a DIR stream. it's giving "let me see what's underneath" π
π SYNOPSIS π
#include <dirent.h>
int dirfd(DIR *dirp);
that's it. that's the whole vibe. one include, one function, iconic π
π¦ DESCRIPTION π¦
okay so here's the tea β the dirfd() function shall return a file descriptor referring to the same directory as the dirp argument.
this file descriptor shall be closed by a call to closedir(). periodt.
β οΈ and now the scary part, bestie, listen up: if any attempt is made to close the file descriptor, or to modify the state of the associated description, other than by means ofclosedir(),readdir(),readdir_r(),rewinddir(), orseekdir(), the behavior is undefined. undefined behavior is not a vibe. do not do it. π
π RETURN VALUE π
- upon successful completion, the
dirfd()function shall return an integer which contains a file descriptor for the stream pointed to bydirp. slay β¨ - otherwise, it shall return
-1and shall seterrnoto indicate the error. sad girl hours π
π ERRORS π
the dirfd() function may fail if:
EINVALβ thedirpargument does not refer to a valid directory stream. like, that's not a real stream bestie, what are you doing π
the following sections are informative π (yes really, that's the actual line, we love a lil structural heads-up)
π EXAMPLES π
None.
(not me being dramatic about it β it literally just says none. the void. the silence. π)
π§ APPLICATION USAGE π§
so the dirfd() function is intended to be a mechanism by which an application may obtain a file descriptor to use for the fchdir() function. that's the whole point of her existence, and honestly? purpose-driven queen π
π RATIONALE π
okay storytime π this interface was introduced because the Base Definitions volume of POSIX.1β2017 does not make public the DIR data structure. so like, you can't just peek inside DIR yourself, no ma'am.
applications tend to use the fchdir() function on the file descriptor returned by this interface, and this has proven useful for security reasons β in particular, it is a better technique than others where directory names might change. names changing underneath you is the actual villain of this story fr fr π₯
also, tiny linguistic moment: the description uses the term a file descriptor''</hotpink> rather than <hotpink>the file descriptor''. the implication intended is that an implementation that does not use an fd for opendir() could still open() the directory to implement the dirfd() function. such a descriptor must be closed later during a call to closedir(). the way that wording was chosen with intentionβ¦ obsessed π
and if it is necessary to allocate an fd to be returned by dirfd(), it should be done at the time of a call to opendir(). timing is everything babes β°
β FUTURE DIRECTIONS β
None.
π SEE ALSO π
closedir()fchdir()fdopendir()fileno()open()readdir()
and also: the Base Definitions volume of POSIX.1β2017, <dirent.h> π
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.
In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
so yeah, that's the whole page bestie π mwah, take care of your file descriptors and never close them behind closedir()'s back π
with love from IEEE/The Open Group, 2017 β signing off, stay iconic β¨π
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms