readdir(3) Library Functions Manual readdir(3) readdir - LIBRARY Standard C library (libc, -lc) #include struct dirent *readdir(DIR *dirp); readdir() dirent, , dirp. NULL . glibc dirent : struct dirent { ino_t d_ino; /* */ off_t d_off; /* , */ unsigned short d_reclen; /* */ unsigned char d_type; /* ; */ char d_name[256]; /* null */ }; POSIX.1, dirent d_name[] d_ino. ; . dirent: d_ino . d_off , d_off, telldir(3) . , , d_off . , ( ) ; telldir(3). d_reclen ( ) . , ; . d_type , lstat(2), . When a suitable feature test macro is defined (_DEFAULT_SOURCE since glibc 2.19, or _BSD_SOURCE on glibc 2.19 and earlier), glibc defines the following macro constants for the value returned in d_type: DT_BLK DT_CHR DT_DIR DT_FIFO (FIFO) DT_LNK DT_REG DT_SOCK UNIX DT_UNKNOWN . , ( : Btrfs, ext2, ext3 ext4) d_type. DT_UNKNOWN. d_name null. . , readdir(), readdir() . readdir() dirent ( ; free(3)). If the end of the directory stream is reached, NULL is returned and errno is not changed. If an error occurs, NULL is returned and errno is set to indicate the error. To distinguish end of stream from an error, set errno to zero before calling readdir() and then check the value of errno if NULL is returned. EBADF dirp. attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |readdir() | | MT-Unsafe race:dirstream | +----------------------------+----------------------------------------------------------+--------------------------+ POSIX.1 (POSIX.1-2008), readdir() . ( glibc) readdir() . , , readdir() , readdir_r(3). , POSIX.1 readdir() . POSIX.1 d_name d_ino ( XSI). Linux, d_type , BSD. , . glibc , POSIX.1, _DIRENT_HAVE_D_NAMLEN, _DIRENT_HAVE_D_RECLEN, _DIRENT_HAVE_D_OFF _DIRENT_HAVE_D_TYPE. d_name dirent, , glibc d_name . Warning: applications should avoid any dependence on the size of the d_name field. POSIX defines it as char d_name[], a character array of unspecified size, with at most NAME_MAX characters preceding the terminating null byte ('\0'). POSIX.1 explicitly notes that this field should not be used as an lvalue. The standard also notes that the use of sizeof(d_name) is incorrect; use strlen(d_name) instead. (On some systems, this field is defined as char d_name[1]!) By implication, the use sizeof(struct dirent) to capture the size of the record including the size of d_name is also incorrect. , fpathconf(fd, _PC_NAME_MAX) 255 , (, CIFS, Windows SMB) null , () d_name, . d_reclen , glibc dirent, . POSIX.1-2008. POSIX.1-2001, SVr4, 4.3BSD. opendir(3). readdir() ; , . getdents(2), read(2), closedir(3), dirfd(3), ftw(3), offsetof(3), opendir(3), readdir_r(3), rewinddir(3), scandir(3), seekdir(3), telldir(3) aereiae , Azamat Hackimov , Dmitriy S. Seregin , Katrin Kutepova , Lockal , Yuri Kozlov , ; GNU (GNU General Public License - GPL, 3 ) , - . - , , <>. Linux man-pages 6.8 2 2024 . readdir(3)