Node:Reading/Closing Directory, Next:Simple Directory Lister, Previous:Opening a Directory, Up:Accessing Directories
This section describes how to read directory entries from a directory
stream, and how to close the stream when you are done with it. All the
symbols are declared in the header file dirent.h
.
struct dirent * readdir (DIR *dirstream) | Function |
This function reads the next entry from the directory. It normally
returns a pointer to a structure containing information about the file.
This structure is statically allocated and can be rewritten by a
subsequent call.
Portability Note: On some systems If there are no more entries in the directory or an error is detected,
|
int readdir_r (DIR *dirstream, struct dirent *entry, struct dirent **result) | Function |
This function is the reentrant version of readdir . Like
readdir it returns the next entry from the directory. But to
prevent conflicts between simultaneously running threads the result is
not stored in statically allocated memory. Instead the argument
entry points to a place to store the result.
The return value is If there are no more directory entries, Portability Note: On some systems It is also important to look at the definition of the union { struct dirent d; char b[offsetof (struct dirent, d_name) + NAME_MAX + 1]; } u; if (readdir_r (dir, &u.d, &res) == 0) ... |
To support large filesystems on 32-bit machines there are LFS variants of the last two functions.
struct dirent64 * readdir64 (DIR *dirstream) | Function |
The readdir64 function is just like the readdir function
except that it returns a pointer to a record of type struct
dirent64 . Some of the members of this data type (notably d_ino )
might have a different size to allow large filesystems.
In all other aspects this function is equivalent to |
int readdir64_r (DIR *dirstream, struct dirent64 *entry, struct dirent64 **result) | Function |
The readdir64_r function is equivalent to the readdir_r
function except that it takes parameters of base type struct
dirent64 instead of struct dirent in the second and third
position. The same precautions mentioned in the documentation of
readdir_r also apply here.
|
int closedir (DIR *dirstream) | Function |
This function closes the directory stream dirstream. It returns
0 on success and -1 on failure.
The following
|