1READLINK(2)                Linux Programmer's Manual               READLINK(2)
2
3
4

NAME

6       readlink - read value of a symbolic link
7

SYNOPSIS

9       #include <unistd.h>
10
11       ssize_t readlink(const char *path, char *buf, size_t bufsiz);
12
13   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
14
15       readlink():
16           _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
17           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED ||
18           _POSIX_C_SOURCE >= 200112L
19

DESCRIPTION

21       readlink()  places the contents of the symbolic link path in the buffer
22       buf, which has size bufsiz.  readlink() does not append a null byte  to
23       buf.  It will truncate the contents (to a length of bufsiz characters),
24       in case the buffer is too small to hold all of the contents.
25

RETURN VALUE

27       On success, readlink() returns the number of bytes placed in  buf.   On
28       error, -1 is returned and errno is set to indicate the error.
29

ERRORS

31       EACCES Search  permission is denied for a component of the path prefix.
32              (See also path_resolution(7).)
33
34       EFAULT buf extends outside the process's allocated address space.
35
36       EINVAL bufsiz is not positive.
37
38       EINVAL The named file is not a symbolic link.
39
40       EIO    An I/O error occurred while reading from the file system.
41
42       ELOOP  Too many symbolic links  were  encountered  in  translating  the
43              pathname.
44
45       ENAMETOOLONG
46              A pathname, or a component of a pathname, was too long.
47
48       ENOENT The named file does not exist.
49
50       ENOMEM Insufficient kernel memory was available.
51
52       ENOTDIR
53              A component of the path prefix is not a directory.
54

CONFORMING TO

56       4.4BSD (readlink() first appeared in 4.2BSD), POSIX.1-2001.
57

NOTES

59       In  versions of glibc up to and including glibc 2.4, the return type of
60       readlink() was declared as int.  Nowadays, the return type is  declared
61       as ssize_t, as (newly) required in POSIX.1-2001.
62
63       Using  a  statically sized buffer might not provide enough room for the
64       symbolic link contents.  The  required  size  for  the  buffer  can  be
65       obtained  from the stat.st_size value returned by a call to lstat(2) on
66       the link.  However, the number of bytes written by readlink() should be
67       checked  to  make  sure  that  the  size  of  the symbolic link did not
68       increase between the calls.   Dynamically  allocating  the  buffer  for
69       readlink()  also  addresses  a  common  portability  problem when using
70       PATH_MAX for the buffer size, as this constant is not guaranteed to  be
71       defined per POSIX if the system does not have such limit.
72

EXAMPLE

74       The following program allocates the buffer needed by readlink() dynami‐
75       cally from the information provided by lstat(), making sure there's  no
76       race condition between the calls.
77
78       #include <sys/types.h>
79       #include <sys/stat.h>
80       #include <stdio.h>
81       #include <stdlib.h>
82       #include <unistd.h>
83
84       int
85       main(int argc, char *argv[])
86       {
87           struct stat sb;
88           char *linkname;
89           ssize_t r;
90
91           if (argc != 2) {
92               fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
93               exit(EXIT_FAILURE);
94           }
95
96           if (lstat(argv[1], &sb) == -1) {
97               perror("lstat");
98               exit(EXIT_FAILURE);
99           }
100
101           linkname = malloc(sb.st_size + 1);
102           if (linkname == NULL) {
103               fprintf(stderr, "insufficient memory\n");
104               exit(EXIT_FAILURE);
105           }
106
107           r = readlink(argv[1], linkname, sb.st_size + 1);
108
109           if (r == -1) {
110               perror("lstat");
111               exit(EXIT_FAILURE);
112           }
113
114           if (r > sb.st_size) {
115               fprintf(stderr, "symlink increased in size "
116                               "between lstat() and readlink()\n");
117               exit(EXIT_FAILURE);
118           }
119
120           linkname[r] = '\0';
121
122           printf("'%s' points to '%s'\n", argv[1], linkname);
123
124           exit(EXIT_SUCCESS);
125       }
126

SEE ALSO

128       readlink(1), lstat(2), readlinkat(2), stat(2), symlink(2), path_resolu‐
129       tion(7), symlink(7)
130

COLOPHON

132       This page is part of release 3.53 of the Linux  man-pages  project.   A
133       description  of  the project, and information about reporting bugs, can
134       be found at http://www.kernel.org/doc/man-pages/.
135
136
137
138Linux                             2013-07-18                       READLINK(2)
Impressum