malloc/Makefile: Split and sort tests
[glibc.git] / sysdeps / unix / sysv / linux / getcwd.c
blobd05b83a1f6b5bc2cbc153d59a41a8ae6b2f79243
1 /* Determine current working directory. Linux version.
2 Copyright (C) 1997-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/param.h>
26 #include <sysdep.h>
27 #include <sys/syscall.h>
30 /* If we compile the file for use in ld.so we don't need the feature
31 that getcwd() allocates the buffers itself. */
32 #if IS_IN (rtld)
33 # define NO_ALLOCATION 1
34 #endif
37 /* The "proc" filesystem provides an easy method to retrieve the value.
38 For each process, the corresponding directory contains a symbolic link
39 named `cwd'. Reading the content of this link immediate gives us the
40 information. But we have to take care for systems which do not have
41 the proc filesystem mounted. Use the POSIX implementation in this case. */
43 /* Get the code for the generic version. */
44 #define GETCWD_RETURN_TYPE static char *
45 #include <sysdeps/posix/getcwd.c>
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 && path[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 either cannot handle paths longer than a page
98 or can succeed without returning an absolute path. Just use the
99 generic implementation right away. */
100 if (retval >= 0 || errno == ENAMETOOLONG)
102 #ifndef NO_ALLOCATION
103 if (buf == NULL && size == 0)
105 free (path);
106 path = NULL;
108 #endif
110 result = __getcwd_generic (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 libc_hidden_def (__getcwd)
133 weak_alias (__getcwd, getcwd)