execute, spawn-pipe: Make multithread-safe on native Windows.
[gnulib.git] / lib / careadlinkat.c
blob79cc3b3a0a0f015ec2c06bf318d46fa01558fe47
1 /* Read symbolic links into a buffer without size limitation, relative to fd.
3 Copyright (C) 2001, 2003-2004, 2007, 2009-2020 Free Software Foundation,
4 Inc.
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 <https://www.gnu.org/licenses/>. */
19 /* Written by Paul Eggert, Bruno Haible, and Jim Meyering. */
21 #include <config.h>
23 #include "careadlinkat.h"
25 #include <errno.h>
26 #include <limits.h>
27 #include <string.h>
28 #include <unistd.h>
30 /* Define this independently so that stdint.h is not a prerequisite. */
31 #ifndef SIZE_MAX
32 # define SIZE_MAX ((size_t) -1)
33 #endif
35 #ifndef SSIZE_MAX
36 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
37 #endif
39 #include "allocator.h"
41 enum { STACK_BUF_SIZE = 1024 };
43 /* Act like careadlinkat (see below), with an additional argument
44 STACK_BUF that can be used as temporary storage.
46 If GCC_LINT is defined, do not inline this function with GCC 10.1
47 and later, to avoid creating a pointer to the stack that GCC
48 -Wreturn-local-addr incorrectly complains about. See:
49 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93644
50 Although the noinline attribute can hurt performance a bit, no better way
51 to pacify GCC is known; even an explicit #pragma does not pacify GCC.
52 When the GCC bug is fixed this workaround should be limited to the
53 broken GCC versions. */
54 #if _GL_GNUC_PREREQ (10, 1)
55 # if defined GCC_LINT || defined lint
56 __attribute__ ((__noinline__))
57 # elif __OPTIMIZE__ && !__NO_INLINE__
58 # warning "GCC might issue a bogus -Wreturn-local-addr warning here."
59 # warning "See <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93644>."
60 # endif
61 #endif
62 static char *
63 readlink_stk (int fd, char const *filename,
64 char *buffer, size_t buffer_size,
65 struct allocator const *alloc,
66 ssize_t (*preadlinkat) (int, char const *, char *, size_t),
67 char stack_buf[STACK_BUF_SIZE])
69 char *buf;
70 size_t buf_size;
71 size_t buf_size_max =
72 SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
74 if (! alloc)
75 alloc = &stdlib_allocator;
77 if (!buffer)
79 buffer = stack_buf;
80 buffer_size = STACK_BUF_SIZE;
83 buf = buffer;
84 buf_size = buffer_size;
86 while (buf)
88 /* Attempt to read the link into the current buffer. */
89 ssize_t link_length = preadlinkat (fd, filename, buf, buf_size);
90 size_t link_size;
91 if (link_length < 0)
93 /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
94 with errno == ERANGE if the buffer is too small. */
95 int readlinkat_errno = errno;
96 if (readlinkat_errno != ERANGE)
98 if (buf != buffer)
100 alloc->free (buf);
101 errno = readlinkat_errno;
103 return NULL;
107 link_size = link_length;
109 if (link_size < buf_size)
111 buf[link_size++] = '\0';
113 if (buf == stack_buf)
115 char *b = alloc->allocate (link_size);
116 buf_size = link_size;
117 if (! b)
118 break;
119 return memcpy (b, buf, link_size);
122 if (link_size < buf_size && buf != buffer && alloc->reallocate)
124 /* Shrink BUF before returning it. */
125 char *b = alloc->reallocate (buf, link_size);
126 if (b)
127 return b;
130 return buf;
133 if (buf != buffer)
134 alloc->free (buf);
136 if (buf_size < buf_size_max / 2)
137 buf_size = 2 * buf_size + 1;
138 else if (buf_size < buf_size_max)
139 buf_size = buf_size_max;
140 else if (buf_size_max < SIZE_MAX)
142 errno = ENAMETOOLONG;
143 return NULL;
145 else
146 break;
147 buf = alloc->allocate (buf_size);
150 if (alloc->die)
151 alloc->die (buf_size);
152 errno = ENOMEM;
153 return NULL;
157 /* Assuming the current directory is FD, get the symbolic link value
158 of FILENAME as a null-terminated string and put it into a buffer.
159 If FD is AT_FDCWD, FILENAME is interpreted relative to the current
160 working directory, as in openat.
162 If the link is small enough to fit into BUFFER put it there.
163 BUFFER's size is BUFFER_SIZE, and BUFFER can be null
164 if BUFFER_SIZE is zero.
166 If the link is not small, put it into a dynamically allocated
167 buffer managed by ALLOC. It is the caller's responsibility to free
168 the returned value if it is nonnull and is not BUFFER. A null
169 ALLOC stands for the standard allocator.
171 The PREADLINKAT function specifies how to read links. It operates
172 like POSIX readlinkat()
173 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/readlink.html>
174 but can assume that its first argument is the same as FD.
176 If successful, return the buffer address; otherwise return NULL and
177 set errno. */
179 char *
180 careadlinkat (int fd, char const *filename,
181 char *buffer, size_t buffer_size,
182 struct allocator const *alloc,
183 ssize_t (*preadlinkat) (int, char const *, char *, size_t))
185 /* Allocate the initial buffer on the stack. This way, in the
186 common case of a symlink of small size, we get away with a
187 single small malloc instead of a big malloc followed by a
188 shrinking realloc.
190 If GCC -Wreturn-local-addr warns about this buffer, the warning
191 is bogus; see readlink_stk. */
192 char stack_buf[STACK_BUF_SIZE];
193 return readlink_stk (fd, filename, buffer, buffer_size, alloc,
194 preadlinkat, stack_buf);