GETDIRENTRIES(2)
๐ GETDIRENTRIES(2) โ ok so this is a System Calls Manual page, section 2, bestie ๐
โจ NAME โจ
getdirentries, getdents โ the girlies that get you directory entries in a file system independent format ๐๏ธโจ
๐ซถ LIBRARY ๐ซถ
Standard C Library (libc, -lc) โ she lives in libc, no cap.
๐ธ SYNOPSIS ๐ธ
#include <sys/types.h>
#include <dirent.h>
ssize_t
getdirentries(int fd, char *buf, size_t nbytes, off_t *basep);
ssize_t
getdents(int fd, char *buf, size_t nbytes);
๐ DESCRIPTION ๐
Okay so picture this: the getdirentries() and getdents() system calls read directory entries from the directory that fd points at, and dump them into the buffer pointed to by buf, in a file system independent format. Iconic behaviour honestly. Up to nbytes of data gets transferred. โจ
Watch out though โ nbytes has to be greater than or equal to the block size associated with the file (peep stat(2) for that). Some file systems lowkey may not support these system calls at all if your buffer is smaller than that size, so likeโฆ don't play with it. ๐
The data in the buffer is a whole series of dirent structures, each one containing these entries:
ino_t d_fileno;
off_t d_off;
uint16_t d_reclen;
uint8_t d_type;
uint16_t d_namlen;
char d_name[MAXNAMLEN + 1]; /* see below */
Let's break down the cast, because each field is a main character ๐ฌ:
d_filenoโ a number that's unique for each distinct file in the file system. Files linked by hard links (seelink(2)) share the samed_fileno. Sibling energy fr fr.d_offโ returns a cookie which, if non-zero, can be used withlseek(2)to position the directory descriptor to the next entry. ๐ชd_reclenโ the length, in bytes, of the directory record.d_typeโ the type of the file pointed to by the directory record. The file type values are defined in<sys/dirent.h>.d_nameโ contains a null terminated file name.d_namlenโ specifies the length of the file name excluding the null byte. So the actual size ofd_namemay vary from 1 toMAXNAMLEN+ 1. She's flexible ๐ .
Entries may be separated by extra space โ don't freak out. You can use the d_reclen entry as an offset from the start of a dirent structure to the next structure, if any exists. ๐ฆ
The actual number of bytes transferred is what gets returned. The current position pointer associated with fd is set to point to the next block of entries. But heads up: the pointer may not advance by the number of bytes returned by getdirentries() or getdents(). And a value of zero is returned when the end of the directory has been reached โ that's the "we're done here" signal ๐.
If the basep pointer value is non-NULL, the getdirentries() system call writes the position of the block read into the location pointed to by basep. Alternatively, the current position pointer may be set and retrieved by lseek(2). And like, the current position pointer should only be set to one of these values:
- a value returned by
lseek(2) - a value returned in the location pointed to by
basep(getdirentries()only) - a value returned in the
d_offfield, if it is non-zero - or zero
โญ That last bit is a vibe check, not a suggestion. Set your position pointer to anything else and you're on your own, bestie. ๐ญ
๐ง IMPLEMENTATION NOTES ๐ง
The d_off field is currently set to 0 by the NFS client, because the directory offset cookies returned by an NFS server cannot be used by lseek(2) at this time. So likeโฆ it's giving nothing. Zero. Empty. On purpose. ๐
๐ฅ RETURN VALUES ๐ฅ
If successful, the number of bytes actually transferred is returned โ slay ๐. Otherwise, -1 is returned and the global variable errno is set to indicate the error. ๐
๐ ERRORS ๐
The getdirentries() system call will fail if:
[EBADF]โ Thefdargument is not a valid file descriptor open for reading. Not her reading era, apparently.[EFAULT]โ Eitherbufor non-NULLbaseppoint outside the allocated address space. Rude.[EINVAL]โ The value ofnbytesis too small for returning a directory entry or block of entries, or the current position pointer is invalid.[EIO]โ An I/O error occurred while reading from or writing to the file system. ๐ญ[EINTEGRITY]โ Corrupted data was detected while reading from the file system. Crunchy. Not in a good way.[ENOENT]โ Directory unlinked but still open. She's ghosting but also still here. ๐ซถ[ENOTDIR]โ The file referenced byfdis not a directory. Wrong main character, wrong movie. ๐
๐ฆ SEE ALSO ๐ฆ
lseek(2), open(2), directory(3), dir(5) โ go read these next, iykyk ๐
โญ HISTORY โญ
The getdirentries() system call first appeared in 4.4BSD. The getdents() system call first appeared in FreeBSD 3.0. Vintage girlies, both of them โจ
and that's the whole page, bestie!! this one was from the Debian fam, dated July 8, 2025 โ mwah, go read some directories ๐๐๐ฆ
๐ฆ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 1989, 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