Manpages

NOM

read - Lire depuis un descripteur de fichier

SYNOPSIS

#include <unistd.h>

ssize_t read(int fd, void *buf, size_t count);

DESCRIPTION

read() lit jusqu’à count octets depuis le descripteur de fichier fd dans le tampon pointé par buf.

On files that support seeking, the read operation commences at the file offset, and the file offset is incremented by the number of bytes read. If the file offset is at or past the end of file, no bytes are read, and read() returns zero.

Si count vaut zéro, read() pourrait détecter les erreurs décrites ci-dessous. En absence d’erreur, ou si read() ne vérifie pas les erreurs, un read() avec un count de 0 renvoie zéro et n’a pas d’autres effets.

According to POSIX.1, if count is greater than SSIZE_MAX, the result is implementation-defined; see NOTES for the upper limit on Linux.

VALEUR RENVOYÉE

On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. See also NOTES.

On error, -1 is returned, and errno is set appropriately. In this case, it is left unspecified whether the file position (if any) changes.

ERREURS

EAGAIN

The file descriptor fd refers to a file other than a socket and has been marked nonblocking (O_NONBLOCK), and the read would block. See open(2) for further details on the O_NONBLOCK flag.

EAGAIN ou EWOULDBLOCK

Le descripteur de fichier fd fait référence à un fichier autre qu’une socket et a été marqué comme non bloquant (O_NONBLOCK), et la lecture devrait bloquer. POSIX.1-2001 permet de renvoyer l’une ou l’autre des erreurs dans ce cas et n’exige pas que ces constantes aient la même valeur. Une application portable devrait donc tester les deux possibilités.

EBADF

fd n’est pas un descripteur de fichier valable ou n’est pas ouvert en lecture.

EFAULT

buf pointe en dehors de l’espace d’adressage accessible.

EINTR

read() a été interrompu par un signal avant d’avoir eu le temps de lire quoi que ce soit ; consultez signal(7).

EINVAL

fd is attached to an object which is unsuitable for reading; or the file was opened with the O_DIRECT flag, and either the address specified in buf, the value specified in count, or the file offset is not suitably aligned.

EINVAL

fd a été créé par un appel à timerfd_create(2) et une mauvaise taille de tampon a été donnée à read() ; consultez timerfd_create(2) pour plus d’informations.

EIO

I/O error. This will happen for example when the process is in a background process group, tries to read from its controlling terminal, and either it is ignoring or blocking SIGTTIN or its process group is orphaned. It may also occur when there is a low-level I/O error while reading from a disk or tape. A further possible cause of EIO on networked filesystems is when an advisory lock had been taken out on the file descriptor and this lock has been lost. See the Lost locks section of fcntl(2) for further details.

EISDIR

fd est un répertoire.

D’autres erreurs peuvent se produire suivant le type d’objet associé à fd.

CONFORMITÉ

SVr4, 4.3BSD, POSIX.1-2001.

NOTES

The types size_t and ssize_t are, respectively, unsigned and signed integer data types specified by POSIX.1.

On Linux, read() (and similar system calls) will transfer at most 0x7ffff000 (2,147,479,552) bytes, returning the number of bytes actually transferred. (This is true on both 32-bit and 64-bit systems.)

On NFS filesystems, reading small amounts of data will update the timestamp only the first time, subsequent calls may not do so. This is caused by client side attribute caching, because most if not all NFS clients leave st_atime (last file access time) updates to the server, and client side reads satisfied from the client’s cache will not cause st_atime updates on the server as there are no server-side reads. UNIX semantics can be obtained by disabling client-side attribute caching, but in most situations this will substantially increase server load and decrease performance.

BOGUES

Selon POSIX.1-2008/SUSv4, Section XSI 2.9.7 ("Thread Interactions with Regular File Operations") :

Toutes les fonctions suivantes doivent être atomiques et ne pas se perturber mutuellement pour ce qui concerne les effets spécifiés dans POSIX.1-2008 lorsqu’elles opèrent sur les fichiers réguliers ou sur les liens symboliques : ...

read() et readv(2) figurent parmi les API listées par la suite. En outre, la mise à jour du décalage de fichier fait partie des effets qui doivent être atomiques pour les threads (et pour les processus). Cependant, dans les versions de Linux antérieures à 3.14, cela n’était pas le cas : si deux processus partageant un même descripteur de fichier (consultez open(2)) effectuaient une action read() (ou readv(2)) simultanément, alors les opération E/S n’étaient pas atomiques pour ce qui concernait la mise à jour du décalage de fichier. En conséquence, les lectures effectuées par les deux processus pouvaient se chevaucher au niveau des blocs de données récupérés (de façon incorrecte). Ce problème a été résolu dans Linux 3.14.

VOIR AUSSI

close(2), fcntl(2), ioctl(2), lseek(2), open(2), pread(2), readdir(2), readlink(2), readv(2), select(2), write(2), fread(3)

COLOPHON

Cette page fait partie de la publication 5.07 du projet man-pages Linux. Une description du projet et des instructions pour signaler des anomalies et la dernière version de cette page, peuvent être trouvées à l’adresse https://www.kernel.org/doc/man-pages/.

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 [AT] laposte.net>, Thierry Vignaud <tvignaud [AT] mandriva.com>, François Micaux, Alain Portal <aportal [AT] univ-montp2.fr>, Jean-Philippe Guérard <fevrier [AT] tigreraye.org>, Jean-Luc Coulon (f5ibh) <jean-luc.coulon [AT] wanadoo.fr>, Julien Cristau <jcristau [AT] debian.org>, Thomas Huriaux <thomas.huriaux [AT] gmail.com>, Nicolas François <nicolas.francois [AT] centraliens.net>, Florentin Duneau <fduneau [AT] gmail.com>, Simon Paillard <simon.paillard [AT] resel.fr>, Denis Barbier <barbier [AT] debian.org>, David Prévot <david [AT] tilapin.org> et Frédéric Hantrais <fhantrais [AT] gmail.com>

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 [AT] lists.org>.