Move bits/types.h into posix/bits.
[glibc.git] / sysdeps / posix / getcwd.c
blob0246e91d7cbcf9b707555bc71ce945b309721260
1 /* Copyright (C) 1991-2017 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 <http://www.gnu.org/licenses/>. */
18 /* Wants:
19 AC_STDC_HEADERS
20 AC_DIR_HEADER
21 AC_UNISTD_H
22 AC_MEMORY_H
23 AC_CONST
24 AC_ALLOCA
27 /* AIX requires this to be the first thing in the file. */
28 #if defined _AIX && !defined __GNUC__
29 #pragma alloca
30 #endif
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
41 #ifdef STDC_HEADERS
42 # include <stddef.h>
43 #endif
45 #if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS
46 extern int errno;
47 #endif
48 #ifndef __set_errno
49 # define __set_errno(val) errno = (val)
50 #endif
52 #ifndef NULL
53 # define NULL 0
54 #endif
56 #if defined USGr3 && !defined DIRENT
57 # define DIRENT
58 #endif /* USGr3 */
59 #if defined Xenix && !defined SYSNDIR
60 # define SYSNDIR
61 #endif /* Xenix */
63 #if defined POSIX || defined DIRENT || defined __GNU_LIBRARY__
64 # include <dirent.h>
65 # ifndef __GNU_LIBRARY__
66 # define D_NAMLEN(d) strlen((d)->d_name)
67 # else
68 # define HAVE_D_NAMLEN
69 # define D_NAMLEN(d) ((d)->d_namlen)
70 # endif
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
76 # if defined SYSNDIR
77 # include <sys/ndir.h>
78 # else /* Not SYSNDIR */
79 # include "ndir.h"
80 # endif /* SYSNDIR */
81 # else /* not USG */
82 # include <sys/dir.h>
83 # endif /* USG */
84 #endif /* POSIX or DIRENT or __GNU_LIBRARY__ */
86 #if defined HAVE_UNISTD_H || defined __GNU_LIBRARY__
87 # include <unistd.h>
88 #endif
90 #if defined STDC_HEADERS || defined __GNU_LIBRARY__ || defined POSIX
91 # include <stdlib.h>
92 # include <string.h>
93 # define ANSI_STRING
94 #else /* No standard headers. */
96 # ifdef USG
98 # include <string.h>
99 # ifdef NEED_MEMORY_H
100 # include <memory.h>
101 # endif
102 # define ANSI_STRING
104 # else /* Not USG. */
106 # ifdef NeXT
108 # include <string.h>
110 # else /* Not NeXT. */
112 # include <strings.h>
114 # ifndef bcmp
115 extern int bcmp ();
116 # endif
117 # ifndef bzero
118 extern void bzero ();
119 # endif
120 # ifndef bcopy
121 extern void bcopy ();
122 # endif
124 # endif /* NeXT. */
126 # endif /* USG. */
128 extern char *malloc (), *realloc ();
129 extern void free ();
131 #endif /* Standard headers. */
133 #ifndef ANSI_STRING
134 # define memcpy(d, s, n) bcopy((s), (d), (n))
135 # define memmove memcpy
136 #endif /* Not ANSI_STRING. */
138 #ifndef MAX
139 # define MAX(a, b) ((a) < (b) ? (b) : (a))
140 #endif
142 #ifdef _LIBC
143 # ifndef mempcpy
144 # define mempcpy __mempcpy
145 # endif
146 # define HAVE_MEMPCPY 1
147 #endif
149 #if !defined __alloca && !defined __GNU_LIBRARY__
151 # ifdef __GNUC__
152 # undef alloca
153 # define alloca(n) __builtin_alloca (n)
154 # else /* Not GCC. */
155 # if defined sparc || defined HAVE_ALLOCA_H
156 # include <alloca.h>
157 # else /* Not sparc or HAVE_ALLOCA_H. */
158 # ifndef _AIX
159 extern char *alloca ();
160 # endif /* Not _AIX. */
161 # endif /* sparc or HAVE_ALLOCA_H. */
162 # endif /* GCC. */
164 # define __alloca alloca
166 #endif
168 #if defined HAVE_LIMITS_H || defined STDC_HEADERS || defined __GNU_LIBRARY__
169 # include <limits.h>
170 #else
171 # include <sys/param.h>
172 #endif
174 #if defined _LIBC
175 # include <not-cancel.h>
176 # include <kernel-features.h>
177 #else
178 # define openat64_not_cancel_3(dfd, name, mode) openat64 (dfd, name, mode)
179 # define close_not_cancel_no_status(fd) close (fd)
180 #endif
182 #ifndef PATH_MAX
183 # ifdef MAXPATHLEN
184 # define PATH_MAX MAXPATHLEN
185 # else
186 # define PATH_MAX 1024
187 # endif
188 #endif
190 #if !defined STDC_HEADERS && !defined __GNU_LIBRARY__
191 # undef size_t
192 # define size_t unsigned int
193 #endif
195 #ifndef __GNU_LIBRARY__
196 # define __lstat64 stat64
197 #endif
199 #ifndef _LIBC
200 # define __rewinddir rewinddir
201 #endif
203 #ifndef _LIBC
204 # define __getcwd getcwd
205 #endif
207 #ifndef GETCWD_RETURN_TYPE
208 # define GETCWD_RETURN_TYPE char *
209 #endif
211 #ifdef __ASSUME_ATFCTS
212 # define __have_atfcts 1
213 #elif IS_IN (rtld)
214 static int __rtld_have_atfcts;
215 # define __have_atfcts __rtld_have_atfcts
216 #endif
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. */
224 GETCWD_RETURN_TYPE
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;
235 #endif
236 int prev_errno = errno;
237 DIR *dirstream = NULL;
238 bool fd_needs_closing = false;
239 int fd = AT_FDCWD;
241 char *path;
242 #ifndef NO_ALLOCATION
243 size_t allocated = size;
244 if (size == 0)
246 if (buf != NULL)
248 __set_errno (EINVAL);
249 return NULL;
252 allocated = PATH_MAX + 1;
255 if (buf == NULL)
257 path = malloc (allocated);
258 if (path == NULL)
259 return NULL;
261 else
262 #else
263 # define allocated size
264 #endif
265 path = buf;
267 char *pathp = path + allocated;
268 *--pathp = '\0';
270 struct stat64 st;
271 if (__lstat64 (".", &st) < 0)
272 goto lose;
273 dev_t thisdev = st.st_dev;
274 ino_t thisino = st.st_ino;
276 if (__lstat64 ("/", &st) < 0)
277 goto lose;
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)
285 int mode = O_RDONLY;
286 #ifdef O_CLOEXEC
287 mode |= O_CLOEXEC;
288 #endif
289 fd = openat64_not_cancel_3 (fd, "..", mode);
291 else
292 fd = -1;
293 if (fd >= 0)
295 fd_needs_closing = true;
296 if (__fstat64 (fd, &st) < 0)
297 goto lose;
299 #ifndef __ASSUME_ATFCTS
300 else if (errno == ENOSYS)
302 __have_atfcts = -1;
304 /* Look at the parent directory. */
305 if (dotp == dotlist)
307 # ifdef NO_ALLOCATION
308 __set_errno (ENOMEM);
309 goto lose;
310 # else
311 /* My, what a deep directory tree you have, Grandma. */
312 char *new;
313 if (dotlist == dots)
315 new = malloc (dotsize * 2 + 1);
316 if (new == NULL)
317 goto lose;
318 # ifdef HAVE_MEMPCPY
319 dotp = mempcpy (new, dots, dotsize);
320 # else
321 memcpy (new, dots, dotsize);
322 dotp = &new[dotsize];
323 # endif
325 else
327 new = realloc ((__ptr_t) dotlist, dotsize * 2 + 1);
328 if (new == NULL)
329 goto lose;
330 dotp = &new[dotsize];
332 # ifdef HAVE_MEMPCPY
333 *((char *) mempcpy ((char *) dotp, new, dotsize)) = '\0';
334 dotsize *= 2;
335 # else
336 memcpy ((char *) dotp, new, dotsize);
337 dotsize *= 2;
338 new[dotsize] = '\0';
339 # endif
340 dotlist = new;
341 # endif
344 dotp -= 3;
346 /* Figure out if this directory is a mount point. */
347 if (__lstat64 (dotp, &st) < 0)
348 goto lose;
350 #endif
351 else
352 goto lose;
354 if (dirstream && __closedir (dirstream) != 0)
356 dirstream = NULL;
357 goto lose;
360 dev_t dotdev = st.st_dev;
361 ino_t dotino = st.st_ino;
362 bool mount_point = dotdev != thisdev;
364 /* Search for the last directory. */
365 if (__have_atfcts >= 0)
366 dirstream = __fdopendir (fd);
367 #ifndef __ASSUME_ATFCTS
368 else
369 dirstream = __opendir (dotp);
370 #endif
371 if (dirstream == NULL)
372 goto lose;
373 fd_needs_closing = false;
375 struct dirent *d;
376 bool use_d_ino = true;
377 while (1)
379 /* Clear errno to distinguish EOF from error if readdir returns
380 NULL. */
381 __set_errno (0);
382 d = __readdir (dirstream);
383 if (d == NULL)
385 if (errno == 0)
387 /* When we've iterated through all directory entries
388 without finding one with a matching d_ino, rewind the
389 stream and consider each name again, but this time, using
390 lstat64. This is necessary in a chroot on at least one
391 system. */
392 if (use_d_ino)
394 use_d_ino = false;
395 __rewinddir (dirstream);
396 continue;
399 /* EOF on dirstream, which means that the current directory
400 has been removed. */
401 __set_errno (ENOENT);
403 goto lose;
406 #ifdef _DIRENT_HAVE_D_TYPE
407 if (d->d_type != DT_DIR && d->d_type != DT_UNKNOWN)
408 continue;
409 #endif
410 if (d->d_name[0] == '.'
411 && (d->d_name[1] == '\0'
412 || (d->d_name[1] == '.' && d->d_name[2] == '\0')))
413 continue;
414 if (use_d_ino && !mount_point && (ino_t) d->d_ino != thisino)
415 continue;
417 if (__have_atfcts >= 0)
419 /* We don't fail here if we cannot stat64() a directory entry.
420 This can happen when (network) filesystems fail. If this
421 entry is in fact the one we are looking for we will find
422 out soon as we reach the end of the directory without
423 having found anything. */
424 if (__fstatat64 (fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
425 continue;
427 #ifndef __ASSUME_ATFCTS
428 else
430 char name[dotlist + dotsize - dotp + 1 + _D_ALLOC_NAMLEN (d)];
431 # ifdef HAVE_MEMPCPY
432 char *tmp = mempcpy (name, dotp, dotlist + dotsize - dotp);
433 *tmp++ = '/';
434 strcpy (tmp, d->d_name);
435 # else
436 memcpy (name, dotp, dotlist + dotsize - dotp);
437 name[dotlist + dotsize - dotp] = '/';
438 strcpy (&name[dotlist + dotsize - dotp + 1], d->d_name);
439 # endif
440 /* We don't fail here if we cannot stat64() a directory entry.
441 This can happen when (network) filesystems fail. If this
442 entry is in fact the one we are looking for we will find
443 out soon as we reach the end of the directory without
444 having found anything. */
445 if (__lstat64 (name, &st) < 0)
446 continue;
448 #endif
449 if (S_ISDIR (st.st_mode)
450 && st.st_dev == thisdev && st.st_ino == thisino)
451 break;
454 size_t namlen = _D_EXACT_NAMLEN (d);
456 if ((size_t) (pathp - path) <= namlen)
458 #ifndef NO_ALLOCATION
459 if (size == 0)
461 size_t oldsize = allocated;
463 allocated = 2 * MAX (allocated, namlen);
464 char *tmp = realloc (path, allocated);
465 if (tmp == NULL)
466 goto lose;
468 /* Move current contents up to the end of the buffer.
469 This is guaranteed to be non-overlapping. */
470 pathp = memcpy (tmp + allocated - (path + oldsize - pathp),
471 tmp + (pathp - path),
472 path + oldsize - pathp);
473 path = tmp;
475 else
476 #endif
478 __set_errno (ERANGE);
479 goto lose;
482 pathp -= namlen;
483 (void) memcpy (pathp, d->d_name, namlen);
484 *--pathp = '/';
486 thisdev = dotdev;
487 thisino = dotino;
490 if (dirstream != NULL && __closedir (dirstream) != 0)
492 dirstream = NULL;
493 goto lose;
496 if (pathp == &path[allocated - 1])
497 *--pathp = '/';
499 #ifndef __ASSUME_ATFCTS
500 if (dotlist != dots)
501 free ((__ptr_t) dotlist);
502 #endif
504 size_t used = path + allocated - pathp;
505 memmove (path, pathp, used);
507 if (size == 0)
508 /* Ensure that the buffer is only as large as necessary. */
509 buf = realloc (path, used);
511 if (buf == NULL)
512 /* Either buf was NULL all along, or `realloc' failed but
513 we still have the original string. */
514 buf = path;
516 /* Restore errno on successful return. */
517 __set_errno (prev_errno);
519 return buf;
521 lose:;
522 int save_errno = errno;
523 #ifndef __ASSUME_ATFCTS
524 if (dotlist != dots)
525 free ((__ptr_t) dotlist);
526 #endif
527 if (dirstream != NULL)
528 __closedir (dirstream);
529 if (fd_needs_closing)
530 close_not_cancel_no_status (fd);
531 #ifndef NO_ALLOCATION
532 if (buf == NULL)
533 free (path);
534 #endif
535 __set_errno (save_errno);
536 return NULL;
539 #if defined _LIBC && !defined __getcwd
540 weak_alias (__getcwd, getcwd)
541 #endif