1READLINK(3P) POSIX Programmer's Manual READLINK(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
11
13 readlink, readlinkat — read the contents of a symbolic link
14
16 #include <unistd.h>
17
18 ssize_t readlink(const char *restrict path, char *restrict buf,
19 size_t bufsize);
20 ssize_t readlinkat(int fd, const char *restrict path,
21 char *restrict buf, size_t bufsize);
22
24 The readlink() function shall place the contents of the symbolic link
25 referred to by path in the buffer buf which has size bufsize. If the
26 number of bytes in the symbolic link is less than bufsize, the contents
27 of the remainder of buf are unspecified. If the buf argument is not
28 large enough to contain the link content, the first bufsize bytes shall
29 be placed in buf.
30
31 If the value of bufsize is greater than {SSIZE_MAX}, the result is
32 implementation-defined.
33
34 Upon successful completion, readlink() shall mark for update the last
35 data access timestamp of the symbolic link.
36
37 The readlinkat() function shall be equivalent to the readlink() func‐
38 tion except in the case where path specifies a relative path. In this
39 case the symbolic link whose content is read is relative to the direc‐
40 tory associated with the file descriptor fd instead of the current
41 working directory. If the file descriptor was opened without O_SEARCH,
42 the function shall check whether directory searches are permitted using
43 the current permissions of the directory underlying the file descrip‐
44 tor. If the file descriptor was opened with O_SEARCH, the function
45 shall not perform the check.
46
47 If readlinkat() is passed the special value AT_FDCWD in the fd parame‐
48 ter, the current working directory shall be used and the behavior shall
49 be identical to a call to readlink().
50
52 Upon successful completion, these functions shall return the count of
53 bytes placed in the buffer. Otherwise, these functions shall return a
54 value of −1, leave the buffer unchanged, and set errno to indicate the
55 error.
56
58 These functions shall fail if:
59
60 EACCES Search permission is denied for a component of the path prefix
61 of path.
62
63 EINVAL The path argument names a file that is not a symbolic link.
64
65 EIO An I/O error occurred while reading from the file system.
66
67 ELOOP A loop exists in symbolic links encountered during resolution of
68 the path argument.
69
70 ENAMETOOLONG
71 The length of a component of a pathname is longer than
72 {NAME_MAX}.
73
74 ENOENT A component of path does not name an existing file or path is an
75 empty string.
76
77 ENOTDIR
78 A component of the path prefix names an existing file that is
79 neither a directory nor a symbolic link to a directory, or the
80 path argument contains at least one non-<slash> character and
81 ends with one or more trailing <slash> characters and the last
82 pathname component names an existing file that is neither a
83 directory nor a symbolic link to a directory.
84
85 The readlinkat() function shall fail if:
86
87 EACCES fd was not opened with O_SEARCH and the permissions of the
88 directory underlying fd do not permit directory searches.
89
90 EBADF The path argument does not specify an absolute path and the fd
91 argument is neither AT_FDCWD nor a valid file descriptor open
92 for reading or searching.
93
94 ENOTDIR
95 The path argument is not an absolute path and fd is a file
96 descriptor associated with a non-directory file.
97
98 These functions may fail if:
99
100 ELOOP More than {SYMLOOP_MAX} symbolic links were encountered during
101 resolution of the path argument.
102
103 ENAMETOOLONG
104 The length of a pathname exceeds {PATH_MAX}, or pathname resolu‐
105 tion of a symbolic link produced an intermediate result with a
106 length that exceeds {PATH_MAX}.
107
108 The following sections are informative.
109
111 Reading the Name of a Symbolic Link
112 The following example shows how to read the name of a symbolic link
113 named /modules/pass1.
114
115 #include <unistd.h>
116
117 char buf[1024];
118 ssize_t len;
119 ...
120 if ((len = readlink("/modules/pass1", buf, sizeof(buf)-1)) != -1)
121 buf[len] = '\0';
122
124 Conforming applications should not assume that the returned contents of
125 the symbolic link are null-terminated.
126
128 The type associated with bufsiz is a size_t in order to be consistent
129 with both the ISO C standard and the definition of read(). The behav‐
130 ior specified for readlink() when bufsiz is zero represents historical
131 practice. For this case, the standard developers considered a change
132 whereby readlink() would return the number of non-null bytes contained
133 in the symbolic link with the buffer buf remaining unchanged; however,
134 since the stat structure member st_size value can be used to determine
135 the size of buffer necessary to contain the contents of the symbolic
136 link as returned by readlink(), this proposal was rejected, and the
137 historical practice retained.
138
139 The purpose of the readlinkat() function is to read the content of sym‐
140 bolic links in directories other than the current working directory
141 without exposure to race conditions. Any part of the path of a file
142 could be changed in parallel to a call to readlink(), resulting in
143 unspecified behavior. By opening a file descriptor for the target
144 directory and using the readlinkat() function it can be guaranteed that
145 the symbolic link read is located relative to the desired directory.
146
148 None.
149
151 fstatat(), symlink()
152
153 The Base Definitions volume of POSIX.1‐2008, <unistd.h>
154
156 Portions of this text are reprinted and reproduced in electronic form
157 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
158 -- Portable Operating System Interface (POSIX), The Open Group Base
159 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
160 cal and Electronics Engineers, Inc and The Open Group. (This is
161 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
162 event of any discrepancy between this version and the original IEEE and
163 The Open Group Standard, the original IEEE and The Open Group Standard
164 is the referee document. The original Standard can be obtained online
165 at http://www.unix.org/online.html .
166
167 Any typographical or formatting errors that appear in this page are
168 most likely to have been introduced during the conversion of the source
169 files to man page format. To report such errors, see https://www.ker‐
170 nel.org/doc/man-pages/reporting_bugs.html .
171
172
173
174IEEE/The Open Group 2013 READLINK(3P)