Update.
[glibc.git] / sysdeps / unix / sysv / linux / getcwd.c
blobbbe21d627f32610d5b6ec34279a4fef4d06d0b69
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. */
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <unistd.h>
26 #include <sysdep.h>
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;
37 #ifdef __NR_getcwd
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;
44 #else
45 # define no_syscall_getcwd 1
46 static int no_new_dcache;
47 #endif
49 char *
50 __getcwd (char *buf, size_t size)
52 int save_errno;
53 char *path;
54 int n;
55 char *result;
56 size_t alloc_size = size;
58 if (no_syscall_getcwd && no_new_dcache)
59 return generic_getcwd (buf, size);
61 if (size == 0)
63 if (buf != NULL)
65 __set_errno (EINVAL);
66 return NULL;
69 alloc_size = PATH_MAX;
72 if (buf != NULL)
73 path = buf;
74 else
76 path = malloc (alloc_size);
77 if (path == NULL)
78 return NULL;
81 save_errno = errno;
83 #ifdef __NR_getcwd
84 if (!no_syscall_getcwd)
86 int retval;
88 retval = INLINE_SYSCALL (getcwd, 2, path, alloc_size);
89 if (retval >= 0)
91 if (buf == NULL)
93 buf = realloc (path, (size_t) retval);
94 if (buf == NULL)
95 /* `realloc' failed but we still have the original string. */
96 buf = path;
98 return buf;
101 if (errno == ENOSYS)
103 no_syscall_getcwd = 1;
104 no_new_dcache = 0; /* Now we will try the /proc method. */
106 else if (errno != ERANGE || buf != NULL)
108 if (buf == NULL)
109 free (path);
110 return NULL;
113 __set_errno (save_errno);
115 #endif
117 n = __readlink ("/proc/self/cwd", path, alloc_size - 1);
118 if (n != -1)
120 if (path[0] == '/')
122 if (n >= alloc_size - 1)
124 if (buf == NULL)
125 free (path);
126 return NULL;
129 path[n] = '\0';
130 if (buf == NULL)
132 buf = realloc (path, (size_t) n + 1);
133 if (buf == NULL)
134 /* `relloc' failed but we still have the original string. */
135 buf = path;
137 return buf;
139 else
140 no_new_dcache = 1;
143 /* Set to no_new_dcache only if error indicates that proc doesn't exist. */
144 if (errno != EACCES && errno != ENAMETOOLONG)
145 no_new_dcache = 1;
147 /* Something went wrong. Restore the error number and use the generic
148 version. */
149 __set_errno (save_errno);
151 /* Don't put restrictions on the length of the path unless the user does. */
152 if (size == 0)
154 free (path);
155 path = NULL;
158 result = generic_getcwd (path, size);
160 if (result == NULL && buf == NULL && size != 0)
161 free (path);
163 return result;
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>