careadlink: fix compilation error on mingw
[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 <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 /* Define this independently so that stdint.h is not a prerequisite.  */
34 #ifndef SIZE_MAX
35 # define SIZE_MAX ((size_t) -1)
36 #endif
37
38 #ifndef SSIZE_MAX
39 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
40 #endif
41
42 #if ! HAVE_READLINKAT
43 /* Ignore FD.  Get the symbolic link value of FILENAME and put it into
44    BUFFER, with size BUFFER_SIZE.  This function acts like readlink
45    but has readlinkat's signature.  */
46 ssize_t
47 careadlinkatcwd (int fd, char const *filename, char *buffer,
48                  size_t buffer_size)
49 {
50   (void) fd;
51   return readlink (filename, buffer, buffer_size);
52 }
53 #endif
54
55 /* Forward declaration.  We want to #undef malloc before initializing
56    this struct, but cannot do so until after all code that uses named
57    fields from "allocator.h" has been compiled.  */
58 static struct allocator const standard_allocator;
59
60 /* Assuming the current directory is FD, get the symbolic link value
61    of FILENAME as a null-terminated string and put it into a buffer.
62    If FD is AT_FDCWD, FILENAME is interpreted relative to the current
63    working directory, as in openat.
64
65    If the link is small enough to fit into BUFFER put it there.
66    BUFFER's size is BUFFER_SIZE, and BUFFER can be null
67    if BUFFER_SIZE is zero.
68
69    If the link is not small, put it into a dynamically allocated
70    buffer managed by ALLOC.  It is the caller's responsibility to free
71    the returned value if it is nonnull and is not BUFFER.  A null
72    ALLOC stands for the standard allocator.
73
74    The PREADLINKAT function specifies how to read links.
75
76    If successful, return the buffer address; otherwise return NULL and
77    set errno.  */
78
79 char *
80 careadlinkat (int fd, char const *filename,
81               char *buffer, size_t buffer_size,
82               struct allocator const *alloc,
83               ssize_t (*preadlinkat) (int, char const *, char *, size_t))
84 {
85   char *buf;
86   size_t buf_size;
87   size_t buf_size_max =
88     SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
89   char stack_buf[1024];
90
91   if (! alloc)
92     alloc = &standard_allocator;
93
94   if (! buffer_size)
95     {
96       /* Allocate the initial buffer on the stack.  This way, in the
97          common case of a symlink of small size, we get away with a
98          single small malloc() instead of a big malloc() followed by a
99          shrinking realloc().  */
100       buffer = stack_buf;
101       buffer_size = sizeof stack_buf;
102     }
103
104   buf = buffer;
105   buf_size = buffer_size;
106
107   do
108     {
109       /* Attempt to read the link into the current buffer.  */
110       ssize_t link_length = preadlinkat (fd, filename, buf, buf_size);
111       size_t link_size;
112       if (link_length < 0)
113         {
114           /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
115              with errno == ERANGE if the buffer is too small.  */
116           int readlinkat_errno = errno;
117           if (readlinkat_errno != ERANGE)
118             {
119               if (buf != buffer)
120                 {
121                   alloc->free (buf);
122                   errno = readlinkat_errno;
123                 }
124               return NULL;
125             }
126         }
127
128       link_size = link_length;
129
130       if (link_size < buf_size)
131         {
132           buf[link_size++] = '\0';
133
134           if (buf == stack_buf)
135             {
136               char *b = (char *) alloc->malloc (link_size);
137               if (! b)
138                 break;
139               memcpy (b, buf, link_size);
140               buf = b;
141             }
142           else if (link_size < buf_size && buf != buffer && alloc->realloc)
143             {
144               /* Shrink BUF before returning it.  */
145               char *b = (char *) alloc->realloc (buf, link_size);
146               if (b)
147                 buf = b;
148             }
149
150           return buf;
151         }
152
153       if (buf != buffer)
154         alloc->free (buf);
155
156       if (buf_size <= buf_size_max / 2)
157         buf_size *= 2;
158       else if (buf_size < buf_size_max)
159         buf_size = buf_size_max;
160       else
161         break;
162       buf = (char *) alloc->malloc (buf_size);
163     }
164   while (buf);
165
166   if (alloc->die)
167     alloc->die ();
168   errno = ENOMEM;
169   return NULL;
170 }
171
172 /* Use the system functions, not the gnulib overrides, because this
173    module does not depend on GNU or POSIX semantics.  See comments
174    above why this must occur here.  */
175 #undef malloc
176 #undef realloc
177
178 /* A standard allocator.  For now, only careadlinkat needs this, but
179    perhaps it should be moved to the allocator module.  */
180 static struct allocator const standard_allocator =
181   { malloc, realloc, free, NULL };