1 /* readlinkat wrapper to return the link name in malloc'd storage.
2 Unlike xreadlinkat, only call exit on failure to change directory.
4 Copyright (C) 2001, 2003-2007, 2009 Free Software Foundation, 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 <http://www.gnu.org/licenses/>. */
19 /* Written by Jim Meyering <jim@meyering.net>
20 and Eric Blake <ebb9@byu.net>. */
24 #include "areadlink.h"
29 #include <sys/types.h>
36 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
39 /* SYMLINK_MAX is used only for an initial memory-allocation sanity
40 check, so it's OK to guess too small on hosts where there is no
41 arbitrary limit to symbolic link length. */
43 # define SYMLINK_MAX 1024
46 # define MAXSIZE (SIZE_MAX < SSIZE_MAX ? SIZE_MAX : SSIZE_MAX)
48 /* Call readlinkat to get the symbolic link value of FILE, relative to FD.
49 SIZE is a hint as to how long the link is expected to be;
50 typically it is taken from st_size. It need not be correct.
51 Return a pointer to that NUL-terminated string in malloc'd storage.
52 If readlinkat fails, malloc fails, or if the link value is longer
53 than SSIZE_MAX, return NULL (caller may use errno to diagnose).
54 However, failure to change directory during readlinkat will issue
55 a diagnostic and exit. */
58 areadlinkat_with_size (int fd, char const *file, size_t size)
60 /* Some buggy file systems report garbage in st_size. Defend
61 against them by ignoring outlandish st_size values in the initial
63 size_t symlink_max = SYMLINK_MAX;
64 size_t INITIAL_LIMIT_BOUND = 8 * 1024;
65 size_t initial_limit = (symlink_max < INITIAL_LIMIT_BOUND
67 : INITIAL_LIMIT_BOUND);
69 /* The initial buffer size for the link value. */
70 size_t buf_size = size < initial_limit ? size + 1 : initial_limit;
76 char *buffer = malloc (buf_size);
80 r = readlinkat (fd, file, buffer, buf_size);
83 /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
84 with errno == ERANGE if the buffer is too small. */
85 if (r < 0 && errno != ERANGE)
87 int saved_errno = errno;
93 if (link_length < buf_size)
95 buffer[link_length] = 0;
100 if (buf_size <= MAXSIZE / 2)
102 else if (buf_size < MAXSIZE)
112 #else /* !HAVE_READLINKAT */
115 /* It is more efficient to change directories only once and call
116 areadlink_with_size, rather than repeatedly call the replacement
119 # define AT_FUNC_NAME areadlinkat_with_size
120 # define AT_FUNC_F1 areadlink_with_size
121 # define AT_FUNC_POST_FILE_PARAM_DECLS , size_t size
122 # define AT_FUNC_POST_FILE_ARGS , size
123 # define AT_FUNC_RESULT char *
124 # define AT_FUNC_FAIL NULL
125 # include "at-func.c"
128 # undef AT_FUNC_POST_FILE_PARAM_DECLS
129 # undef AT_FUNC_POST_FILE_ARGS
130 # undef AT_FUNC_RESULT
133 #endif /* !HAVE_READLINKAT */