(RLIM_INFINITY): Correct for LFS support.
[glibc/history.git] / sysdeps / unix / sysv / linux / getcwd.c
blobed649f8fefdd3ca63b23bc8158294902f8580238
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. */
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <unistd.h>
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;
34 char *
35 __getcwd (char *buf, size_t size)
37 static int no_new_dcache = 0;
38 int save_errno;
39 char *path;
40 int n;
41 char *result;
42 size_t alloc_size = size;
44 if (no_new_dcache)
45 return generic_getcwd (buf, size);
47 if (size == 0)
49 if (buf != NULL)
51 __set_errno (EINVAL);
52 return NULL;
55 alloc_size = PATH_MAX + 1;
58 if (buf != NULL)
59 path = buf;
60 else
62 path = malloc (alloc_size);
63 if (path == NULL)
64 return NULL;
67 save_errno = errno;
69 n = __readlink ("/proc/self/cwd", path, alloc_size - 1);
70 if (n != -1)
72 if (path[0] == '/')
74 if (n >= alloc_size - 1)
76 if (buf == NULL)
77 free (path);
78 return NULL;
81 path[n] = '\0';
82 return buf ?: (char *) realloc (path, (size_t) n + 1);
84 else
85 no_new_dcache = 1;
88 /* Set to no_new_dcache only if error indicates that proc doesn't exist. */
89 if (errno != EACCES && errno != ENAMETOOLONG)
90 no_new_dcache = 1;
92 /* Something went wrong. Restore the error number and use the generic
93 version. */
94 __set_errno (save_errno);
96 /* Don't put restrictions on the length of the path unless the user does. */
97 if (size == 0)
99 free (path);
100 path = NULL;
103 result = generic_getcwd (path, size);
105 if (result == NULL && buf == NULL && size != 0)
106 free (path);
108 return result;
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>