Care about dangling symbolic links.
[gnulib.git] / lib / mreadlink-with-size.c
1 /* readlink wrapper to return the link name in malloc'd storage.
2    Unlike xreadlink and xreadlink_with_size, don't ever call exit.
3
4    Copyright (C) 2001, 2003-2007 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 2, or (at your option)
9    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; see the file COPYING.
18    If not, write to the Free Software Foundation,
19    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21 /* Written by Jim Meyering <jim@meyering.net>  */
22
23 #include <config.h>
24
25 #include "mreadlink.h"
26
27 #include <stdio.h>
28 #include <errno.h>
29 #include <limits.h>
30 #include <sys/types.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #ifndef SIZE_MAX
35 # define SIZE_MAX ((size_t) -1)
36 #endif
37 #ifndef SSIZE_MAX
38 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
39 #endif
40
41 /* SYMLINK_MAX is used only for an initial memory-allocation sanity
42    check, so it's OK to guess too small on hosts where there is no
43    arbitrary limit to symbolic link length.  */
44 #ifndef SYMLINK_MAX
45 # define SYMLINK_MAX 1024
46 #endif
47
48 #define MAXSIZE (SIZE_MAX < SSIZE_MAX ? SIZE_MAX : SSIZE_MAX)
49
50 /* Call readlink to get the symbolic link value of FILE.
51    SIZE is a hint as to how long the link is expected to be;
52    typically it is taken from st_size.  It need not be correct.
53    Return a pointer to that NUL-terminated string in malloc'd storage.
54    If readlink fails, malloc fails, or if the link value is longer
55    than SSIZE_MAX, return NULL (caller may use errno to diagnose).  */
56
57 char *
58 mreadlink_with_size (char const *file, size_t size)
59 {
60   /* Some buggy file systems report garbage in st_size.  Defend
61      against them by ignoring outlandish st_size values in the initial
62      memory allocation.  */
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
66                           ? symlink_max + 1
67                           : INITIAL_LIMIT_BOUND);
68
69   /* The initial buffer size for the link value.  */
70   size_t buf_size = size < initial_limit ? size + 1 : initial_limit;
71
72   while (1)
73     {
74       ssize_t r;
75       size_t link_length;
76       char *buffer = malloc (buf_size);
77
78       if (buffer == NULL)
79         return NULL;
80       r = readlink (file, buffer, buf_size);
81       link_length = r;
82
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)
86         {
87           int saved_errno = errno;
88           free (buffer);
89           errno = saved_errno;
90           return NULL;
91         }
92
93       if (link_length < buf_size)
94         {
95           buffer[link_length] = 0;
96           return buffer;
97         }
98
99       free (buffer);
100       if (buf_size <= MAXSIZE / 2)
101         buf_size *= 2;
102       else if (buf_size < MAXSIZE)
103         buf_size = MAXSIZE;
104       else
105         {
106           errno = ENOMEM;
107           return NULL;
108         }
109     }
110 }