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