Update.
[glibc.git] / sysdeps / unix / sysv / linux / getcwd.c
blob9695d8a035e9035aa719bae9ded8272ab3931309
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
19 02111-1307 USA. */
21 #include <assert.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <unistd.h>
27 #include <sysdep.h>
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. */
36 #ifdef IS_IN_rtld
37 # define NO_ALLOCATION 1
38 #endif
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
49 #else
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;
57 # if __NR_getcwd
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;
63 # else
64 # define no_syscall_getcwd 1
65 static int have_new_dcache = 1;
66 # endif
67 #endif
69 char *
70 __getcwd (char *buf, size_t size)
72 char *path;
73 int n;
74 char *result;
76 if (no_syscall_getcwd && !have_new_dcache)
77 return generic_getcwd (buf, size);
79 #ifndef NO_ALLOCATION
80 size_t alloc_size = size;
81 if (size == 0)
83 if (buf != NULL)
85 __set_errno (EINVAL);
86 return NULL;
89 alloc_size = PATH_MAX;
92 if (buf == NULL)
94 path = malloc (alloc_size);
95 if (path == NULL)
96 return NULL;
98 else
99 #else
100 # define alloc_size size
101 #endif
102 path = buf;
104 #if defined __NR_getcwd || __LINUX_GETCWD_SYSCALL > 0
105 if (!no_syscall_getcwd)
107 int retval;
109 retval = INLINE_SYSCALL (getcwd, 2, CHECK_STRING (path), alloc_size);
110 if (retval >= 0)
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);
117 if (buf == NULL)
118 /* Either buf was NULL all along, or `realloc' failed but
119 we still have the original string. */
120 buf = path;
121 # endif
123 return buf;
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
129 large enough. */
130 assert (errno != ERANGE || buf != NULL || size != 0);
132 # ifndef NO_ALLOCATION
133 if (buf == NULL)
134 free (path);
135 # endif
137 return NULL;
138 # else
139 if (errno == ENOSYS)
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
147 if (buf == NULL)
148 free (path);
149 # endif
150 return NULL;
152 # endif
154 #endif
156 n = __readlink ("/proc/self/cwd", path, alloc_size - 1);
157 if (n != -1)
159 if (path[0] == '/')
161 if ((size_t) n >= alloc_size - 1)
163 #ifndef NO_ALLOCATION
164 if (buf == NULL)
165 free (path);
166 #endif
167 return NULL;
170 path[n] = '\0';
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);
175 if (buf == NULL)
176 /* Either buf was NULL all along, or `realloc' failed but
177 we still have the original string. */
178 buf = path;
179 #endif
181 return buf;
183 #ifndef have_new_dcache
184 else
185 have_new_dcache = 0;
186 #endif
189 #if __ASSUME_GETCWD_SYSCALL == 0
190 /* Set to have_new_dcache only if error indicates that proc doesn't
191 exist. */
192 if (errno != EACCES && errno != ENAMETOOLONG)
193 have_new_dcache = 0;
194 #endif
196 #ifndef NO_ALLOCATION
197 /* Don't put restrictions on the length of the path unless the user does. */
198 if (size == 0)
200 free (path);
201 path = NULL;
203 #endif
205 result = generic_getcwd (path, size);
207 #ifndef NO_ALLOCATION
208 if (result == NULL && buf == NULL && size != 0)
209 free (path);
210 #endif
212 return result;
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>
221 #endif