Scroll to navigation

readdir(3) Library Functions Manual readdir(3)

NOM

readdir - Consulter un répertoire

BIBLIOTHÈQUE

Bibliothèque C standard (libc-lc)

SYNOPSIS

#include <dirent.h>
struct dirent *readdir(DIR *dirp);

DESCRIPTION

La fonction readdir() renvoie un pointeur sur une structure dirent représentant l'entrée suivante du flux répertoire pointé par dirp. Elle renvoie NULL à la fin du répertoire ou en cas d'erreur.

Dans l'implémentation de la glibc, la structure dirent est définie comme suit :


struct dirent {

ino_t d_ino; /* numéro d'inœud */
off_t d_off; /* pas une position ; consultez NOTES */
unsigned short d_reclen; /* longueur de cet enregistrement */
unsigned char d_type; /* type du fichier ; pas pris en
charge par tous les types de
système de fichiers */
char d_name[256]; /* nom du fichier terminé par l'octet NULL */ };

The only fields in the dirent structure that are mandated by POSIX.1 are .d_name and .d_ino. The other fields are unstandardized, and not present on all systems; see VERSIONS.

Les champs de la structure dirent sont les suivants :

.d_ino
This is the inode number of the file in the filesystem containing the directory on which readdir() was called. If the directory entry is a mount point, then .d_ino differs from .st_ino returned by stat(2) on this file: .d_ino is the inode number of the mount point, while .st_ino is the inode number of the root directory of the mounted filesystem.
.d_off
The value returned in .d_off is the same as would be returned by calling telldir(3) at the current position in the directory stream. Be aware that despite its type and name, the .d_off field is seldom any kind of directory offset on modern filesystems. Applications should treat this field as an opaque value, making no assumptions about its contents; see also telldir(3).
.d_reclen
La taille (en octets) de l'enregistrement renvoyé. Elle peut ne pas correspondre à la taille de la définition de la structure montrée plus haut ; voir VERSIONS.
.d_type
Ce champ contient une valeur indiquant le type du fichier, permettant d'éviter le coût d'un appel à lstat(2) si d'autres actions dépendent du type du fichier.
When a suitable feature test macro is defined (_DEFAULT_SOURCE since glibc 2.19, or _BSD_SOURCE on glibc 2.19 and earlier), glibc defines the following macro constants for the value returned in .d_type:
Il s'agit d'un périphérique bloc.
Il s'agit d'un périphérique caractère.
Il s'agit d'un répertoire
Il s'agit d'un tube nommé (FIFO).
Il s'agit d'un lien symbolique.
Il s'agit d'un fichier ordinaire.
Il s'agit d'un socket de domaine UNIX.
Le type du fichier n'a pu être déterminé.
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.
.d_name
Ce champ contient le nom de fichier terminé par l'octet NULL. Voir VERSIONS.

Les donnes renvoyées par readdir() sont écrasées lors de l'appel suivant à readdir() sur le même flux répertoire.

VALEUR RENVOYÉE

En cas de succès, readdir() renvoie un pointeur sur une structure dirent (cette structure peut avoir été allouée statiquement ; n'essayez pas de la désallouer avec free(3)).

Lorsque la fin du flux répertoire est atteinte, NULL est renvoyé et errno n'est pas modifiée. En cas d'erreur, NULL est renvoyé et errno contient le code d'erreur. Pour distinguer la fin du flux d'une erreur, il faut assigner errno à zéro avant d'appeler readdir() puis ensuite tester la valeur de errno si NULL est renvoyé.

ERREURS

Le descripteur de flux répertoire dirp n'est pas valable.

ATTRIBUTS

Pour une explication des termes utilisés dans cette section, consulter attributes(7).

Interface Attribut Valeur
readdir() Sécurité des threads MT-Unsafe race:dirstream

Dans la spécification POSIX.1 actuelle (POSIX.1-2008), il n'est pas requis que readdir(3) soit sûr vis-à-vis des threads. Cependant, dans les implémentations modernes, incluant la glibc, des appels concurrents à readdir(3) pour des flux répertoire différents sont sûrs vis-à-vis des threads. Dans le cas où de multiples threads doivent lire depuis un flux de répertoire identique, l'utilisation de readdir(3) avec une synchronisation externe est toujours préférable à l'utilisation de readdir_r(3). Il est attendu qu'une future version de POSIX.1 demande que readdir() soit sûr vis-à-vis des threads lorsqu'il est employé simultanément sur des flux répertoire différents.

VERSIONS

Only the fields .d_name and (as an XSI extension) .d_ino are specified in POSIX.1. Other than Linux, the .d_type field is available mainly only on BSD systems. The remaining fields are available on many, but not all systems. Under glibc, programs can check for the availability of the fields not defined in POSIX.1 by testing whether the macros _DIRENT_HAVE_D_NAMLEN, _DIRENT_HAVE_D_RECLEN, _DIRENT_HAVE_D_OFF, or _DIRENT_HAVE_D_TYPE are defined.

The .d_name field

The dirent structure definition shown above is taken from the glibc headers, and shows the .d_name field with a fixed size.

Warning: applications should avoid any dependence on the size of the .d_name field. POSIX defines it as char d_name[], a character array of unspecified size, with at most NAME_MAX characters preceding the terminating null byte ('\0').

POSIX.1 explicitly notes that this field should not be used as an lvalue. The standard also notes that the use of sizeof(.d_name) is incorrect; use strlen(.d_name) instead. (On some systems, this field is defined as char d_name[1]!) By implication, the use sizeof(struct dirent) to capture the size of the record including the size of .d_name is also incorrect.

Notez que bien que l'appel


fpathconf(fd, _PC_NAME_MAX)

returns the value 255 for most filesystems, on some filesystems (e.g., CIFS, Windows SMB servers), the null-terminated filename that is (correctly) returned in .d_name can actually exceed this size. In such cases, the .d_reclen field will contain a value that exceeds the size of the glibc dirent structure shown above.

NORMES

POSIX.1-2008.

HISTORIQUE

POSIX.1-2001, SVr4, 4.3BSD.

NOTES

Un flux répertoire est ouvert avec opendir(3).

L'ordre dans lequel les noms de fichier sont lus par des appels successifs à readdir() dépend de l'implémentation du système de fichiers ; il est peu probable que les noms soient triés d'une quelconque façon.

VOIR AUSSI

getdents(2), read(2), closedir(3), dirfd(3), ftw(3), offsetof(3), opendir(3), readdir_r(3), rewinddir(3), scandir(3), seekdir(3), telldir(3)

TRADUCTION

La traduction française de cette page de manuel a été créée par Christophe Blaess <https://www.blaess.fr/christophe/>, Stéphan Rafin <stephan.rafin@laposte.net>, Thierry Vignaud <tvignaud@mandriva.com>, François Micaux, Alain Portal <aportal@univ-montp2.fr>, Jean-Philippe Guérard <fevrier@tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>, Julien Cristau <jcristau@debian.org>, Thomas Huriaux <thomas.huriaux@gmail.com>, Nicolas François <nicolas.francois@centraliens.net>, Florentin Duneau <fduneau@gmail.com>, Simon Paillard <simon.paillard@resel.enst-bretagne.fr>, Denis Barbier <barbier@debian.org>, David Prévot <david@tilapin.org>, Frédéric Hantrais <fhantrais@gmail.com> et Grégoire Scano <gregoire.scano@malloc.fr>

Cette traduction est une documentation libre ; veuillez vous reporter à la GNU General Public License version 3 concernant les conditions de copie et de distribution. Il n'y a aucune RESPONSABILITÉ LÉGALE.

Si vous découvrez un bogue dans la traduction de cette page de manuel, veuillez envoyer un message à debian-l10n-french@lists.debian.org.

8 février 2026 Pages du manuel de Linux 6.17