Bug in current versions of /usr/include/dirent.h ?

andrew clarke mail at ozzmosis.com
Wed Apr 21 11:19:57 UTC 2021

On 2021-04-20 19:21:45, Frank Leonhardt (freebsd-doc at fjl.co.uk) wrote:

> Weird one this! I'm currently using 12.2, but this appears to be a problem
> in recent versions.
> In FreeBSD 8, dirent.h contains the following:
> /* structure describing an open directory. */
> typedef struct _dirdesc {
>         int     dd_fd;          /* file descriptor associated with directory
> */
>         long    dd_loc;         /* offset in current buffer */
>         long    dd_size;        /* amount of data returned by getdirentries
> */
>         char    *dd_buf;        /* data buffer */
>         int     dd_len;         /* size of data buffer */
>         long    dd_seek;        /* magic cookie returned by getdirentries */
>         long    dd_rewind;      /* magic cookie for rewinding */
>         int     dd_flags;       /* flags for readdir */
>         struct pthread_mutex    *dd_lock;       /* lock */
>         struct _telldir *dd_td; /* telldir position recording */
> } DIR;
> Nothing wrong there. It's the structure used by opendir() etc in the
> standard C library.

FWIW opendir() is a POSIX extension, not standard C.

> In 12.2 we've not got a structure definition, but instead a forward
> reference at line 87. DIR is typedefed on the following line. However,
> nowhere can I find where this forward reference is later resolved - meaning
> it isn't.
> Has anyone got the faintest idea what's going on? I've had a look through
> the source code to see where a DIR structure is used, and it may as well be
> a (void *) - it's used as a handle returned by opendir() in subsequent
> operations but never dereferenced.

POSIX doesn't guarantee DIR is a struct, just a "directory stream" type.

I suspect the FreeBSD folks have deprecated dereferencing DIR, perhaps in an effort to discourage people writing non-portable code.

See this commit from 2012:

"Hide DIR definition by making it an opaque struct typedef."


> And before anyone questions why de-referencing it is necessary - first off,
> it's always been a published structure so  it's fair game. Secondly, full
> access to the dd_fd field was handy. If access to this structure is
> "deprecated" after 40 years, at the very least the handle should have been
> typedefed into something safer than a forward reference.

There is the dirfd() function if you really need access to the file descriptor.

DIR *dir;
dir = opendir(".");
if (dir)
	int fd = dirfd(dir);


More information about the freebsd-questions mailing list