| readlink(2) | System Calls Manual | readlink(2) |
NOM¶
readlink, readlinkat - Lire le contenu d'un lien symbolique
BIBLIOTHÈQUE¶
Bibliothèque C standard (libc, -lc)
SYNOPSIS¶
#include <unistd.h>
ssize_t readlink(size_t bufsiz;
const char *restrict path,
char buf[restrict bufsiz], size_t bufsiz);
#include <fcntl.h> /* Définition des constantes AT_* */ #include <unistd.h>
ssize_t readlinkat(size_t bufsiz;
int dirfd, const char *restrict path,
char buf[restrict bufsiz], size_t bufsiz);
readlink():
_XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
|| /* glibc <= 2.19 : */ _BSD_SOURCE
readlinkat():
Depuis la glibc 2.10 :
_POSIX_C_SOURCE >= 200809L
avant la glibc 2.10 :
_ATFILE_SOURCE
DESCRIPTION¶
readlink() places the contents of the symbolic link path in the buffer buf, which has size bufsiz. readlink() does not append a terminating null byte to buf. It will (silently) truncate the contents (to a length of bufsiz characters), in case the buffer is too small to hold all of the contents.
readlinkat()¶
L'appel système readlinkat() fonctionne exactement comme readlink(), les seules différences étant décrites ici.
If path is relative, then it is interpreted relative to the directory referred to by the file descriptor dirfd (rather than relative to the current working directory of the calling process, as is done by readlink() for a relative pathname).
If path is relative and dirfd is the special value AT_FDCWD, then path is interpreted relative to the current working directory of the calling process (like readlink()).
Si path est absolu, alors dirfd est ignoré.
Since Linux 2.6.39, path can be an empty string, in which case the call operates on the symbolic link referred to by dirfd (which should have been obtained using open(2) with the O_PATH and O_NOFOLLOW flags).
Consultez openat(2) pour une explication de la nécessité de readlinkat().
VALEUR RENVOYÉE¶
S'il réussit, ces appels renvoient le nombre d'octets placés dans buf (si la valeur renvoyée est égale à bufsiz, il se peut qu'il y ait eu une troncature). S'il échoue, il renvoie -1 et écrit errno pour indiquer l'erreur.
ERREURS¶
- EACCES
- Un élément du chemin d'accès ne permet pas la recherche. (Consultez aussi path_resolution(7).)
- EBADF
- (readlinkat()) path is relative but dirfd is neither AT_FDCWD nor a valid file descriptor.
- EFAULT
- buf pointe en dehors de l'espace d'adressage accessible.
- EINVAL
- bufsiz n'est pas un nombre positif.
- EINVAL
- The named file (i.e., the final filename component of path) is not a symbolic link.
- EIO
- Une erreur d'entrée-sortie s'est produite durant la lecture du système de fichiers.
- ELOOP
- Trop de liens symboliques ont été rencontrés en parcourant le chemin.
- ENAMETOOLONG
- Un nom de chemin d'accès ou l'un des composants d'un nom de chemin d'accès est trop long.
- ENOENT
- Le fichier indiqué n'existe pas.
- ENOMEM
- La mémoire disponible du noyau n'était pas suffisante.
- ENOTDIR
- Un élément du chemin d'accès n'est pas un répertoire.
- ENOTDIR
- (readlinkat()) path is relative and dirfd is a file descriptor referring to a file other than a directory.
STANDARDS¶
POSIX.1-2008.
HISTORIQUE¶
- readlink()
- 4.4BSD (apparue dans 4.2BSD), POSIX.1-2001, POSIX.1-2008.
- readlinkat()
- POSIX.1-2008. Linux 2.6.16, glibc 2.4.
Jusqu'à la glibc 2.4 incluse, le type de retour de readlink() était déclaré comme int. À présent, le type de retour est déclaré comme ssize_t, ainsi que le prescrit POSIX.1-2001.
glibc¶
On older kernels where readlinkat() is unavailable, the glibc wrapper function falls back to the use of readlink(). When path is relative, glibc constructs a pathname based on the symbolic link in /proc/self/fd that corresponds to the dirfd argument.
NOTES¶
L'utilisation d'un tampon de taille statique risque de ne pas fournir assez de place pour le contenu du lien symbolique. La taille nécessaire au tampon peut être lue dans la valeur stat.st_size renvoyée par un appel à lstat(2) sur le lien. Cependant, le nombre d'octets écrits par readlink() et par readlinkat() devrait être vérifié pour s'assurer que la taille du lien symbolique n'a pas augmenté entre les appels. L'allocation dynamique du tampon pour readlink() et pour readlinkat() résout aussi un problème habituel de portabilité si PATH_MAX est utilisé comme taille de tampon, car la définition de cette constante n'est pas garantie selon les POSIX si le système n'a pas ce genre de limite.
EXEMPLES¶
Le programme suivant alloue le tampon nécessaire à readlink() dynamiquement à partir des données fournies par lstat(), en se rabattant sur un tampon de taille PATH_MAX si lstat(2) signale une taille de zéro.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
char *buf;
ssize_t nbytes, bufsiz;
struct stat sb;
if (argc != 2) {
fprintf(stderr, "Usage: %s <path>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (lstat(argv[1], &sb) == -1) {
perror("lstat");
exit(EXIT_FAILURE);
}
/* Add one to the link size, so that we can determine whether
the buffer returned by readlink() was truncated. */
bufsiz = sb.st_size + 1;
/* Some magic symlinks under (for example) /proc and /sys
report 'st_size' as zero. In that case, take PATH_MAX as
a "good enough" estimate. */
if (sb.st_size == 0)
bufsiz = PATH_MAX;
buf = malloc(bufsiz);
if (buf == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
nbytes = readlink(argv[1], buf, bufsiz);
if (nbytes == -1) {
perror("readlink");
exit(EXIT_FAILURE);
}
/* Print only 'nbytes' of 'buf', as it doesn't contain a terminating
null byte ('\0'). */
printf("'%s' points to '%.*s'\n", argv[1], (int) nbytes, buf);
/* If the return value was equal to the buffer size, then
the link target was larger than expected (perhaps because the
target was changed between the call to lstat() and the call to
readlink()). Warn the user that the returned target may have
been truncated. */
if (nbytes == bufsiz)
printf("(Returned buffer may have been truncated)\n");
free(buf);
exit(EXIT_SUCCESS);
}
VOIR AUSSI¶
readlink(1), lstat(2), stat(2), symlink(2), realpath(3), path_resolution(7), symlink(7)
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 Jean-Philippe MENGUAL <jpmengual@debian.org>
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.
| 28 juin 2025 | Pages du manuel de Linux (non publiées) |