exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / areadlink-with-size.c
blobea2fe7f51b11b93462709aaefcdb5374d2a6d6a4
1 /* readlink wrapper to return the link name in malloc'd storage.
2 Unlike xreadlink and xreadlink_with_size, don't ever call exit.
4 Copyright (C) 2001, 2003-2007, 2009-2024 Free Software Foundation, Inc.
6 This file is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 This file 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 /* Written by Jim Meyering <jim@meyering.net> */
21 #include <config.h>
23 #include "areadlink.h"
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 /* SYMLINK_MAX is used only for an initial memory-allocation sanity
33 check, so it's OK to guess too small on hosts where there is no
34 arbitrary limit to symbolic link length. */
35 #ifndef SYMLINK_MAX
36 # define SYMLINK_MAX 1024
37 #endif
39 #define MAXSIZE (SIZE_MAX < SSIZE_MAX ? SIZE_MAX : SSIZE_MAX)
41 /* Call readlink to get the symbolic link value of FILE.
42 SIZE is a hint as to how long the link is expected to be;
43 typically it is taken from st_size. It need not be correct.
44 Return a pointer to that NUL-terminated string in malloc'd storage.
45 If readlink fails, malloc fails, or if the link value is longer
46 than SSIZE_MAX, return NULL (caller may use errno to diagnose). */
48 char *
49 areadlink_with_size (char const *file, size_t size)
51 /* Some buggy file systems report garbage in st_size. Defend
52 against them by ignoring outlandish st_size values in the initial
53 memory allocation. */
54 size_t symlink_max = SYMLINK_MAX;
55 size_t INITIAL_LIMIT_BOUND = 8 * 1024;
56 size_t initial_limit = (symlink_max < INITIAL_LIMIT_BOUND
57 ? symlink_max + 1
58 : INITIAL_LIMIT_BOUND);
60 enum { stackbuf_size = 128 };
62 /* The initial buffer size for the link value. */
63 size_t buf_size = (size == 0 ? stackbuf_size
64 : size < initial_limit ? size + 1 : initial_limit);
66 while (1)
68 ssize_t r;
69 size_t link_length;
70 char stackbuf[stackbuf_size];
71 char *buf = stackbuf;
72 char *buffer = NULL;
74 if (! (size == 0 && buf_size == stackbuf_size))
76 buf = buffer = malloc (buf_size);
77 if (!buffer)
79 errno = ENOMEM;
80 return NULL;
84 r = readlink (file, buf, buf_size);
85 link_length = r;
87 if (r < 0)
89 free (buffer);
90 return NULL;
93 if (link_length < buf_size)
95 buf[link_length] = 0;
96 if (!buffer)
98 buffer = malloc (link_length + 1);
99 if (buffer)
100 return memcpy (buffer, buf, link_length + 1);
102 else if (link_length + 1 < buf_size)
104 /* Shrink BUFFER before returning it. */
105 char *shrinked_buffer = realloc (buffer, link_length + 1);
106 if (shrinked_buffer != NULL)
107 buffer = shrinked_buffer;
109 return buffer;
112 free (buffer);
113 if (buf_size <= MAXSIZE / 2)
114 buf_size *= 2;
115 else if (buf_size < MAXSIZE)
116 buf_size = MAXSIZE;
117 else
119 errno = ENOMEM;
120 return NULL;