This source file includes following definitions.
- rpl_getcwd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #include <config.h>
18
19
20 #include <unistd.h>
21
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #if GNULIB_GETCWD
27
28 typedef int dummy;
29 #else
30
31
32
33
34
35
36
37
38
39
40 # undef getcwd
41 # if defined _WIN32 && !defined __CYGWIN__
42 # define getcwd _getcwd
43 # endif
44
45 char *
46 rpl_getcwd (char *buf, size_t size)
47 {
48 char *ptr;
49 char *result;
50
51
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 free (buf);
73 return result;
74 }
75
76
77
78
79 {
80 char tmp[4032];
81 size = sizeof tmp;
82 ptr = getcwd (tmp, size);
83 if (ptr)
84 {
85 result = strdup (ptr);
86 if (!result)
87 errno = ENOMEM;
88 return result;
89 }
90 if (errno != ERANGE)
91 return NULL;
92 }
93
94
95 do
96 {
97 size <<= 1;
98 ptr = realloc (buf, size);
99 if (ptr == NULL)
100 {
101 free (buf);
102 errno = ENOMEM;
103 return NULL;
104 }
105 buf = ptr;
106 result = getcwd (buf, size);
107 }
108 while (!result && errno == ERANGE);
109
110 if (!result)
111 free (buf);
112 else
113 {
114
115
116 size_t actual_size = strlen (result) + 1;
117 if (actual_size < size)
118 {
119 char *shrinked_result = realloc (result, actual_size);
120 if (shrinked_result != NULL)
121 result = shrinked_result;
122 }
123 }
124 return result;
125 }
126
127 #endif