READDIR(3) Linux Programmer's Manual READDIR(3)
NAME
readdir - read a directory
SYNOPSIS
#include <sys/types.h>
#include <dirent.h>
struct dirent *readdir(DIR *dir);
DESCRIPTION
The readdir() function returns a pointer to a dirent structure repre-
senting the next directory entry in the directory stream pointed to by
dir. It returns NULL on reaching the end-of-file or if an error
occurred.
On Linux, the dirent structure is defined as follows:
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file */
char d_name[256]; /* filename */
};
According to POSIX, the dirent structure contains a field char d_name[]
of unspecified size, with at most NAME_MAX characters preceding the
terminating null byte. POSIX.1-2001 also documents the field ino_t
d_ino as an XSI extension. Use of other fields will harm the portabil-
ity of your programs.
The data returned by readdir() may be overwritten by subsequent calls
to readdir() for the same directory stream.
RETURN VALUE
The readdir() function returns a pointer to a dirent structure, or NULL
if an error occurs or end-of-file is reached. On error, errno is set
appropriately.
ERRORS
EBADF Invalid directory stream descriptor dir.
CONFORMING TO
SVr4, 4.3BSD, POSIX.1-2001
SEE ALSO
read(2), closedir(3), dirfd(3), ftw(3), opendir(3), rewinddir(3), scan-
dir(3), seekdir(3), telldir(3)
1996-04-22 READDIR(3)
OPENDIR(3) Linux Programmer's Manual OPENDIR(3)
NAME
opendir - open a directory
SYNOPSIS
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
DESCRIPTION
The opendir() function opens a directory stream corresponding to the
directory name, and returns a pointer to the directory stream. The
stream is positioned at the first entry in the directory.
RETURN VALUE
The opendir() function returns a pointer to the directory stream. On
error, NULL is returned, and errno is set appropriately.
ERRORS
EACCES Permission denied.
EMFILE Too many file descriptors in use by process.
ENFILE Too many files are currently open in the system.
ENOENT Directory does not exist, or name is an empty string.
ENOMEM Insufficient memory to complete the operation.
ENOTDIR
name is not a directory.
NOTES
The underlying file descriptor of the directory stream can be obtained
using dirfd(3).
CONFORMING TO
SVr4, 4.3BSD, POSIX.1-2001.
SEE ALSO
open(2), closedir(3), dirfd(3), readdir(3), rewinddir(3), scandir(3),
seekdir(3), telldir(3)
1995-06-11 OPENDIR(3)
And some code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
int main()
{
DIR *dir;
dirent *item;
dir = opendir("/home/draco/");
if ( dir == NULL )
{
printf("Error opening directory!\n");
exit(1);
}
while ( (item = readdir(dir)) != NULL )
{
printf(item->d_name);
printf("\n");
}
closedir(dir);
return 0;
}