1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Read symbolic links into a buffer without size limitation, relative to fd.
5 Copyright (C) 2001, 2003-2004, 2007, 2009-2014 Free Software Foundation,
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
25 #include "careadlinkat.h"
32 /* Define this independently so that stdint.h is not a prerequisite. */
34 # define SIZE_MAX ((size_t) -1)
38 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
41 #include "allocator.h"
43 /* Assuming the current directory is FD, get the symbolic link value
44 of FILENAME as a null-terminated string and put it into a buffer.
45 If FD is AT_FDCWD, FILENAME is interpreted relative to the current
46 working directory, as in openat.
48 If the link is small enough to fit into BUFFER put it there.
49 BUFFER's size is BUFFER_SIZE, and BUFFER can be null
50 if BUFFER_SIZE is zero.
52 If the link is not small, put it into a dynamically allocated
53 buffer managed by ALLOC. It is the caller's responsibility to free
54 the returned value if it is nonnull and is not BUFFER. A null
55 ALLOC stands for the standard allocator.
57 The PREADLINKAT function specifies how to read links. It operates
58 like POSIX readlinkat()
59 <http://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html>
60 but can assume that its first argument is the same as FD.
62 If successful, return the buffer address; otherwise return NULL and
66 careadlinkat (int fd, char const *filename,
67 char *buffer, size_t buffer_size,
68 struct allocator const *alloc,
69 ssize_t (*preadlinkat) (int, char const *, char *, size_t))
74 SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
78 alloc = &stdlib_allocator;
82 /* Allocate the initial buffer on the stack. This way, in the
83 common case of a symlink of small size, we get away with a
84 single small malloc() instead of a big malloc() followed by a
85 shrinking realloc(). */
87 buffer_size = sizeof stack_buf;
91 buf_size = buffer_size;
95 /* Attempt to read the link into the current buffer. */
96 ssize_t link_length = preadlinkat (fd, filename, buf, buf_size);
100 /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
101 with errno == ERANGE if the buffer is too small. */
102 int readlinkat_errno = errno;
103 if (readlinkat_errno != ERANGE)
108 errno = readlinkat_errno;
114 link_size = link_length;
116 if (link_size < buf_size)
118 buf[link_size++] = '\0';
120 if (buf == stack_buf)
122 char *b = (char *) alloc->allocate (link_size);
123 buf_size = link_size;
126 memcpy (b, buf, link_size);
129 else if (link_size < buf_size && buf != buffer && alloc->reallocate)
131 /* Shrink BUF before returning it. */
132 char *b = (char *) alloc->reallocate (buf, link_size);
143 if (buf_size <= buf_size_max / 2)
145 else if (buf_size < buf_size_max)
146 buf_size = buf_size_max;
147 else if (buf_size_max < SIZE_MAX)
149 errno = ENAMETOOLONG;
154 buf = (char *) alloc->allocate (buf_size);
159 alloc->die (buf_size);