GETDENTS(2) Linux Programmer's Manual GETDENTS(2)

getdents - get directory entries

#include <unistd.h>
#include <linux/types.h>
#include <linux/dirent.h>
#include <linux/unistd.h>
#include <errno.h>
int getdents(unsigned int fd, struct dirent *dirp,
             unsigned int count);

This is not the function you are interested in. Look at readdir(3) for the POSIX conforming C library interface. This page documents the bare kernel system call interface.

The system call getdents() reads several dirent structures from the directory referred to by the open file descriptor fd into the buffer pointed to by dirp. The argument count is the size of the memory area.

The dirent structure is declared as follows:


struct linux_dirent {

    unsigned long  d_ino;     /* Inode number */

    unsigned long  d_off;     /* Offset to next dirent */

    unsigned short d_reclen;  /* Length of this dirent */

    char           d_name []; /* Filename (null-terminated) */

                        /* length is actually (d_reclen - 2 -

                           offsetof(struct linux_dirent, d_name) */

    char           pad;       /* Zero padding byte */

    char           d_type;    /* File type (only since Linux 2.6.4;

                                 offset is (d_reclen - 1)) */
}

d_ino is an inode number. d_off is the distance from the start of the directory to the start of the next dirent. d_reclen is the size of this entire dirent. d_name is a null-terminated filename.

d_type is a byte at the end of the structure that indicates the file type. It contains one of the following values:

This is a block device.
This is a character device.
This is a directory.
This is a named pipe (FIFO).
This is a symbolic link.
This is a regular file.
This is a Unix domain socket.
The file type is unknown.

On success, the number of bytes read is returned. On end of directory, 0 is returned. On error, -1 is returned, and errno is set appropriately.

Invalid file descriptor fd.
Argument points outside the calling process's address space.
Result buffer is too small.
No such directory.
File descriptor does not refer to a directory.

SVr4.

Glibc does not provide a wrapper for this system call; call it using syscall(2).

This call supersedes readdir(2).

readdir(2), readdir(3)

This page is part of release 3.09 of the Linux man-pages project. A description of the project, and information about reporting documentation bugs, can be found at http://www.kernel.org/doc/man-pages/.

2008-06-22 Linux

Different Versions of this Page: