dirfd Subroutine

Purpose

Extracts the file descriptor used by a DIR stream.

Library

Standard C Library (libc.a)

Syntax

#include <dirent.h>
int dirfd(dirp);
DIR *dirp;

Description

The dirfd subroutine returns a file descriptor that refers to the directory pointed to by the dirp argument. This file descriptor is closed by a call to the closedir subroutine. If an attempt is made to close the file descriptor, and to modify the state of the associated description, other than through the closedir, readdir, readdir_r, or rewinddir subroutines, the behavior is undefined.

Return Values

If successful, the dirfd subroutine returns an integer that contains a file descriptor for the stream pointed to by dirp argument. Otherwise, the dirfd subroutine returns -1 and sets the errno global variable to indicate the error.

Error Codes

The dirfd subroutine might fail if the following is true:

Item Description
EINVAL The dirp argument does not refer to a valid directory stream.
ENOTSUP The implementation does not support the association of a file descriptor with a directory.