1 /* Copyright (C) 2011-2021 Free Software Foundation, Inc.
2 This file is part of gnulib.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program 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 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 /* Specification */
20 #include <unistd.h>
21
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25
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
30
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. */
39
40 # undef getcwd
41 # if defined _WIN32 && !defined __CYGWIN__
42 # define getcwd _getcwd
43 # endif
44
45 char *
rpl_getcwd(char * buf,size_t size)46 rpl_getcwd (char *buf, size_t size)
47 {
48 char *ptr;
49 char *result;
50
51 /* Handle single size operations. */
52 if (buf)
53 {
54 if (!size)
55 {
56 errno = EINVAL;
57 return NULL;
58 }
59 return getcwd (buf, size);
60 }
61
62 if (size)
63 {
64 buf = malloc (size);
65 if (!buf)
66 {
67 errno = ENOMEM;
68 return NULL;
69 }
70 result = getcwd (buf, size);
71 if (!result)
72 {
73 int saved_errno = errno;
74 free (buf);
75 errno = saved_errno;
76 }
77 return result;
78 }
79
80 /* Flexible sizing requested. Avoid over-allocation for the common
81 case of a name that fits within a 4k page, minus some space for
82 local variables, to be sure we don't skip over a guard page. */
83 {
84 char tmp[4032];
85 size = sizeof tmp;
86 ptr = getcwd (tmp, size);
87 if (ptr)
88 {
89 result = strdup (ptr);
90 if (!result)
91 errno = ENOMEM;
92 return result;
93 }
94 if (errno != ERANGE)
95 return NULL;
96 }
97
98 /* My what a large directory name we have. */
99 do
100 {
101 size <<= 1;
102 ptr = realloc (buf, size);
103 if (ptr == NULL)
104 {
105 free (buf);
106 errno = ENOMEM;
107 return NULL;
108 }
109 buf = ptr;
110 result = getcwd (buf, size);
111 }
112 while (!result && errno == ERANGE);
113
114 if (!result)
115 {
116 int saved_errno = errno;
117 free (buf);
118 errno = saved_errno;
119 }
120 else
121 {
122 /* Here result == buf. */
123 /* Shrink result before returning it. */
124 size_t actual_size = strlen (result) + 1;
125 if (actual_size < size)
126 {
127 char *shrinked_result = realloc (result, actual_size);
128 if (shrinked_result != NULL)
129 result = shrinked_result;
130 }
131 }
132 return result;
133 }
134
135 #endif
136