]> git.cworth.org Git - tar/blob - gnu/areadlink.c
Imported Upstream version 1.24
[tar] / gnu / areadlink.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* areadlink.c -- 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    and Bruno Haible <bruno@clisp.org>.  */
23
24 #include <config.h>
25
26 /* Specification.  */
27 #include "areadlink.h"
28
29 #include <errno.h>
30 #include <limits.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 #ifndef SSIZE_MAX
37 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
38 #endif
39
40 /* Use the system functions, not the gnulib overrides in this file.  */
41 #undef malloc
42 #undef realloc
43
44 /* The initial buffer size for the link value.  A power of 2
45    detects arithmetic overflow earlier, but is not required.  */
46 enum {
47   INITIAL_BUF_SIZE = 1024
48 };
49
50 /* Call readlink to get the symbolic link value of FILENAME.
51    Return a pointer to that NUL-terminated string in malloc'd storage.
52    If readlink fails, return NULL and set errno.
53    If realloc fails, or if the link value is longer than SIZE_MAX :-),
54    return NULL and set errno to ENOMEM.  */
55
56 char *
57 areadlink (char const *filename)
58 {
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];
63
64   char *buffer = initial_buf;
65   size_t buf_size = sizeof initial_buf;
66
67   while (1)
68     {
69       /* Attempt to read the link into the current buffer.  */
70       ssize_t link_length = readlink (filename, buffer, buf_size);
71
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)
75         {
76           if (buffer != initial_buf)
77             {
78               int saved_errno = errno;
79               free (buffer);
80               errno = saved_errno;
81             }
82           return NULL;
83         }
84
85       if ((size_t) link_length < buf_size)
86         {
87           buffer[link_length++] = '\0';
88
89           /* Return it in a chunk of memory as small as possible.  */
90           if (buffer == initial_buf)
91             {
92               buffer = (char *) malloc (link_length);
93               if (buffer == NULL)
94                 {
95                   /* It's easier to set errno to ENOMEM than to rely on the
96                      'malloc-posix' gnulib module.  */
97                   errno = ENOMEM;
98                   return NULL;
99                 }
100               memcpy (buffer, initial_buf, link_length);
101             }
102           else
103             {
104               /* Shrink buffer before returning it.  */
105               if ((size_t) link_length < buf_size)
106                 {
107                   char *smaller_buffer = (char *) realloc (buffer, link_length);
108
109                   if (smaller_buffer != NULL)
110                     buffer = smaller_buffer;
111                 }
112             }
113           return buffer;
114         }
115
116       if (buffer != initial_buf)
117         free (buffer);
118       buf_size *= 2;
119       if (SSIZE_MAX < buf_size || (SIZE_MAX / 2 < SSIZE_MAX && buf_size == 0))
120         {
121           errno = ENOMEM;
122           return NULL;
123         }
124       buffer = (char *) malloc (buf_size);
125       if (buffer == NULL)
126         {
127           /* It's easier to set errno to ENOMEM than to rely on the
128              'malloc-posix' gnulib module.  */
129           errno = ENOMEM;
130           return NULL;
131         }
132     }
133 }