readlink(2) System Calls Manual readlink(2) NAAM readlink, readlinkat - lees waarde van een symbolische koppeling BIBLIOTHEEK Standard C bibliotheek (libc, -lc) SAMENVATTING #include ssize_t readlink(size_t bufsiz; const char *restrict path, char buf[restrict bufsiz], size_t bufsiz); #include /* Definitie van of AT_* constanten */ #include ssize_t readlinkat(size_t bufsiz; int dirfd, const char *restrict path, char buf[restrict bufsiz], size_t bufsiz); Feature Test Macro's eisen in glibc (zie feature_test_macros(7)): readlink(): _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L || /* glibc <= 2.19: */ _BSD_SOURCE readlinkat(): Vanaf glibc 2.10: _POSIX_C_SOURCE >= 200809L Voor glibc 2.10: _ATFILE_SOURCE BESCHRIJVING 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() De readlinkat() systeem aanroep werkt op exact dezelfde manier als readlink(), behalve voor de hier beschreven verschillen. 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()). Als padnaam absoluut is, dan wordt mapbi genegeerd. 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). Zie openat(2) voor de uitleg over de noodzaak van readlinkat(). EIND WAARDE Bij succes, geven deze aanroepen het aantal bytes terug dat in de buffer werd geplaatst (als de teruggegeven waarde gelijk is aan bufsiz, dan kan afbreken zijn opgetreden.) Bij de fout wordt -1 teruggegeven en wordt errno gezet om de fout aan te geven. FOUTEN EACCES Zoek toestemming werd geweigerd voor een deel van het pad voorvoegsel. (Zie ook path_resolution(7).) EBADF (readlinkat()) path is relative but dirfd is neither AT_FDCWD nor a valid file descriptor. EFAULT buf strekt zich uit voorbij de aan het proces toegewezen adres ruimte. EINVAL bufmaat is niet positief. EINVAL The named file (i.e., the final filename component of path) is not a symbolic link. EIO Een In/Uit fout trad op terwijl er van het bestandsysteem gelezen werd. ELOOP Teveel symbolische koppelingen werden tegengekomen bij het vertalen van de padnaam. ENAMETOOLONG Een padnaam, of een deel van een padnaam, was te lang. ENOENT Het genoemde bestand bestaat niet. ENOMEM Onvoldoende kernelgeheugen voorhanden. ENOTDIR Een deel van het pad-voorvoegsel is geen map. ENOTDIR (readlinkat()) path is relative and dirfd is a file descriptor referring to a file other than a directory. VOLDOET AAN POSIX.1-2008. GESCHIEDENIS readlink() 4.4BSD (verscheen voor het eerst in 4.2BSD), POSIX.1-2001, POSIX.1-2008. readlinkat() POSIX.1-2008. Linux 2.6.16, glibc 2.4. Tot en met glibc 2.4 was het uitvoer type van readlink() gedeclareerd als int. Tegenwoordig is het uitvoer type gedeclareerd als ssize_t, als (nieuw) vereist in 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. OPMERKINGEN Gebruik van een buffer met een statische grootte kan niet genoeg ruimte opleveren voor de inhoud van de symbolische koppeling. De vereiste grootte van de buffer kan worden verkregen van de stat_st_size waarde zoals teruggegeven door lstat(2) op de koppeling. Echter moet het aantal door readlink() en readlinkat() geschreven bytes gecontroleerd worden om er van zeker te zijn dat de grootte van de symbolische koppeling niet toenam tussen de twee aanroepen in. Het dynamisch toewijzen van de buffer voor readlink() en readlinkat() adresseert ook een vaak voorkomend portabiliteit probleem bij het gebruik van PATH_MAX voor de buffer grootte, omdat deze constante niet gegarandeerd gedefinieerd wordt conform POSIX als het systeem niet deze limiet heeft. VOORBEELDEN Het volgende programma wijst de door readlink() benodigde buffer dynamisch toe aan de hand van de informatie voorzien door lstat(2), met terug vallende naar een buffer van PATH_MAX grootte in die gevallen waar lstat(2) een grootte van nul meldt. #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); } ZIE OOK readlink(1), lstat(2), stat(2), symlink(2), realpath(3), path_resolution(7), symlink(7) VERTALING De Nederlandse vertaling van deze handleiding is geschreven door Jos Boersema , Mario Blattermann en Luc Castermans Deze vertaling is vrije documentatie; lees de GNU General Public License Version 3 of later over de Copyright-voorwaarden. Er is geen AANSPRAKELIJKHEID. Indien U fouten in de vertaling van deze handleiding zou vinden, stuur een e-mail naar . Linux man-pages 6.15 28 juni 2025 readlink(2)