1 /* Determine current working directory. Linux version.
2 Copyright (C) 1997,1998,1999,2000,2002,2003,2006
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 #include <sys/syscall.h>
30 #include <bp-checks.h>
32 #include <kernel-features.h>
35 /* If we compile the file for use in ld.so we don't need the feature
36 that getcwd() allocates the buffers itself. */
38 # define NO_ALLOCATION 1
42 #if __ASSUME_GETCWD_SYSCALL > 0
43 /* Kernel 2.1.92 introduced a third way to get the current working
44 directory: a syscall. We've got to be careful that even when
45 compiling under 2.1.92+ the libc still runs under older kernels. */
46 # define no_syscall_getcwd 0
47 # define have_new_dcache 1
48 /* This is a trick since we don't define generic_getcwd. */
49 # define generic_getcwd getcwd
51 /* The "proc" filesystem provides an easy method to retrieve the value.
52 For each process, the corresponding directory contains a symbolic link
53 named `cwd'. Reading the content of this link immediate gives us the
54 information. But we have to take care for systems which do not have
55 the proc filesystem mounted. Use the POSIX implementation in this case. */
56 static char *generic_getcwd (char *buf
, size_t size
) internal_function
;
59 /* Kernel 2.1.92 introduced a third way to get the current working
60 directory: a syscall. We've got to be careful that even when
61 compiling under 2.1.92+ the libc still runs under older kernels. */
62 static int no_syscall_getcwd
;
63 static int have_new_dcache
;
65 # define no_syscall_getcwd 1
66 static int have_new_dcache
= 1;
71 __getcwd (char *buf
, size_t size
)
77 if (no_syscall_getcwd
&& !have_new_dcache
)
78 return generic_getcwd (buf
, size
);
81 size_t alloc_size
= size
;
90 alloc_size
= MAX (PATH_MAX
, __getpagesize ());
95 path
= malloc (alloc_size
);
101 # define alloc_size size
105 #if defined __NR_getcwd || __LINUX_GETCWD_SYSCALL > 0
106 if (!no_syscall_getcwd
)
110 retval
= INLINE_SYSCALL (getcwd
, 2, CHECK_STRING (path
), alloc_size
);
113 # ifndef NO_ALLOCATION
114 if (buf
== NULL
&& size
== 0)
115 /* Ensure that the buffer is only as large as necessary. */
116 buf
= realloc (path
, (size_t) retval
);
119 /* Either buf was NULL all along, or `realloc' failed but
120 we still have the original string. */
127 # if __ASSUME_GETCWD_SYSCALL
128 /* It should never happen that the `getcwd' syscall failed because
129 the buffer is too small if we allocated the buffer ourselves
131 assert (errno
!= ERANGE
|| buf
!= NULL
|| size
!= 0);
133 # ifndef NO_ALLOCATION
142 no_syscall_getcwd
= 1;
143 have_new_dcache
= 1; /* Now we will try the /proc method. */
145 else if (errno
!= ERANGE
|| buf
!= NULL
)
147 # ifndef NO_ALLOCATION
157 n
= __readlink ("/proc/self/cwd", path
, alloc_size
- 1);
162 if ((size_t) n
>= alloc_size
- 1)
164 #ifndef NO_ALLOCATION
172 #ifndef NO_ALLOCATION
173 if (buf
== NULL
&& size
== 0)
174 /* Ensure that the buffer is only as large as necessary. */
175 buf
= realloc (path
, (size_t) n
+ 1);
177 /* Either buf was NULL all along, or `realloc' failed but
178 we still have the original string. */
184 #ifndef have_new_dcache
190 #if __ASSUME_GETCWD_SYSCALL == 0
191 /* Set to have_new_dcache only if error indicates that proc doesn't
193 if (errno
!= EACCES
&& errno
!= ENAMETOOLONG
)
197 #ifndef NO_ALLOCATION
198 /* Don't put restrictions on the length of the path unless the user does. */
206 result
= generic_getcwd (path
, size
);
208 #ifndef NO_ALLOCATION
209 if (result
== NULL
&& buf
== NULL
&& size
!= 0)
215 weak_alias (__getcwd
, getcwd
)
217 #if __ASSUME_GETCWD_SYSCALL == 0
218 /* Get the code for the generic version. */
219 # define GETCWD_RETURN_TYPE static char * internal_function
220 # define __getcwd generic_getcwd
221 # include <sysdeps/posix/getcwd.c>