1 /* Copyright (C) 1991-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
27 /* AIX requires this to be the first thing in the file. */
28 #if defined _AIX && !defined __GNUC__
38 #include <sys/types.h>
45 #if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS
49 # define __set_errno(val) errno = (val)
56 #if defined USGr3 && !defined DIRENT
59 #if defined Xenix && !defined SYSNDIR
63 #if defined POSIX || defined DIRENT || defined __GNU_LIBRARY__
65 # ifndef __GNU_LIBRARY__
66 # define D_NAMLEN(d) strlen((d)->d_name)
68 # define HAVE_D_NAMLEN
69 # define D_NAMLEN(d) ((d)->d_namlen)
71 #else /* not POSIX or DIRENT */
72 # define dirent direct
73 # define D_NAMLEN(d) ((d)->d_namlen)
74 # define HAVE_D_NAMLEN
75 # if defined USG && !defined sgi
77 # include <sys/ndir.h>
78 # else /* Not SYSNDIR */
84 #endif /* POSIX or DIRENT or __GNU_LIBRARY__ */
86 #if defined HAVE_UNISTD_H || defined __GNU_LIBRARY__
90 #if defined STDC_HEADERS || defined __GNU_LIBRARY__ || defined POSIX
94 #else /* No standard headers. */
104 # else /* Not USG. */
110 # else /* Not NeXT. */
112 # include <strings.h>
118 extern void bzero ();
121 extern void bcopy ();
128 extern char *malloc (), *realloc ();
131 #endif /* Standard headers. */
134 # define memcpy(d, s, n) bcopy((s), (d), (n))
135 # define memmove memcpy
136 #endif /* Not ANSI_STRING. */
139 # define MAX(a, b) ((a) < (b) ? (b) : (a))
144 # define mempcpy __mempcpy
146 # define HAVE_MEMPCPY 1
149 #if !defined __alloca && !defined __GNU_LIBRARY__
153 # define alloca(n) __builtin_alloca (n)
154 # else /* Not GCC. */
155 # if defined sparc || defined HAVE_ALLOCA_H
157 # else /* Not sparc or HAVE_ALLOCA_H. */
159 extern char *alloca ();
160 # endif /* Not _AIX. */
161 # endif /* sparc or HAVE_ALLOCA_H. */
164 # define __alloca alloca
168 #if defined HAVE_LIMITS_H || defined STDC_HEADERS || defined __GNU_LIBRARY__
171 # include <sys/param.h>
175 # include <not-cancel.h>
176 # include <kernel-features.h>
178 # define __openat64_nocancel(dfd, name, mode) openat64 (dfd, name, mode)
179 # define __close_nocancel_nostatus(fd) close (fd)
184 # define PATH_MAX MAXPATHLEN
186 # define PATH_MAX 1024
190 #if !defined STDC_HEADERS && !defined __GNU_LIBRARY__
192 # define size_t unsigned int
195 #ifndef __GNU_LIBRARY__
196 # define __lstat64 stat64
200 # define __rewinddir rewinddir
204 # define __getcwd getcwd
207 #ifndef GETCWD_RETURN_TYPE
208 # define GETCWD_RETURN_TYPE char *
211 #ifdef __ASSUME_ATFCTS
212 # define __have_atfcts 1
214 static int __rtld_have_atfcts
;
215 # define __have_atfcts __rtld_have_atfcts
218 /* Get the pathname of the current working directory, and put it in SIZE
219 bytes of BUF. Returns NULL if the directory couldn't be determined or
220 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
221 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
222 unless SIZE == 0, in which case it is as big as necessary. */
225 __getcwd (char *buf
, size_t size
)
227 #ifndef __ASSUME_ATFCTS
228 static const char dots
[]
229 = "../../../../../../../../../../../../../../../../../../../../../../../\
230 ../../../../../../../../../../../../../../../../../../../../../../../../../../\
231 ../../../../../../../../../../../../../../../../../../../../../../../../../..";
232 const char *dotp
= &dots
[sizeof (dots
)];
233 const char *dotlist
= dots
;
234 size_t dotsize
= sizeof (dots
) - 1;
236 int prev_errno
= errno
;
237 DIR *dirstream
= NULL
;
238 bool fd_needs_closing
= false;
242 #ifndef NO_ALLOCATION
243 size_t allocated
= size
;
248 __set_errno (EINVAL
);
252 allocated
= PATH_MAX
+ 1;
257 path
= malloc (allocated
);
263 # define allocated size
267 char *pathp
= path
+ allocated
;
271 if (__lstat64 (".", &st
) < 0)
273 dev_t thisdev
= st
.st_dev
;
274 ino_t thisino
= st
.st_ino
;
276 if (__lstat64 ("/", &st
) < 0)
278 dev_t rootdev
= st
.st_dev
;
279 ino_t rootino
= st
.st_ino
;
281 while (!(thisdev
== rootdev
&& thisino
== rootino
))
283 if (__have_atfcts
>= 0)
284 fd
= __openat64_nocancel (fd
, "..", O_RDONLY
| O_CLOEXEC
);
289 fd_needs_closing
= true;
290 if (__fstat64 (fd
, &st
) < 0)
293 #ifndef __ASSUME_ATFCTS
294 else if (errno
== ENOSYS
)
298 /* Look at the parent directory. */
301 # ifdef NO_ALLOCATION
302 __set_errno (ENOMEM
);
305 /* My, what a deep directory tree you have, Grandma. */
309 new = malloc (dotsize
* 2 + 1);
313 dotp
= mempcpy (new, dots
, dotsize
);
315 memcpy (new, dots
, dotsize
);
316 dotp
= &new[dotsize
];
321 new = realloc ((void *) dotlist
, dotsize
* 2 + 1);
324 dotp
= &new[dotsize
];
327 *((char *) mempcpy ((char *) dotp
, new, dotsize
)) = '\0';
330 memcpy ((char *) dotp
, new, dotsize
);
340 /* Figure out if this directory is a mount point. */
341 if (__lstat64 (dotp
, &st
) < 0)
348 if (dirstream
&& __closedir (dirstream
) != 0)
354 dev_t dotdev
= st
.st_dev
;
355 ino_t dotino
= st
.st_ino
;
356 bool mount_point
= dotdev
!= thisdev
;
358 /* Search for the last directory. */
359 if (__have_atfcts
>= 0)
360 dirstream
= __fdopendir (fd
);
361 #ifndef __ASSUME_ATFCTS
363 dirstream
= __opendir (dotp
);
365 if (dirstream
== NULL
)
367 fd_needs_closing
= false;
370 bool use_d_ino
= true;
373 /* Clear errno to distinguish EOF from error if readdir returns
376 d
= __readdir (dirstream
);
381 /* When we've iterated through all directory entries
382 without finding one with a matching d_ino, rewind the
383 stream and consider each name again, but this time, using
384 lstat64. This is necessary in a chroot on at least one
389 __rewinddir (dirstream
);
393 /* EOF on dirstream, which means that the current directory
395 __set_errno (ENOENT
);
400 #ifdef _DIRENT_HAVE_D_TYPE
401 if (d
->d_type
!= DT_DIR
&& d
->d_type
!= DT_UNKNOWN
)
404 if (d
->d_name
[0] == '.'
405 && (d
->d_name
[1] == '\0'
406 || (d
->d_name
[1] == '.' && d
->d_name
[2] == '\0')))
408 if (use_d_ino
&& !mount_point
&& (ino_t
) d
->d_ino
!= thisino
)
411 if (__have_atfcts
>= 0)
413 /* We don't fail here if we cannot stat64() a directory entry.
414 This can happen when (network) filesystems fail. If this
415 entry is in fact the one we are looking for we will find
416 out soon as we reach the end of the directory without
417 having found anything. */
418 if (__fstatat64 (fd
, d
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0)
421 #ifndef __ASSUME_ATFCTS
424 char name
[dotlist
+ dotsize
- dotp
+ 1 + _D_ALLOC_NAMLEN (d
)];
426 char *tmp
= mempcpy (name
, dotp
, dotlist
+ dotsize
- dotp
);
428 strcpy (tmp
, d
->d_name
);
430 memcpy (name
, dotp
, dotlist
+ dotsize
- dotp
);
431 name
[dotlist
+ dotsize
- dotp
] = '/';
432 strcpy (&name
[dotlist
+ dotsize
- dotp
+ 1], d
->d_name
);
434 /* We don't fail here if we cannot stat64() a directory entry.
435 This can happen when (network) filesystems fail. If this
436 entry is in fact the one we are looking for we will find
437 out soon as we reach the end of the directory without
438 having found anything. */
439 if (__lstat64 (name
, &st
) < 0)
443 if (S_ISDIR (st
.st_mode
)
444 && st
.st_dev
== thisdev
&& st
.st_ino
== thisino
)
448 size_t namlen
= _D_EXACT_NAMLEN (d
);
450 if ((size_t) (pathp
- path
) <= namlen
)
452 #ifndef NO_ALLOCATION
455 size_t oldsize
= allocated
;
457 allocated
= 2 * MAX (allocated
, namlen
);
458 char *tmp
= realloc (path
, allocated
);
462 /* Move current contents up to the end of the buffer.
463 This is guaranteed to be non-overlapping. */
464 pathp
= memcpy (tmp
+ allocated
- (path
+ oldsize
- pathp
),
465 tmp
+ (pathp
- path
),
466 path
+ oldsize
- pathp
);
472 __set_errno (ERANGE
);
477 (void) memcpy (pathp
, d
->d_name
, namlen
);
484 if (dirstream
!= NULL
&& __closedir (dirstream
) != 0)
490 if (pathp
== &path
[allocated
- 1])
493 #ifndef __ASSUME_ATFCTS
495 free ((void *) dotlist
);
498 size_t used
= path
+ allocated
- pathp
;
499 memmove (path
, pathp
, used
);
502 /* Ensure that the buffer is only as large as necessary. */
503 buf
= realloc (path
, used
);
506 /* Either buf was NULL all along, or `realloc' failed but
507 we still have the original string. */
510 /* Restore errno on successful return. */
511 __set_errno (prev_errno
);
516 int save_errno
= errno
;
517 #ifndef __ASSUME_ATFCTS
519 free ((void *) dotlist
);
521 if (dirstream
!= NULL
)
522 __closedir (dirstream
);
523 if (fd_needs_closing
)
524 __close_nocancel_nostatus (fd
);
525 #ifndef NO_ALLOCATION
529 __set_errno (save_errno
);
533 #if defined _LIBC && !defined __getcwd
534 libc_hidden_def (__getcwd
)
535 weak_alias (__getcwd
, getcwd
)