1*946379e7Schristos /* xreadlink.c -- readlink wrapper to return the link name in malloc'd storage
2*946379e7Schristos
3*946379e7Schristos Copyright (C) 2001, 2003, 2005-2006 Free Software Foundation, Inc.
4*946379e7Schristos
5*946379e7Schristos This program is free software; you can redistribute it and/or modify
6*946379e7Schristos it under the terms of the GNU General Public License as published by
7*946379e7Schristos the Free Software Foundation; either version 2, or (at your option)
8*946379e7Schristos any later version.
9*946379e7Schristos
10*946379e7Schristos This program is distributed in the hope that it will be useful,
11*946379e7Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
12*946379e7Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*946379e7Schristos GNU General Public License for more details.
14*946379e7Schristos
15*946379e7Schristos You should have received a copy of the GNU General Public License
16*946379e7Schristos along with this program; see the file COPYING.
17*946379e7Schristos If not, write to the Free Software Foundation,
18*946379e7Schristos 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19*946379e7Schristos
20*946379e7Schristos /* Written by Jim Meyering <jim@meyering.net>
21*946379e7Schristos and Bruno Haible <bruno@clisp.org>. */
22*946379e7Schristos
23*946379e7Schristos #include <config.h>
24*946379e7Schristos
25*946379e7Schristos /* Specification. */
26*946379e7Schristos #include "xreadlink.h"
27*946379e7Schristos
28*946379e7Schristos #include <stdio.h>
29*946379e7Schristos #include <string.h>
30*946379e7Schristos #include <errno.h>
31*946379e7Schristos #include <limits.h>
32*946379e7Schristos #include <sys/types.h>
33*946379e7Schristos #include <stdlib.h>
34*946379e7Schristos #if HAVE_UNISTD_H
35*946379e7Schristos # include <unistd.h>
36*946379e7Schristos #endif
37*946379e7Schristos
38*946379e7Schristos #ifndef SIZE_MAX
39*946379e7Schristos # define SIZE_MAX ((size_t) -1)
40*946379e7Schristos #endif
41*946379e7Schristos #ifndef SSIZE_MAX
42*946379e7Schristos # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
43*946379e7Schristos #endif
44*946379e7Schristos
45*946379e7Schristos #ifdef NO_XMALLOC
46*946379e7Schristos # define xmalloc malloc
47*946379e7Schristos #else
48*946379e7Schristos # include "xalloc.h"
49*946379e7Schristos #endif
50*946379e7Schristos
51*946379e7Schristos /* Call readlink to get the symbolic link value of FILENAME.
52*946379e7Schristos Return a pointer to that NUL-terminated string in malloc'd storage.
53*946379e7Schristos If readlink fails, return NULL (caller may use errno to diagnose).
54*946379e7Schristos If realloc fails, or if the link value is longer than SIZE_MAX :-),
55*946379e7Schristos give a diagnostic and exit. */
56*946379e7Schristos
57*946379e7Schristos char *
xreadlink(char const * filename)58*946379e7Schristos xreadlink (char const *filename)
59*946379e7Schristos {
60*946379e7Schristos /* The initial buffer size for the link value. A power of 2
61*946379e7Schristos detects arithmetic overflow earlier, but is not required. */
62*946379e7Schristos #define INITIAL_BUF_SIZE 1024
63*946379e7Schristos
64*946379e7Schristos /* Allocate the initial buffer on the stack. This way, in the common
65*946379e7Schristos case of a symlink of small size, we get away with a single small malloc()
66*946379e7Schristos instead of a big malloc() followed by a shrinking realloc(). */
67*946379e7Schristos char initial_buf[INITIAL_BUF_SIZE];
68*946379e7Schristos
69*946379e7Schristos char *buffer = initial_buf;
70*946379e7Schristos size_t buf_size = sizeof (initial_buf);
71*946379e7Schristos
72*946379e7Schristos while (1)
73*946379e7Schristos {
74*946379e7Schristos /* Attempt to read the link into the current buffer. */
75*946379e7Schristos ssize_t link_length = readlink (filename, buffer, buf_size);
76*946379e7Schristos
77*946379e7Schristos if (link_length < 0)
78*946379e7Schristos {
79*946379e7Schristos if (buffer != initial_buf)
80*946379e7Schristos {
81*946379e7Schristos int saved_errno = errno;
82*946379e7Schristos free (buffer);
83*946379e7Schristos errno = saved_errno;
84*946379e7Schristos }
85*946379e7Schristos return NULL;
86*946379e7Schristos }
87*946379e7Schristos
88*946379e7Schristos if ((size_t) link_length < buf_size)
89*946379e7Schristos {
90*946379e7Schristos buffer[link_length++] = '\0';
91*946379e7Schristos
92*946379e7Schristos /* Return it in a chunk of memory as small as possible. */
93*946379e7Schristos if (buffer == initial_buf)
94*946379e7Schristos {
95*946379e7Schristos buffer = (char *) xmalloc (link_length);
96*946379e7Schristos #ifdef NO_XMALLOC
97*946379e7Schristos if (buffer == NULL)
98*946379e7Schristos return NULL;
99*946379e7Schristos #endif
100*946379e7Schristos memcpy (buffer, initial_buf, link_length);
101*946379e7Schristos }
102*946379e7Schristos else
103*946379e7Schristos {
104*946379e7Schristos /* Shrink buffer before returning it. */
105*946379e7Schristos if ((size_t) link_length < buf_size)
106*946379e7Schristos {
107*946379e7Schristos char *smaller_buffer = (char *) realloc (buffer, link_length);
108*946379e7Schristos
109*946379e7Schristos if (smaller_buffer != NULL)
110*946379e7Schristos buffer = smaller_buffer;
111*946379e7Schristos }
112*946379e7Schristos }
113*946379e7Schristos return buffer;
114*946379e7Schristos }
115*946379e7Schristos
116*946379e7Schristos if (buffer != initial_buf)
117*946379e7Schristos free (buffer);
118*946379e7Schristos buf_size *= 2;
119*946379e7Schristos if (SSIZE_MAX < buf_size || (SIZE_MAX / 2 < SSIZE_MAX && buf_size == 0))
120*946379e7Schristos #ifdef NO_XMALLOC
121*946379e7Schristos return NULL;
122*946379e7Schristos #else
123*946379e7Schristos xalloc_die ();
124*946379e7Schristos #endif
125*946379e7Schristos buffer = (char *) xmalloc (buf_size);
126*946379e7Schristos #ifdef NO_XMALLOC
127*946379e7Schristos if (buffer == NULL)
128*946379e7Schristos return NULL;
129*946379e7Schristos #endif
130*946379e7Schristos }
131*946379e7Schristos }
132