__readdir2() — Read directory entry and get file information

Standards

Standards / Extensions C or C++ Dependencies

z/OS® UNIX

both OS/390 V2R6

Format

#define _OPEN_SYS_DIR_EXT
#include <dirent.h>

struct dirent *__readdir2(DIR *dir, struct stat *info);

General description

The __readdir2() function returns a pointer to a dirent structure describing the next directory entry in the directory stream associated with dir.

A dirent structure contains the character pointer d_name, which points to a string that gives the name of a file in the directory. This string ends in a terminating NULL, and has a maximum of NAME_MAX characters.

The info argument points to an area of storage that will be filled in with information about the file d_name. This information is returned in a stat structure defined in the sys/stat.h header file. The format of this structure is described in the topic about the lstat() function. If info is NULL, no stat information is passed back.

If entries for dot or dot-dot exist, one entry will be returned for dot and one entry will be returned for dot-dot; otherwise they will not be returned.

A call to __readdir2() overwrites data produced by a previous call to __readdir2() or readdir() on the same directory stream. Calls for different directory streams do not overwrite each other's data.

Save the dirent data from __readdir2(), if required, before calling closedir(), because closedir() frees the dirent data.

The __readdir2() function may buffer several directory entries per actual read operation. __readdir2() updates the st_atime (access time) field of the directory each time the directory is actually read.

After a call to fork(), either the parent or child (but not both) may continue processing the directory stream using __readdir2(), readdir(), rewinddir() or seekdir(). If both the parent and child processes use these functions, the result is undefined.

If the entry names a symbolic link, the value of d_ino member in dirent structure is unspecified.

Unpredictable results can occur if closedir() is used to close the the directory stream before __readdir2() is called. If the contents of a directory have changed since the directory was opened (files added or removed), a call should be made to rewinddir() so that subsequent _readdir2() requests can read the new contents.

The output from this function is similar to a combination of readdir() and lstat(). In some cases, certain information in the output stat structure differs from what lstat() would return. Also, the d_extra field in dir is always NULL for __readdir2().

Returned value

If successful, __readdir2() returns a pointer to a dirent structure describing the next directory entry in the directory stream. When __readdir2() reaches the end of the directory stream, it returns a NULL pointer.

If unsuccessful, __readdir2() returns a NULL pointer and sets errno to one of the following values:
Error Code
Description
EBADF
dir does not yield an open directory stream.
EINVAL
The buffer was too small to contain any directories.
ELOOP
A loop exists in symbolic links. This error occurs if the number of symbolic links in a file name in the directory is greater than POSIX_SYMLOOP.
ENOENT
The current position of the directory stream is invalid.

Related information