* fts.c (fts_open): Tiny comment change.
[gnulib.git] / 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, 2006 Free Software
4    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 2, or (at your option)
9    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; see the file COPYING.
18    If not, write to the Free Software Foundation,
19    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21 /* Written by Jim Meyering <jim@meyering.net>  */
22
23 #include <config.h>
24
25 #include "xreadlink.h"
26
27 #include <stdio.h>
28 #include <errno.h>
29 #include <limits.h>
30 #include <sys/types.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #ifndef SIZE_MAX
35 # define SIZE_MAX ((size_t) -1)
36 #endif
37 #ifndef SSIZE_MAX
38 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
39 #endif
40
41 #define MAXSIZE (SIZE_MAX < SSIZE_MAX ? SIZE_MAX : SSIZE_MAX)
42
43 #include "xalloc.h"
44
45 /* Call readlink to get the symbolic link value of FILE.
46    SIZE is a hint as to how long the link is expected to be;
47    typically it is taken from st_size.  It need not be correct.
48    Return a pointer to that NUL-terminated string in malloc'd storage.
49    If readlink fails, return NULL (caller may use errno to diagnose).
50    If malloc fails, or if the link value is longer than SSIZE_MAX :-),
51    give a diagnostic and exit.  */
52
53 char *
54 xreadlink (char const *file, size_t size)
55 {
56   /* The initial buffer size for the link value.  A power of 2
57      detects arithmetic overflow earlier, but is not required.  */
58   size_t buf_size = size < MAXSIZE ? size + 1 : MAXSIZE;
59
60   while (1)
61     {
62       char *buffer = xmalloc (buf_size);
63       ssize_t r = readlink (file, buffer, buf_size);
64       size_t link_length = r;
65
66       /* On AIX 5L v5.3 and HP-UX 11i v2 04/09, readlink returns -1
67          with errno == ERANGE if the buffer is too small.  */
68       if (r < 0 && errno != ERANGE)
69         {
70           int saved_errno = errno;
71           free (buffer);
72           errno = saved_errno;
73           return NULL;
74         }
75
76       if (link_length < buf_size)
77         {
78           buffer[link_length] = 0;
79           return buffer;
80         }
81
82       free (buffer);
83       if (buf_size <= MAXSIZE / 2)
84         buf_size *= 2;
85       else if (buf_size < MAXSIZE)
86         buf_size = MAXSIZE;
87       else
88         xalloc_die ();
89     }
90 }