DIRENT(3)
π dirent β directory format π
bestie, this is dirent(3) from the Library Functions Manual (section 3), and itβs literally the directory format page. Weβre decoding it together. π«Ά
πΈ NAME πΈ
dirent β directory format. Thatβs it, thatβs the name. π
β¨ SYNOPSIS β¨
The synopsis is literally this, no cap:
#include <sys/types.h>
#include <sys/dirent.h>
mode
DTTOIF(dirtype);
dirtype
IFTODT(mode);
π¦ DESCRIPTION π¦
okay so bestie, directories are a convenient hierarchical way to group files while obscuring the underlying details of the storage medium. A directory file is differentiated from a plain file by a flag in its inode(5) entry. It consists of records (directory entries), each of which contains information about a file and a pointer to the file itself. Directory entries may contain other directories as well as plain files; such nested directories are referred to as subdirectories. A hierarchy of directories and files is formed in this manner and is called a file system (or referred to as a file system tree). π
Each directory file contains two special directory entries; one is a pointer to the directory itself called dot . and the other is a pointer to its parent directory called dot-dot ... Dot and dot-dot are valid pathnames, however, the system root directory / has no parent and dot-dot points to itself like dot. π
File system nodes are ordinary directory files on which has been grafted a file system object, such as a physical disk or a partitioned area of such a disk. (See mount(8).) π
π IMPLEMENTATION NOTES π
The directory entry format is defined in the file <sys/dirent.h>, which is also included by <dirent.h>. The format is represented by the dirent structure, which contains the following entries:
ino_t d_fileno;
uint16_t d_reclen;
uint16_t d_namlen;
uint8_t d_type;
char d_name[MAXNAMLEN + 1];
These are:
- The
d_filenoentry is a number which is unique for each distinct file in the filesystem. Files that are linked by hard links (seelink(2)) have the samed_fileno. Ifd_filenois zero, the entry refers to a deleted file. The typeino_tis defined in<sys/types.h>. π - The
d_reclenentry is the length, in bytes, of the directory record. - The
d_namlenentry specifies the length of the file name excluding theNUL. Thus the actual size ofd_namemay vary from 1 toMAXNAMLEN + 1. - The
d_typeis the type of the file. - The
d_nameentry contains aNUL-terminated file name.
The following list maps the types available for d_type to the corresponding ones used in the struct stat (see stat(2)), respectively:
DT_UNKNOWNβ stat:-β unknown file typeDT_FIFOβ stat:S_IFIFOβ named pipeDT_CHRβ stat:S_IFCHRβ character deviceDT_DIRβ stat:S_IFDIRβ directoryDT_BLKβ stat:S_IFBLKβ block deviceDT_REGβ stat:S_IFREGβ regular fileDT_LNKβ stat:S_IFLNKβ symbolic linkDT_SOCKβ stat:S_IFSOCKβ UNIX domain socketDT_WHTβ stat:S_IFWHTβ dummy βwhiteout inodeβ
The DT_WHT type is internal to the implementation and should not be seen in normal user applications. The macros DTTOIF() and IFTODT() can be used to convert from struct dirent types to struct stat types, and vice versa. π§
π₯Ί COMPATIBILITY π₯Ί
The IEEE Std 1003.1-2001 (βPOSIX.1β) standard specifies only the fields d_ino and d_name. The remaining fields are available on many, but not all systems. π
Furthermore, the standard leaves the size of d_name as unspecified, mentioning only that the number of bytes preceding the terminating NUL shall not exceed NAME_MAX. Because of this, and because the d_namlen field may not be present, a portable application should determine the size of d_name by using strlen(3) instead of applying the sizeof() operator. π«Ά
π«Ά SEE ALSO π«Ά
π HISTORY π
- A directory file format appeared in Version 1 AT&T UNIX. β
- The
direntstructure appeared in 4.3BSD-Reno. π
mwah! π this page is from the Debian version, dated September 7, 2019 β source/version and date, right there in the cutest little sign-off. π β¨
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 1983, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms