1 /* Determine current working directory. Linux version.
2 Copyright (C) 1997,1998,1999,2000,2002,2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <sys/syscall.h>
29 #include <bp-checks.h>
31 #include "kernel-features.h"
34 /* If we compile the file for use in ld.so we don't need the feature
35 that getcwd() allocates the buffers itself. */
37 # define NO_ALLOCATION 1
41 #if __ASSUME_GETCWD_SYSCALL > 0
42 /* Kernel 2.1.92 introduced a third way to get the current working
43 directory: a syscall. We've got to be careful that even when
44 compiling under 2.1.92+ the libc still runs under older kernels. */
45 # define no_syscall_getcwd 0
46 # define have_new_dcache 1
47 /* This is a trick since we don't define generic_getcwd. */
48 # define generic_getcwd getcwd
50 /* The "proc" filesystem provides an easy method to retrieve the value.
51 For each process, the corresponding directory contains a symbolic link
52 named `cwd'. Reading the content of this link immediate gives us the
53 information. But we have to take care for systems which do not have
54 the proc filesystem mounted. Use the POSIX implementation in this case. */
55 static char *generic_getcwd (char *buf
, size_t size
) internal_function
;
58 /* Kernel 2.1.92 introduced a third way to get the current working
59 directory: a syscall. We've got to be careful that even when
60 compiling under 2.1.92+ the libc still runs under older kernels. */
61 static int no_syscall_getcwd
;
62 static int have_new_dcache
;
64 # define no_syscall_getcwd 1
65 static int have_new_dcache
= 1;
70 __getcwd (char *buf
, size_t size
)
76 if (no_syscall_getcwd
&& !have_new_dcache
)
77 return generic_getcwd (buf
, size
);
80 size_t alloc_size
= size
;
89 alloc_size
= PATH_MAX
;
94 path
= malloc (alloc_size
);
100 # define alloc_size size
104 #if defined __NR_getcwd || __LINUX_GETCWD_SYSCALL > 0
105 if (!no_syscall_getcwd
)
109 retval
= INLINE_SYSCALL (getcwd
, 2, CHECK_STRING (path
), alloc_size
);
112 # ifndef NO_ALLOCATION
113 if (buf
== NULL
&& size
== 0)
114 /* Ensure that the buffer is only as large as necessary. */
115 buf
= realloc (path
, (size_t) retval
);
118 /* Either buf was NULL all along, or `realloc' failed but
119 we still have the original string. */
126 # if __ASSUME_GETCWD_SYSCALL
127 /* It should never happen that the `getcwd' syscall failed because
128 the buffer is too small if we allocated the buffer ourselves
130 assert (errno
!= ERANGE
|| buf
!= NULL
|| size
!= 0);
132 # ifndef NO_ALLOCATION
141 no_syscall_getcwd
= 1;
142 have_new_dcache
= 1; /* Now we will try the /proc method. */
144 else if (errno
!= ERANGE
|| buf
!= NULL
)
146 # ifndef NO_ALLOCATION
156 n
= __readlink ("/proc/self/cwd", path
, alloc_size
- 1);
161 if ((size_t) n
>= alloc_size
- 1)
163 #ifndef NO_ALLOCATION
171 #ifndef NO_ALLOCATION
172 if (buf
== NULL
&& size
== 0)
173 /* Ensure that the buffer is only as large as necessary. */
174 buf
= realloc (path
, (size_t) n
+ 1);
176 /* Either buf was NULL all along, or `realloc' failed but
177 we still have the original string. */
183 #ifndef have_new_dcache
189 #if __ASSUME_GETCWD_SYSCALL == 0
190 /* Set to have_new_dcache only if error indicates that proc doesn't
192 if (errno
!= EACCES
&& errno
!= ENAMETOOLONG
)
196 #ifndef NO_ALLOCATION
197 /* Don't put restrictions on the length of the path unless the user does. */
205 result
= generic_getcwd (path
, size
);
207 #ifndef NO_ALLOCATION
208 if (result
== NULL
&& buf
== NULL
&& size
!= 0)
214 weak_alias (__getcwd
, getcwd
)
216 #if __ASSUME_GETCWD_SYSCALL == 0
217 /* Get the code for the generic version. */
218 # define GETCWD_RETURN_TYPE static char * internal_function
219 # define __getcwd generic_getcwd
220 # include <sysdeps/posix/getcwd.c>