readlink(2) System Calls Manual readlink(2) NOM readlink, readlinkat - Lire le contenu d'un lien symbolique BIBLIOTHEQUE Bibliotheque C standard (libc, -lc) SYNOPSIS #include ssize_t readlink(size_t bufsiz; const char *restrict path, char buf[restrict bufsiz], size_t bufsiz); #include /* Definition des constantes AT_* */ #include ssize_t readlinkat(size_t bufsiz; int dirfd, const char *restrict path, char buf[restrict bufsiz], size_t bufsiz); Exigences de macros de test de fonctionnalites pour la glibc (consulter feature_test_macros(7)) : 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 systeme readlinkat() fonctionne exactement comme readlink(), les seules differences etant decrites 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 ignore. 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 necessite de readlinkat(). VALEUR RENVOYEE S'il reussit, ces appels renvoient le nombre d'octets places dans buf (si la valeur renvoyee est egale a bufsiz, il se peut qu'il y ait eu une troncature). S'il echoue, il renvoie -1 et ecrit errno pour indiquer l'erreur. ERREURS EACCES Un element du chemin d'acces 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'entree-sortie s'est produite durant la lecture du systeme de fichiers. ELOOP Trop de liens symboliques ont ete rencontres en parcourant le chemin. ENAMETOOLONG Un nom de chemin d'acces ou l'un des composants d'un nom de chemin d'acces est trop long. ENOENT Le fichier indique n'existe pas. ENOMEM La memoire disponible du noyau n'etait pas suffisante. ENOTDIR Un element du chemin d'acces n'est pas un repertoire. ENOTDIR (readlinkat()) path is relative and dirfd is a file descriptor referring to a file other than a directory. NORMES POSIX.1-2024. 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'a la glibc 2.4 incluse, le type de retour de readlink() etait declare comme int. A present, le type de retour est declare 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 necessaire au tampon peut etre lue dans la valeur stat.st_size renvoyee par un appel a lstat(2) sur le lien. Cependant, le nombre d'octets ecrits par readlink() et par readlinkat() devrait etre verifie pour s'assurer que la taille du lien symbolique n'a pas augmente entre les appels. L'allocation dynamique du tampon pour readlink() et pour readlinkat() resout aussi un probleme habituel de portabilite si PATH_MAX est utilise comme taille de tampon, car la definition de cette constante n'est pas garantie selon les POSIX si le systeme n'a pas ce genre de limite. EXEMPLES Le programme suivant alloue le tampon necessaire a readlink() dynamiquement a partir des donnees fournies par lstat(), en se rabattant sur un tampon de taille PATH_MAX si lstat(2) signale une taille de zero. #include #include #include #include #include #include int main(int argc, char *argv[]) { char *buf; ssize_t nbytes, bufsiz; struct stat sb; if (argc != 2) { fprintf(stderr, "Usage: %s \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 francaise de cette page de manuel a ete creee par Christophe Blaess , Stephan Rafin , Thierry Vignaud , Francois Micaux, Alain Portal , Jean-Philippe Guerard , Jean-Luc Coulon (f5ibh) , Julien Cristau , Thomas Huriaux , Nicolas Francois , Florentin Duneau , Simon Paillard , Denis Barbier , David Prevot , Frederic Hantrais et Jean- Philippe MENGUAL Cette traduction est une documentation libre ; veuillez vous reporter a la GNU General Public License version 3 concernant les conditions de copie et de distribution. Il n'y a aucune RESPONSABILITE LEGALE. Si vous decouvrez un bogue dans la traduction de cette page de manuel, veuillez envoyer un message a . Pages du manuel de Linux 6.18 8 fevrier 2026 readlink(2)