1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" And Copyright (C) 2011 Guillem Jover <guillem@hadrons.org>
3 .\" All rights reserved.
5 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. All advertising materials mentioning features or use of this software
15 .\" must display the following acknowledgement:
16 .\" This product includes software developed by the University of
17 .\" California, Berkeley and its contributors.
18 .\" 4. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 .\" @(#)readlink.2 6.8 (Berkeley) 3/10/91
37 .\" Modified Sat Jul 24 00:10:21 1993 by Rik Faith (faith@cs.unc.edu)
38 .\" Modified Tue Jul 9 23:55:17 1996 by aeb
39 .\" Modified Fri Jan 24 00:26:00 1997 by aeb
40 .\" 2011-09-20, Guillem Jover <guillem@hadrons.org>:
41 .\" Added text on dynamically allocating buffer + example program
43 .TH READLINK 2 2013-07-18 "Linux" "Linux Programmer's Manual"
45 readlink \- read value of a symbolic link
47 .B #include <unistd.h>
49 .BI "ssize_t readlink(const char *" path ", char *" buf ", size_t " bufsiz );
52 Feature Test Macro Requirements for glibc (see
53 .BR feature_test_macros (7)):
59 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
60 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED || _POSIX_C_SOURCE\ >=\ 200112L
65 places the contents of the symbolic link
72 does not append a null byte to
74 It will truncate the contents (to a length of
76 characters), in case the buffer is too small to hold all of the contents.
80 returns the number of bytes placed in
82 On error, \-1 is returned and
84 is set to indicate the error.
88 Search permission is denied for a component of the path prefix.
90 .BR path_resolution (7).)
94 extends outside the process's allocated address space.
99 .\" At the glibc level, bufsiz is unsigned, so this error can only occur
100 .\" if bufsiz==0. However, the in the kernel syscall, bufsiz is signed,
101 .\" and this error can also occur if bufsiz < 0.
102 .\" See: http://thread.gmane.org/gmane.linux.man/380
103 .\" Subject: [patch 0/3] [RFC] kernel/glibc mismatch of "readlink" syscall?
106 The named file is not a symbolic link.
109 An I/O error occurred while reading from the file system.
112 Too many symbolic links were encountered in translating the pathname.
115 A pathname, or a component of a pathname, was too long.
118 The named file does not exist.
121 Insufficient kernel memory was available.
124 A component of the path prefix is not a directory.
128 first appeared in 4.2BSD),
131 In versions of glibc up to and including glibc 2.4, the return type of
135 Nowadays, the return type is declared as
137 as (newly) required in POSIX.1-2001.
139 Using a statically sized buffer might not provide enough room for the
140 symbolic link contents.
141 The required size for the buffer can be obtained from the
143 value returned by a call to
146 However, the number of bytes written by
148 should be checked to make sure that the size of the
149 symbolic link did not increase between the calls.
150 Dynamically allocating the buffer for
152 also addresses a common portability problem when using
155 as this constant is not guaranteed to be defined per POSIX
156 if the system does not have such limit.
158 The following program allocates the buffer needed by
160 dynamically from the information provided by
162 making sure there's no race condition between the calls.
165 #include <sys/types.h>
166 #include <sys/stat.h>
172 main(int argc, char *argv[])
179 fprintf(stderr, "Usage: %s <pathname>\\n", argv[0]);
183 if (lstat(argv[1], &sb) == \-1) {
188 linkname = malloc(sb.st_size + 1);
189 if (linkname == NULL) {
190 fprintf(stderr, "insufficient memory\\n");
194 r = readlink(argv[1], linkname, sb.st_size + 1);
201 if (r > sb.st_size) {
202 fprintf(stderr, "symlink increased in size "
203 "between lstat() and readlink()\\n");
207 linkname[r] = \(aq\\0\(aq;
209 printf("\(aq%s\(aq points to \(aq%s\(aq\\n", argv[1], linkname);
220 .BR path_resolution (7),