1 /* Determine current working directory. Linux version.
2 Copyright (C) 1997, 1998 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 Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
27 #include <sys/syscall.h>
30 /* The "proc" filesystem provides an easy method to retrieve the value.
31 For each process, the corresponding directory contains a symbolic link
32 named `cwd'. Reading the content of this link immediate gives us the
33 information. But we have to take care for systems which do not have
34 the proc filesystem mounted. Use the POSIX implementation in this case. */
35 static char *generic_getcwd (char *buf
, size_t size
) internal_function
;
38 /* Kernel 2.1.92 introduced a third way to get the current working
39 directory: a syscall. We've got to be careful that even when
40 compiling under 2.1.92+ the libc still runs under older kernels. */
41 extern int __syscall_getcwd (char *buf
, unsigned long size
);
42 static int no_syscall_getcwd
;
43 static int no_new_dcache
= 1;
45 # define no_syscall_getcwd 1
46 static int no_new_dcache
;
50 __getcwd (char *buf
, size_t size
)
56 size_t alloc_size
= size
;
58 if (no_syscall_getcwd
&& no_new_dcache
)
59 return generic_getcwd (buf
, size
);
69 alloc_size
= PATH_MAX
;
76 path
= malloc (alloc_size
);
84 if (!no_syscall_getcwd
)
88 retval
= INLINE_SYSCALL (getcwd
, 2, path
, alloc_size
);
93 buf
= realloc (path
, (size_t) retval
);
95 /* `realloc' failed but we still have the original string. */
103 no_syscall_getcwd
= 1;
104 no_new_dcache
= 0; /* Now we will try the /proc method. */
106 else if (errno
!= ERANGE
|| buf
!= NULL
)
113 __set_errno (save_errno
);
117 n
= __readlink ("/proc/self/cwd", path
, alloc_size
- 1);
122 if (n
>= alloc_size
- 1)
132 buf
= realloc (path
, (size_t) n
+ 1);
134 /* `relloc' failed but we still have the original string. */
143 /* Set to no_new_dcache only if error indicates that proc doesn't exist. */
144 if (errno
!= EACCES
&& errno
!= ENAMETOOLONG
)
147 /* Something went wrong. Restore the error number and use the generic
149 __set_errno (save_errno
);
151 /* Don't put restrictions on the length of the path unless the user does. */
158 result
= generic_getcwd (path
, size
);
160 if (result
== NULL
&& buf
== NULL
&& size
!= 0)
165 weak_alias (__getcwd
, getcwd
)
167 /* Get the code for the generic version. */
168 #define GETCWD_RETURN_TYPE static char * internal_function
169 #define __getcwd generic_getcwd
170 #include <sysdeps/posix/getcwd.c>