getdents (2) - Linux Manuals
getdents: get directory entries
NAME
getdents, getdents64 - get directory entries
SYNOPSIS
long getdents(unsigned int fd, struct linux_dirent *dirp, unsigned int count); #define _GNU_SOURCE /* See feature_test_macros(7) */ #include <dirent.h> ssize_t getdents64(int fd, void *dirp, size_t count);
Note: There is no glibc wrapper for getdents(); see NOTES.
DESCRIPTION
These are not the interfaces you are interested in. Look at readdir(3) for the POSIX-conforming C library interface. This page documents the bare kernel system call interfaces.getdents()
The system call getdents() reads several linux_dirent structures from the directory referred to by the open file descriptor fd into the buffer pointed to by dirp. The argument count specifies the size of that buffer.The linux_dirent structure is declared as follows:
struct linux_dirent {
d_ino
is an inode number.
d_off
is the distance from the start of the directory to the start of the next
linux_dirent.
d_reclen
is the size of this entire
linux_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 (defined in
<dirent.h>):
The
d_type
field is implemented since Linux 2.6.4.
It occupies a space that was previously a zero-filled padding byte in the
linux_dirent
structure.
Thus, on kernels up to and including 2.6.3,
attempting to access this field always provides the value 0
(DT_UNKNOWN).
Currently,
only some filesystems (among them: Btrfs, ext2, ext3, and ext4)
have full support for returning the file type in
d_type.
All applications must properly handle a return of
DT_UNKNOWN.
The
getdents64()
system call is like
getdents(),
except that its second argument is a pointer to a buffer containing
structures of the following type:
struct linux_dirent64 {
Probably, you want to use
readdir(3)
instead of these system calls.
These calls supersede
readdir(2).
$ ./a.out /testfs/
--------------- nread=120 ---------------
inode# file type d_reclen d_off d_name
getdents64()
The original Linux
getdents()
system call did not handle large filesystems and large file offsets.
Consequently, Linux 2.4 added
getdents64(),
with wider types for the
d_ino
and
d_off
fields.
In addition,
getdents64()
supports an explicit
d_type
field.
RETURN VALUE
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.
ERRORS
CONFORMING TO
SVr4.
NOTES
Library support for
getdents64()
was added in glibc 2.30;
there is no glibc wrapper for
getdents().
Calling
getdents()
(or
getdents64()
on earlier glibc versions) requires the use of
syscall(2).
In that case you will need to define the
linux_dirent
or
linux_dirent64
structure yourself.
EXAMPLES
The program below demonstrates the use of
getdents().
The following output shows an example of what we see when running this
program on an ext2 directory: