Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / lib / areadlink-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 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
21 #include <config.h>
22
23 #include "areadlink.h"
24
25 #include <stdio.h>
26 #include <errno.h>
27 #include <limits.h>
28 #include <sys/types.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31
32 #ifndef SIZE_MAX
33 # define SIZE_MAX ((size_t) -1)
34 #endif
35 #ifndef SSIZE_MAX
36 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
37 #endif
38
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.  */
42 #ifndef SYMLINK_MAX
43 # define SYMLINK_MAX 1024
44 #endif
45
46 #define MAXSIZE (SIZE_MAX < SSIZE_MAX ? SIZE_MAX : SSIZE_MAX)
47
48 /* Call readlink to get the symbolic link value of FILE.
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 readlink fails, malloc fails, or if the link value is longer
53    than SSIZE_MAX, return NULL (caller may use errno to diagnose).  */
54
55 char *
56 areadlink_with_size (char const *file, size_t size)
57 {
58   /* Some buggy file systems report garbage in st_size.  Defend
59      against them by ignoring outlandish st_size values in the initial
60      memory allocation.  */
61   size_t symlink_max = SYMLINK_MAX;
62   size_t INITIAL_LIMIT_BOUND = 8 * 1024;
63   size_t initial_limit = (symlink_max < INITIAL_LIMIT_BOUND
64                           ? symlink_max + 1
65                           : INITIAL_LIMIT_BOUND);
66
67   /* The initial buffer size for the link value.  */
68   size_t buf_size = size < initial_limit ? size + 1 : initial_limit;
69
70   while (1)
71     {
72       ssize_t r;
73       size_t link_length;
74       char *buffer = malloc (buf_size);
75
76       if (buffer == NULL)
77         return NULL;
78       r = readlink (file, buffer, buf_size);
79       link_length = r;
80
81       /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
82          with errno == ERANGE if the buffer is too small.  */
83       if (r < 0 && errno != ERANGE)
84         {
85           int saved_errno = errno;
86           free (buffer);
87           errno = saved_errno;
88           return NULL;
89         }
90
91       if (link_length < buf_size)
92         {
93           buffer[link_length] = 0;
94           return buffer;
95         }
96
97       free (buffer);
98       if (buf_size <= MAXSIZE / 2)
99         buf_size *= 2;
100       else if (buf_size < MAXSIZE)
101         buf_size = MAXSIZE;
102       else
103         {
104           errno = ENOMEM;
105           return NULL;
106         }
107     }
108 }