1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* areadlinkat.c -- readlinkat wrapper to return malloc'd link name
4 Unlike xreadlinkat, only call exit on failure to change directory.
6 Copyright (C) 2001, 2003-2007, 2009-2011 Free Software Foundation, Inc.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Written by Jim Meyering <jim@meyering.net>,
22 and Bruno Haible <bruno@clisp.org>,
23 and Eric Blake <ebb9@byu.net>. */
28 #include "areadlink.h"
38 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
43 /* The initial buffer size for the link value. A power of 2
44 detects arithmetic overflow earlier, but is not required. */
46 INITIAL_BUF_SIZE = 1024
49 /* Call readlinkat to get the symbolic link value of FILENAME relative to FD.
50 Return a pointer to that NUL-terminated string in malloc'd storage.
51 If readlinkat fails, return NULL and set errno (although failure to
52 change directory will issue a diagnostic and exit).
53 If realloc fails, or if the link value is longer than SIZE_MAX :-),
54 return NULL and set errno to ENOMEM. */
57 areadlinkat (int fd, char const *filename)
59 /* Allocate the initial buffer on the stack. This way, in the common
60 case of a symlink of small size, we get away with a single small malloc()
61 instead of a big malloc() followed by a shrinking realloc(). */
62 char initial_buf[INITIAL_BUF_SIZE];
64 char *buffer = initial_buf;
65 size_t buf_size = sizeof initial_buf;
69 /* Attempt to read the link into the current buffer. */
70 ssize_t link_length = readlinkat (fd, filename, buffer, buf_size);
72 /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
73 with errno == ERANGE if the buffer is too small. */
74 if (link_length < 0 && errno != ERANGE)
76 if (buffer != initial_buf)
78 int saved_errno = errno;
85 if ((size_t) link_length < buf_size)
87 buffer[link_length++] = '\0';
89 /* Return it in a chunk of memory as small as possible. */
90 if (buffer == initial_buf)
92 buffer = (char *) malloc (link_length);
94 /* errno is ENOMEM. */
96 memcpy (buffer, initial_buf, link_length);
100 /* Shrink buffer before returning it. */
101 if ((size_t) link_length < buf_size)
103 char *smaller_buffer = (char *) realloc (buffer, link_length);
105 if (smaller_buffer != NULL)
106 buffer = smaller_buffer;
112 if (buffer != initial_buf)
115 if (SSIZE_MAX < buf_size || (SIZE_MAX / 2 < SSIZE_MAX && buf_size == 0))
120 buffer = (char *) malloc (buf_size);
122 /* errno is ENOMEM. */
127 #else /* !HAVE_READLINKAT */
129 /* It is more efficient to change directories only once and call
130 areadlink, rather than repeatedly call the replacement
133 # define AT_FUNC_NAME areadlinkat
134 # define AT_FUNC_F1 areadlink
135 # define AT_FUNC_POST_FILE_PARAM_DECLS /* empty */
136 # define AT_FUNC_POST_FILE_ARGS /* empty */
137 # define AT_FUNC_RESULT char *
138 # define AT_FUNC_FAIL NULL
139 # include "at-func.c"
142 # undef AT_FUNC_POST_FILE_PARAM_DECLS
143 # undef AT_FUNC_POST_FILE_ARGS
144 # undef AT_FUNC_RESULT
147 #endif /* !HAVE_READLINKAT */