allocator: New module.
[gnulib.git] / lib / careadlinkat.c
1 /* Read symbolic links into a buffer without size limitation, relative to fd.
2
3    Copyright (C) 2001, 2003-2004, 2007, 2009-2011 Free Software Foundation,
4    Inc.
5
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.
10
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.
15
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/>.  */
18
19 /* Written by Paul Eggert, Bruno Haible, and Jim Meyering.  */
20
21 #include <config.h>
22
23 #include "careadlinkat.h"
24
25 #include "allocator.h"
26
27 #include <errno.h>
28 #include <limits.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 /* Define this independently so that stdint.h is not a prerequisite.  */
33 #ifndef SIZE_MAX
34 # define SIZE_MAX ((size_t) -1)
35 #endif
36
37 #ifndef SSIZE_MAX
38 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
39 #endif
40
41 #if ! HAVE_READLINKAT
42 /* Ignore FD.  Get the symbolic link value of FILENAME and put it into
43    BUFFER, with size BUFFER_SIZE.  This function acts like readlink
44    but has readlinkat's signature.  */
45 ssize_t
46 careadlinkatcwd (int fd, char const *filename, char *buffer,
47                  size_t buffer_size)
48 {
49   (void) fd;
50   return readlink (filename, buffer, buffer_size);
51 }
52 #endif
53
54 /* Assuming the current directory is FD, get the symbolic link value
55    of FILENAME as a null-terminated string and put it into a buffer.
56    If FD is AT_FDCWD, FILENAME is interpreted relative to the current
57    working directory, as in openat.
58
59    If the link is small enough to fit into BUFFER put it there.
60    BUFFER's size is BUFFER_SIZE, and BUFFER can be null
61    if BUFFER_SIZE is zero.
62
63    If the link is not small, put it into a dynamically allocated
64    buffer managed by ALLOC.  It is the caller's responsibility to free
65    the returned value if it is nonnull and is not BUFFER.  A null
66    ALLOC stands for the standard allocator.
67
68    The PREADLINKAT function specifies how to read links.
69
70    If successful, return the buffer address; otherwise return NULL and
71    set errno.  */
72
73 char *
74 careadlinkat (int fd, char const *filename,
75               char *buffer, size_t buffer_size,
76               struct allocator const *alloc,
77               ssize_t (*preadlinkat) (int, char const *, char *, size_t))
78 {
79   char *buf;
80   size_t buf_size;
81   size_t buf_size_max =
82     SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
83   char stack_buf[1024];
84
85   if (! alloc)
86     alloc = &stdlib_allocator;
87
88   if (! buffer_size)
89     {
90       /* Allocate the initial buffer on the stack.  This way, in the
91          common case of a symlink of small size, we get away with a
92          single small malloc() instead of a big malloc() followed by a
93          shrinking realloc().  */
94       buffer = stack_buf;
95       buffer_size = sizeof stack_buf;
96     }
97
98   buf = buffer;
99   buf_size = buffer_size;
100
101   do
102     {
103       /* Attempt to read the link into the current buffer.  */
104       ssize_t link_length = preadlinkat (fd, filename, buf, buf_size);
105       size_t link_size;
106       if (link_length < 0)
107         {
108           /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
109              with errno == ERANGE if the buffer is too small.  */
110           int readlinkat_errno = errno;
111           if (readlinkat_errno != ERANGE)
112             {
113               if (buf != buffer)
114                 {
115                   alloc->free (buf);
116                   errno = readlinkat_errno;
117                 }
118               return NULL;
119             }
120         }
121
122       link_size = link_length;
123
124       if (link_size < buf_size)
125         {
126           buf[link_size++] = '\0';
127
128           if (buf == stack_buf)
129             {
130               char *b = (char *) alloc->allocate (link_size);
131               if (! b)
132                 break;
133               memcpy (b, buf, link_size);
134               buf = b;
135             }
136           else if (link_size < buf_size && buf != buffer && alloc->reallocate)
137             {
138               /* Shrink BUF before returning it.  */
139               char *b = (char *) alloc->reallocate (buf, link_size);
140               if (b)
141                 buf = b;
142             }
143
144           return buf;
145         }
146
147       if (buf != buffer)
148         alloc->free (buf);
149
150       if (buf_size <= buf_size_max / 2)
151         buf_size *= 2;
152       else if (buf_size < buf_size_max)
153         buf_size = buf_size_max;
154       else
155         break;
156       buf = (char *) alloc->allocate (buf_size);
157     }
158   while (buf);
159
160   if (alloc->die)
161     alloc->die ();
162   errno = ENOMEM;
163   return NULL;
164 }