Import a stripped down cvs-1.12.13
[dragonfly.git] / contrib / cvs-1.12 / lib / xreadlink.c
1 /* xreadlink.c -- readlink wrapper to return the link name in malloc'd storage
2
3    Copyright (C) 2001, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; see the file COPYING.
17    If not, write to the Free Software Foundation,
18    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* Written by Jim Meyering <jim@meyering.net>  */
21
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "xreadlink.h"
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <sys/types.h>
32 #include <stdlib.h>
33 #if HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36
37 #ifndef SIZE_MAX
38 # define SIZE_MAX ((size_t) -1)
39 #endif
40 #ifndef SSIZE_MAX
41 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
42 #endif
43
44 #define MAXSIZE (SIZE_MAX < SSIZE_MAX ? SIZE_MAX : SSIZE_MAX)
45
46 #include "xalloc.h"
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, return NULL (caller may use errno to diagnose).
53    If malloc fails, or if the link value is longer than SSIZE_MAX :-),
54    give a diagnostic and exit.  */
55
56 char *
57 xreadlink (char const *file, size_t size)
58 {
59   /* The initial buffer size for the link value.  A power of 2
60      detects arithmetic overflow earlier, but is not required.  */
61   size_t buf_size = size < MAXSIZE ? size + 1 : MAXSIZE;
62
63   while (1)
64     {
65       char *buffer = xmalloc (buf_size);
66       ssize_t r = readlink (file, buffer, buf_size);
67       size_t link_length = r;
68
69       /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
70          with errno == ERANGE if the buffer is too small.  */
71       if (r < 0 && errno != ERANGE)
72         {
73           int saved_errno = errno;
74           free (buffer);
75           errno = saved_errno;
76           return NULL;
77         }
78
79       if (link_length < buf_size)
80         {
81           buffer[link_length] = 0;
82           return buffer;
83         }
84
85       free (buffer);
86       if (buf_size <= MAXSIZE / 2)
87         buf_size *= 2;
88       else if (buf_size < MAXSIZE)
89         buf_size = MAXSIZE;
90       else
91         xalloc_die ();
92     }
93 }