1 /* Determine current working directory. Linux version.
2 Copyright (C) 1997 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 /* The "proc" filesystem provides an easy method to retrieve the value.
28 For each process, the corresponding directory contains a symbolic link
29 named `cwd'. Reading the content of this link immediate gives us the
30 information. But we have to take care for systems which do not have
31 the proc filesystem mounted. Use the POSIX implementation in this case. */
32 static char *generic_getcwd (char *buf
, size_t size
) internal_function
;
35 __getcwd (char *buf
, size_t size
)
37 static int no_new_dcache
= 0;
42 size_t alloc_size
= size
;
45 return generic_getcwd (buf
, size
);
55 alloc_size
= PATH_MAX
+ 1;
62 path
= malloc (alloc_size
);
69 n
= __readlink ("/proc/self/cwd", path
, alloc_size
- 1);
74 if (n
>= alloc_size
- 1)
82 return buf
?: (char *) realloc (path
, (size_t) n
+ 1);
88 /* Set to no_new_dcache only if error indicates that proc doesn't exist. */
89 if (errno
!= EACCES
&& errno
!= ENAMETOOLONG
)
92 /* Something went wrong. Restore the error number and use the generic
94 __set_errno (save_errno
);
96 /* Don't put restrictions on the length of the path unless the user does. */
103 result
= generic_getcwd (path
, size
);
105 if (result
== NULL
&& buf
== NULL
&& size
!= 0)
110 weak_alias (__getcwd
, getcwd
)
112 /* Get the code for the generic version. */
113 #define GETCWD_RETURN_TYPE static char * internal_function
114 #define __getcwd generic_getcwd
115 #include <sysdeps/posix/getcwd.c>