2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / getcwd.c
blob911d85f43d461d4ebcf57bdd044ea95e189c7bd7
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
20 02111-1307 USA. */
22 #include <assert.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdlib.h>
26 #include <unistd.h>
28 #include <sysdep.h>
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. */
37 #ifdef IS_IN_rtld
38 # define NO_ALLOCATION 1
39 #endif
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
50 #else
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;
58 # if __NR_getcwd
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;
64 # else
65 # define no_syscall_getcwd 1
66 static int have_new_dcache = 1;
67 # endif
68 #endif
70 char *
71 __getcwd (char *buf, size_t size)
73 char *path;
74 int n;
75 char *result;
77 if (no_syscall_getcwd && !have_new_dcache)
78 return generic_getcwd (buf, size);
80 #ifndef NO_ALLOCATION
81 size_t alloc_size = size;
82 if (size == 0)
84 if (buf != NULL)
86 __set_errno (EINVAL);
87 return NULL;
90 alloc_size = MAX (PATH_MAX, __getpagesize ());
93 if (buf == NULL)
95 path = malloc (alloc_size);
96 if (path == NULL)
97 return NULL;
99 else
100 #else
101 # define alloc_size size
102 #endif
103 path = buf;
105 #if defined __NR_getcwd || __LINUX_GETCWD_SYSCALL > 0
106 if (!no_syscall_getcwd)
108 int retval;
110 retval = INLINE_SYSCALL (getcwd, 2, CHECK_STRING (path), alloc_size);
111 if (retval >= 0)
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);
118 if (buf == NULL)
119 /* Either buf was NULL all along, or `realloc' failed but
120 we still have the original string. */
121 buf = path;
122 # endif
124 return buf;
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
130 large enough. */
131 assert (errno != ERANGE || buf != NULL || size != 0);
133 # ifndef NO_ALLOCATION
134 if (buf == NULL)
135 free (path);
136 # endif
138 return NULL;
139 # else
140 if (errno == ENOSYS)
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
148 if (buf == NULL)
149 free (path);
150 # endif
151 return NULL;
153 # endif
155 #endif
157 n = __readlink ("/proc/self/cwd", path, alloc_size - 1);
158 if (n != -1)
160 if (path[0] == '/')
162 if ((size_t) n >= alloc_size - 1)
164 #ifndef NO_ALLOCATION
165 if (buf == NULL)
166 free (path);
167 #endif
168 return NULL;
171 path[n] = '\0';
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);
176 if (buf == NULL)
177 /* Either buf was NULL all along, or `realloc' failed but
178 we still have the original string. */
179 buf = path;
180 #endif
182 return buf;
184 #ifndef have_new_dcache
185 else
186 have_new_dcache = 0;
187 #endif
190 #if __ASSUME_GETCWD_SYSCALL == 0
191 /* Set to have_new_dcache only if error indicates that proc doesn't
192 exist. */
193 if (errno != EACCES && errno != ENAMETOOLONG)
194 have_new_dcache = 0;
195 #endif
197 #ifndef NO_ALLOCATION
198 /* Don't put restrictions on the length of the path unless the user does. */
199 if (size == 0)
201 free (path);
202 path = NULL;
204 #endif
206 result = generic_getcwd (path, size);
208 #ifndef NO_ALLOCATION
209 if (result == NULL && buf == NULL && size != 0)
210 free (path);
211 #endif
213 return result;
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>
222 #endif