1 | /* Determine current working directory. Linux version. |
2 | Copyright (C) 1997-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <assert.h> |
20 | #include <errno.h> |
21 | #include <limits.h> |
22 | #include <stdlib.h> |
23 | #include <unistd.h> |
24 | #include <sys/param.h> |
25 | |
26 | #include <sysdep.h> |
27 | #include <sys/syscall.h> |
28 | |
29 | |
30 | /* If we compile the file for use in ld.so we don't need the feature |
31 | that getcwd() allocates the buffers itself. */ |
32 | #if IS_IN (rtld) |
33 | # define NO_ALLOCATION 1 |
34 | #endif |
35 | |
36 | |
37 | /* The "proc" filesystem provides an easy method to retrieve the value. |
38 | For each process, the corresponding directory contains a symbolic link |
39 | named `cwd'. Reading the content of this link immediate gives us the |
40 | information. But we have to take care for systems which do not have |
41 | the proc filesystem mounted. Use the POSIX implementation in this case. */ |
42 | |
43 | /* Get the code for the generic version. */ |
44 | #define GETCWD_RETURN_TYPE static char * |
45 | #include <sysdeps/posix/getcwd.c> |
46 | |
47 | char * |
48 | __getcwd (char *buf, size_t size) |
49 | { |
50 | char *path; |
51 | char *result; |
52 | |
53 | #ifndef NO_ALLOCATION |
54 | size_t alloc_size = size; |
55 | if (size == 0) |
56 | { |
57 | if (buf != NULL) |
58 | { |
59 | __set_errno (EINVAL); |
60 | return NULL; |
61 | } |
62 | |
63 | alloc_size = MAX (PATH_MAX, __getpagesize ()); |
64 | } |
65 | |
66 | if (buf == NULL) |
67 | { |
68 | path = malloc (alloc_size); |
69 | if (path == NULL) |
70 | return NULL; |
71 | } |
72 | else |
73 | #else |
74 | # define alloc_size size |
75 | #endif |
76 | path = buf; |
77 | |
78 | int retval; |
79 | |
80 | retval = INLINE_SYSCALL (getcwd, 2, path, alloc_size); |
81 | if (retval > 0 && path[0] == '/') |
82 | { |
83 | #ifndef NO_ALLOCATION |
84 | if (buf == NULL && size == 0) |
85 | /* Ensure that the buffer is only as large as necessary. */ |
86 | buf = realloc (path, (size_t) retval); |
87 | |
88 | if (buf == NULL) |
89 | /* Either buf was NULL all along, or `realloc' failed but |
90 | we still have the original string. */ |
91 | buf = path; |
92 | #endif |
93 | |
94 | return buf; |
95 | } |
96 | |
97 | /* The system call either cannot handle paths longer than a page |
98 | or can succeed without returning an absolute path. Just use the |
99 | generic implementation right away. */ |
100 | if (retval >= 0 || errno == ENAMETOOLONG) |
101 | { |
102 | #ifndef NO_ALLOCATION |
103 | if (buf == NULL && size == 0) |
104 | { |
105 | free (path); |
106 | path = NULL; |
107 | } |
108 | #endif |
109 | |
110 | result = __getcwd_generic (path, size); |
111 | |
112 | #ifndef NO_ALLOCATION |
113 | if (result == NULL && buf == NULL && size != 0) |
114 | free (path); |
115 | #endif |
116 | |
117 | return result; |
118 | } |
119 | |
120 | /* It should never happen that the `getcwd' syscall failed because |
121 | the buffer is too small if we allocated the buffer ourselves |
122 | large enough. */ |
123 | assert (errno != ERANGE || buf != NULL || size != 0); |
124 | |
125 | #ifndef NO_ALLOCATION |
126 | if (buf == NULL) |
127 | free (path); |
128 | #endif |
129 | |
130 | return NULL; |
131 | } |
132 | libc_hidden_def (__getcwd) |
133 | weak_alias (__getcwd, getcwd) |
134 | |