exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / getcwd-lgpl.c
blob67ed2878e6e0d574910d5004d3e21f1feb37bd08
1 /* Copyright (C) 2011-2024 Free Software Foundation, Inc.
2 This file is part of gnulib.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 /* Specification */
20 #include <unistd.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #if GNULIB_GETCWD
27 /* Favor GPL getcwd.c if both getcwd and getcwd-lgpl modules are in use. */
28 typedef int dummy;
29 #else
31 /* Get the name of the current working directory, and put it in SIZE
32 bytes of BUF. Returns NULL if the directory couldn't be determined
33 (perhaps because the absolute name was longer than PATH_MAX, or
34 because of missing read/search permissions on parent directories)
35 or SIZE was too small. If successful, returns BUF. If BUF is
36 NULL, an array is allocated with 'malloc'; the array is SIZE bytes
37 long, unless SIZE == 0, in which case it is as big as
38 necessary. */
40 # undef getcwd
41 # if defined _WIN32 && !defined __CYGWIN__
42 # define getcwd _getcwd
43 # endif
45 char *
46 rpl_getcwd (char *buf, size_t size)
48 char *result;
50 /* Handle single size operations. */
51 if (buf)
53 /* Check SIZE argument. */
54 if (!size)
56 errno = EINVAL;
57 return NULL;
59 return getcwd (buf, size);
62 if (size)
64 buf = malloc (size);
65 if (!buf)
67 errno = ENOMEM;
68 return NULL;
70 result = getcwd (buf, size);
71 if (!result)
72 free (buf);
73 return result;
76 /* Flexible sizing requested. Avoid over-allocation for the common
77 case of a name that fits within a 4k page, minus some space for
78 local variables, to be sure we don't skip over a guard page. */
80 char tmp[4032];
81 size = sizeof tmp;
82 char *ptr = getcwd (tmp, size);
83 if (ptr)
85 result = strdup (ptr);
86 if (!result)
87 errno = ENOMEM;
88 return result;
90 if (errno != ERANGE)
91 return NULL;
94 /* My what a large directory name we have. */
97 size <<= 1;
98 char *ptr = realloc (buf, size);
99 if (ptr == NULL)
101 free (buf);
102 errno = ENOMEM;
103 return NULL;
105 buf = ptr;
106 result = getcwd (buf, size);
108 while (!result && errno == ERANGE);
110 if (!result)
111 free (buf);
112 else
114 /* Here result == buf. */
115 /* Shrink result before returning it. */
116 size_t actual_size = strlen (result) + 1;
117 if (actual_size < size)
119 char *shrinked_result = realloc (result, actual_size);
120 if (shrinked_result != NULL)
121 result = shrinked_result;
124 return result;
127 #endif