1 /* Read symbolic links into a buffer without size limitation, relative to fd.
3 Copyright (C) 2001, 2003-2004, 2007, 2009-2011 Free Software Foundation,
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
23 #include "careadlinkat.h"
25 #include "allocator.h"
33 /* Use the system functions, not the gnulib overrides, because this
34 module does not depend on GNU or POSIX semantics. */
38 /* Define this independently so that stdint.h is not a prerequisite. */
40 # define SIZE_MAX ((size_t) -1)
44 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
48 /* Ignore FD. Get the symbolic link value of FILENAME and put it into
49 BUFFER, with size BUFFER_SIZE. This function acts like readlink
50 but has readlinkat's signature. */
52 careadlinkatcwd (int fd, char const *filename, char *buffer,
56 return readlink (filename, buffer, buffer_size);
60 /* A standard allocator. For now, only careadlinkat needs this, but
61 perhaps it should be moved to the allocator module. */
62 static struct allocator const standard_allocator =
63 { malloc, realloc, free, NULL };
65 /* Assuming the current directory is FD, get the symbolic link value
66 of FILENAME as a null-terminated string and put it into a buffer.
67 If FD is AT_FDCWD, FILENAME is interpreted relative to the current
68 working directory, as in openat.
70 If the link is small enough to fit into BUFFER put it there.
71 BUFFER's size is BUFFER_SIZE, and BUFFER can be null
72 if BUFFER_SIZE is zero.
74 If the link is not small, put it into a dynamically allocated
75 buffer managed by ALLOC. It is the caller's responsibility to free
76 the returned value if it is nonnull and is not BUFFER. A null
77 ALLOC stands for the standard allocator.
79 The PREADLINKAT function specifies how to read links.
81 If successful, return the buffer address; otherwise return NULL and
85 careadlinkat (int fd, char const *filename,
86 char *buffer, size_t buffer_size,
87 struct allocator const *alloc,
88 ssize_t (*preadlinkat) (int, char const *, char *, size_t))
93 SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
97 alloc = &standard_allocator;
101 /* Allocate the initial buffer on the stack. This way, in the
102 common case of a symlink of small size, we get away with a
103 single small malloc() instead of a big malloc() followed by a
104 shrinking realloc(). */
106 buffer_size = sizeof stack_buf;
110 buf_size = buffer_size;
114 /* Attempt to read the link into the current buffer. */
115 ssize_t link_length = preadlinkat (fd, filename, buf, buf_size);
119 /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
120 with errno == ERANGE if the buffer is too small. */
121 int readlinkat_errno = errno;
122 if (readlinkat_errno != ERANGE)
127 errno = readlinkat_errno;
133 link_size = link_length;
135 if (link_size < buf_size)
137 buf[link_size++] = '\0';
139 if (buf == stack_buf)
141 char *b = (char *) alloc->malloc (link_size);
144 memcpy (b, buf, link_size);
147 else if (link_size < buf_size && buf != buffer && alloc->realloc)
149 /* Shrink BUF before returning it. */
150 char *b = (char *) alloc->realloc (buf, link_size);
161 if (buf_size <= buf_size_max / 2)
163 else if (buf_size < buf_size_max)
164 buf_size = buf_size_max;
167 buf = (char *) alloc->malloc (buf_size);