maint: update copyright
[gnulib.git] / lib / areadlinkat-with-size.c
1 /* readlinkat wrapper to return the link name in malloc'd storage.
2    Unlike xreadlinkat, only call exit on failure to change directory.
3
4    Copyright (C) 2001, 2003-2007, 2009-2014 Free Software Foundation, 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 Jim Meyering <jim@meyering.net>
20    and Eric Blake <ebb9@byu.net>.  */
21
22 #include <config.h>
23
24 #include "areadlink.h"
25
26 #include <errno.h>
27 #include <limits.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31
32 #if HAVE_READLINKAT
33
34 # ifndef SSIZE_MAX
35 #  define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
36 # endif
37
38 /* SYMLINK_MAX is used only for an initial memory-allocation sanity
39    check, so it's OK to guess too small on hosts where there is no
40    arbitrary limit to symbolic link length.  */
41 # ifndef SYMLINK_MAX
42 #  define SYMLINK_MAX 1024
43 # endif
44
45 # define MAXSIZE (SIZE_MAX < SSIZE_MAX ? SIZE_MAX : SSIZE_MAX)
46
47 /* Call readlinkat to get the symbolic link value of FILE, relative to FD.
48    SIZE is a hint as to how long the link is expected to be;
49    typically it is taken from st_size.  It need not be correct.
50    Return a pointer to that NUL-terminated string in malloc'd storage.
51    If readlinkat fails, malloc fails, or if the link value is longer
52    than SSIZE_MAX, return NULL (caller may use errno to diagnose).
53    However, failure to change directory during readlinkat will issue
54    a diagnostic and exit.  */
55
56 char *
57 areadlinkat_with_size (int fd, char const *file, size_t size)
58 {
59   /* Some buggy file systems report garbage in st_size.  Defend
60      against them by ignoring outlandish st_size values in the initial
61      memory allocation.  */
62   size_t symlink_max = SYMLINK_MAX;
63   size_t INITIAL_LIMIT_BOUND = 8 * 1024;
64   size_t initial_limit = (symlink_max < INITIAL_LIMIT_BOUND
65                           ? symlink_max + 1
66                           : INITIAL_LIMIT_BOUND);
67
68   /* The initial buffer size for the link value.  */
69   size_t buf_size = size < initial_limit ? size + 1 : initial_limit;
70
71   while (1)
72     {
73       ssize_t r;
74       size_t link_length;
75       char *buffer = malloc (buf_size);
76
77       if (buffer == NULL)
78         return NULL;
79       r = readlinkat (fd, file, buffer, buf_size);
80       link_length = r;
81
82       /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
83          with errno == ERANGE if the buffer is too small.  */
84       if (r < 0 && errno != ERANGE)
85         {
86           int saved_errno = errno;
87           free (buffer);
88           errno = saved_errno;
89           return NULL;
90         }
91
92       if (link_length < buf_size)
93         {
94           buffer[link_length] = 0;
95           return buffer;
96         }
97
98       free (buffer);
99       if (buf_size <= MAXSIZE / 2)
100         buf_size *= 2;
101       else if (buf_size < MAXSIZE)
102         buf_size = MAXSIZE;
103       else
104         {
105           errno = ENOMEM;
106           return NULL;
107         }
108     }
109 }
110
111 #else /* !HAVE_READLINKAT */
112
113
114 /* It is more efficient to change directories only once and call
115    areadlink_with_size, rather than repeatedly call the replacement
116    readlinkat.  */
117
118 # define AT_FUNC_NAME areadlinkat_with_size
119 # define AT_FUNC_F1 areadlink_with_size
120 # define AT_FUNC_POST_FILE_PARAM_DECLS , size_t size
121 # define AT_FUNC_POST_FILE_ARGS        , size
122 # define AT_FUNC_RESULT char *
123 # define AT_FUNC_FAIL NULL
124 # include "at-func.c"
125 # undef AT_FUNC_NAME
126 # undef AT_FUNC_F1
127 # undef AT_FUNC_POST_FILE_PARAM_DECLS
128 # undef AT_FUNC_POST_FILE_ARGS
129 # undef AT_FUNC_RESULT
130 # undef AT_FUNC_FAIL
131
132 #endif /* !HAVE_READLINKAT */