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