]> git.cworth.org Git - tar/blob - gnu/areadlink-with-size.c
Imported Upstream version 1.23
[tar] / gnu / areadlink-with-size.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* readlink wrapper to return the link name in malloc'd storage.
4    Unlike xreadlink and xreadlink_with_size, don't ever call exit.
5
6    Copyright (C) 2001, 2003-2007, 2009-2010 Free Software Foundation, Inc.
7
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.
12
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.
17
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/>.  */
20
21 /* Written by Jim Meyering <jim@meyering.net>  */
22
23 #include <config.h>
24
25 #include "areadlink.h"
26
27 #include <errno.h>
28 #include <limits.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32
33 #ifndef SSIZE_MAX
34 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
35 #endif
36
37 /* SYMLINK_MAX is used only for an initial memory-allocation sanity
38    check, so it's OK to guess too small on hosts where there is no
39    arbitrary limit to symbolic link length.  */
40 #ifndef SYMLINK_MAX
41 # define SYMLINK_MAX 1024
42 #endif
43
44 #define MAXSIZE (SIZE_MAX < SSIZE_MAX ? SIZE_MAX : SSIZE_MAX)
45
46 /* Call readlink to get the symbolic link value of FILE.
47    SIZE is a hint as to how long the link is expected to be;
48    typically it is taken from st_size.  It need not be correct.
49    Return a pointer to that NUL-terminated string in malloc'd storage.
50    If readlink fails, malloc fails, or if the link value is longer
51    than SSIZE_MAX, return NULL (caller may use errno to diagnose).  */
52
53 char *
54 areadlink_with_size (char const *file, size_t size)
55 {
56   /* Some buggy file systems report garbage in st_size.  Defend
57      against them by ignoring outlandish st_size values in the initial
58      memory allocation.  */
59   size_t symlink_max = SYMLINK_MAX;
60   size_t INITIAL_LIMIT_BOUND = 8 * 1024;
61   size_t initial_limit = (symlink_max < INITIAL_LIMIT_BOUND
62                           ? symlink_max + 1
63                           : INITIAL_LIMIT_BOUND);
64
65   /* The initial buffer size for the link value.  */
66   size_t buf_size = size < initial_limit ? size + 1 : initial_limit;
67
68   while (1)
69     {
70       ssize_t r;
71       size_t link_length;
72       char *buffer = malloc (buf_size);
73
74       if (buffer == NULL)
75         return NULL;
76       r = readlink (file, buffer, buf_size);
77       link_length = r;
78
79       /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
80          with errno == ERANGE if the buffer is too small.  */
81       if (r < 0 && errno != ERANGE)
82         {
83           int saved_errno = errno;
84           free (buffer);
85           errno = saved_errno;
86           return NULL;
87         }
88
89       if (link_length < buf_size)
90         {
91           buffer[link_length] = 0;
92           return buffer;
93         }
94
95       free (buffer);
96       if (buf_size <= MAXSIZE / 2)
97         buf_size *= 2;
98       else if (buf_size < MAXSIZE)
99         buf_size = MAXSIZE;
100       else
101         {
102           errno = ENOMEM;
103           return NULL;
104         }
105     }
106 }