Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / unix / sysv / linux / getcwd.c
blobccf3bf25c6cc8d68bde5b9248a2898b36d5be3a8
1 /* Determine current working directory. Linux version.
2 Copyright (C) 1997-2014 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, see
18 <http://www.gnu.org/licenses/>. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <sys/param.h>
27 #include <sysdep.h>
28 #include <sys/syscall.h>
30 #include <kernel-features.h>
33 /* If we compile the file for use in ld.so we don't need the feature
34 that getcwd() allocates the buffers itself. */
35 #ifdef IS_IN_rtld
36 # define NO_ALLOCATION 1
37 #endif
40 /* The "proc" filesystem provides an easy method to retrieve the value.
41 For each process, the corresponding directory contains a symbolic link
42 named `cwd'. Reading the content of this link immediate gives us the
43 information. But we have to take care for systems which do not have
44 the proc filesystem mounted. Use the POSIX implementation in this case. */
45 static char *generic_getcwd (char *buf, size_t size) internal_function;
47 char *
48 __getcwd (char *buf, size_t size)
50 char *path;
51 char *result;
53 #ifndef NO_ALLOCATION
54 size_t alloc_size = size;
55 if (size == 0)
57 if (buf != NULL)
59 __set_errno (EINVAL);
60 return NULL;
63 alloc_size = MAX (PATH_MAX, __getpagesize ());
66 if (buf == NULL)
68 path = malloc (alloc_size);
69 if (path == NULL)
70 return NULL;
72 else
73 #else
74 # define alloc_size size
75 #endif
76 path = buf;
78 int retval;
80 retval = INLINE_SYSCALL (getcwd, 2, path, alloc_size);
81 if (retval >= 0)
83 #ifndef NO_ALLOCATION
84 if (buf == NULL && size == 0)
85 /* Ensure that the buffer is only as large as necessary. */
86 buf = realloc (path, (size_t) retval);
88 if (buf == NULL)
89 /* Either buf was NULL all along, or `realloc' failed but
90 we still have the original string. */
91 buf = path;
92 #endif
94 return buf;
97 /* The system call cannot handle paths longer than a page.
98 Neither can the magic symlink in /proc/self. Just use the
99 generic implementation right away. */
100 if (errno == ENAMETOOLONG)
102 #ifndef NO_ALLOCATION
103 if (buf == NULL && size == 0)
105 free (path);
106 path = NULL;
108 #endif
110 result = generic_getcwd (path, size);
112 #ifndef NO_ALLOCATION
113 if (result == NULL && buf == NULL && size != 0)
114 free (path);
115 #endif
117 return result;
120 /* It should never happen that the `getcwd' syscall failed because
121 the buffer is too small if we allocated the buffer ourselves
122 large enough. */
123 assert (errno != ERANGE || buf != NULL || size != 0);
125 #ifndef NO_ALLOCATION
126 if (buf == NULL)
127 free (path);
128 #endif
130 return NULL;
132 weak_alias (__getcwd, getcwd)
134 /* Get the code for the generic version. */
135 #define GETCWD_RETURN_TYPE static char * internal_function
136 #define __getcwd generic_getcwd
137 #include <sysdeps/posix/getcwd.c>