1readlink(2)                   System Calls Manual                  readlink(2)
2
3
4

NAME

6       readlink, readlinkat - read value of a symbolic link
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <unistd.h>
13
14       ssize_t readlink(const char *restrict pathname, char *restrict buf,
15                        size_t bufsiz);
16
17       #include <fcntl.h>            /* Definition of AT_* constants */
18       #include <unistd.h>
19
20       ssize_t readlinkat(int dirfd, const char *restrict pathname,
21                        char *restrict buf, size_t bufsiz);
22
23   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
24
25       readlink():
26           _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
27               || /* glibc <= 2.19: */ _BSD_SOURCE
28
29       readlinkat():
30           Since glibc 2.10:
31               _POSIX_C_SOURCE >= 200809L
32           Before glibc 2.10:
33               _ATFILE_SOURCE
34

DESCRIPTION

36       readlink()  places  the  contents  of the symbolic link pathname in the
37       buffer buf, which has size bufsiz.  readlink() does not append a termi‐
38       nating  null byte to buf.  It will (silently) truncate the contents (to
39       a length of bufsiz characters), in case the buffer is too small to hold
40       all of the contents.
41
42   readlinkat()
43       The  readlinkat() system call operates in exactly the same way as read‐
44       link(), except for the differences described here.
45
46       If the pathname given in pathname is relative, then it  is  interpreted
47       relative  to  the  directory  referred  to by the file descriptor dirfd
48       (rather than relative to the current working directory of  the  calling
49       process, as is done by readlink() for a relative pathname).
50
51       If  pathname  is relative and dirfd is the special value AT_FDCWD, then
52       pathname is interpreted relative to the current  working  directory  of
53       the calling process (like readlink()).
54
55       If pathname is absolute, then dirfd is ignored.
56
57       Since  Linux 2.6.39, pathname can be an empty string, in which case the
58       call operates on the symbolic link referred to by dirfd  (which  should
59       have been obtained using open(2) with the O_PATH and O_NOFOLLOW flags).
60
61       See openat(2) for an explanation of the need for readlinkat().
62

RETURN VALUE

64       On  success, these calls return the number of bytes placed in buf.  (If
65       the returned value equals bufsiz, then truncation may  have  occurred.)
66       On error, -1 is returned and errno is set to indicate the error.
67

ERRORS

69       EACCES Search  permission is denied for a component of the path prefix.
70              (See also path_resolution(7).)
71
72       EBADF  (readlinkat()) pathname is relative but dirfd is neither  AT_FD‐
73              CWD nor a valid file descriptor.
74
75       EFAULT buf extends outside the process's allocated address space.
76
77       EINVAL bufsiz is not positive.
78
79       EINVAL The  named file (i.e., the final filename component of pathname)
80              is not a symbolic link.
81
82       EIO    An I/O error occurred while reading from the filesystem.
83
84       ELOOP  Too many symbolic links  were  encountered  in  translating  the
85              pathname.
86
87       ENAMETOOLONG
88              A pathname, or a component of a pathname, was too long.
89
90       ENOENT The named file does not exist.
91
92       ENOMEM Insufficient kernel memory was available.
93
94       ENOTDIR
95              A component of the path prefix is not a directory.
96
97       ENOTDIR
98              (readlinkat()) pathname is relative and dirfd is a file descrip‐
99              tor referring to a file other than a directory.
100

STANDARDS

102       POSIX.1-2008.
103

HISTORY

105       readlink()
106              4.4BSD (first appeared in 4.2BSD), POSIX.1-2001, POSIX.1-2008.
107
108       readlinkat()
109              POSIX.1-2008.  Linux 2.6.16, glibc 2.4.
110
111       Up to and including glibc 2.4, the return type of  readlink()  was  de‐
112       clared  as  int.   Nowadays, the return type is declared as ssize_t, as
113       (newly) required in POSIX.1-2001.
114
115   glibc
116       On older kernels where readlinkat() is unavailable, the  glibc  wrapper
117       function falls back to the use of readlink().  When pathname is a rela‐
118       tive pathname, glibc constructs a pathname based on the  symbolic  link
119       in /proc/self/fd that corresponds to the dirfd argument.
120

NOTES

122       Using  a  statically sized buffer might not provide enough room for the
123       symbolic link contents.  The required size for the buffer  can  be  ob‐
124       tained  from  the  stat.st_size value returned by a call to lstat(2) on
125       the link.  However, the number of bytes written by readlink() and read‐
126       linkat()  should  be checked to make sure that the size of the symbolic
127       link did not increase between the calls.   Dynamically  allocating  the
128       buffer  for  readlink() and readlinkat() also addresses a common porta‐
129       bility problem when using PATH_MAX for the buffer size,  as  this  con‐
130       stant  is not guaranteed to be defined per POSIX if the system does not
131       have such limit.
132

EXAMPLES

134       The following program allocates the buffer needed by readlink() dynami‐
135       cally from the information provided by lstat(2), falling back to a buf‐
136       fer of size PATH_MAX in cases where lstat(2) reports a size of zero.
137
138       #include <limits.h>
139       #include <stdio.h>
140       #include <stdlib.h>
141       #include <sys/stat.h>
142       #include <unistd.h>
143
144       int
145       main(int argc, char *argv[])
146       {
147           char         *buf;
148           ssize_t      nbytes, bufsiz;
149           struct stat  sb;
150
151           if (argc != 2) {
152               fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
153               exit(EXIT_FAILURE);
154           }
155
156           if (lstat(argv[1], &sb) == -1) {
157               perror("lstat");
158               exit(EXIT_FAILURE);
159           }
160
161           /* Add one to the link size, so that we can determine whether
162              the buffer returned by readlink() was truncated. */
163
164           bufsiz = sb.st_size + 1;
165
166           /* Some magic symlinks under (for example) /proc and /sys
167              report 'st_size' as zero. In that case, take PATH_MAX as
168              a "good enough" estimate. */
169
170           if (sb.st_size == 0)
171               bufsiz = PATH_MAX;
172
173           buf = malloc(bufsiz);
174           if (buf == NULL) {
175               perror("malloc");
176               exit(EXIT_FAILURE);
177           }
178
179           nbytes = readlink(argv[1], buf, bufsiz);
180           if (nbytes == -1) {
181               perror("readlink");
182               exit(EXIT_FAILURE);
183           }
184
185           /* Print only 'nbytes' of 'buf', as it doesn't contain a terminating
186              null byte ('\0'). */
187           printf("'%s' points to '%.*s'\n", argv[1], (int) nbytes, buf);
188
189           /* If the return value was equal to the buffer size, then the
190              the link target was larger than expected (perhaps because the
191              target was changed between the call to lstat() and the call to
192              readlink()). Warn the user that the returned target may have
193              been truncated. */
194
195           if (nbytes == bufsiz)
196               printf("(Returned buffer may have been truncated)\n");
197
198           free(buf);
199           exit(EXIT_SUCCESS);
200       }
201

SEE ALSO

203       readlink(1), lstat(2), stat(2), symlink(2),  realpath(3),  path_resolu‐
204       tion(7), symlink(7)
205
206
207
208Linux man-pages 6.04              2023-03-30                       readlink(2)
Impressum