GETDIRENTRIES(2)

๐Ÿ“ฆ
version 15.1 ยท 2 versions
, in freebsd 15.1

๐Ÿ’… 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 (see link(2)) share the same d_fileno. Sibling energy fr fr.
  • d_off โ€” returns a cookie which, if non-zero, can be used with lseek(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 of d_name may vary from 1 to MAXNAMLEN + 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_off field, 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] โ€” The fd argument is not a valid file descriptor open for reading. Not her reading era, apparently.
  • [EFAULT] โ€” Either buf or non-NULL basep point outside the allocated address space. Rude.
  • [EINVAL] โ€” The value of nbytes is 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 by fd is 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms