1/* Determine current working directory. Linux version.
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
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.
10
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.
15
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/>. */
19
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>
26
27#include <sysdep.h>
28#include <sys/syscall.h>
29
30
31/* If we compile the file for use in ld.so we don't need the feature
32 that getcwd() allocates the buffers itself. */
33#if IS_IN (rtld)
34# define NO_ALLOCATION 1
35#endif
36
37
38/* The "proc" filesystem provides an easy method to retrieve the value.
39 For each process, the corresponding directory contains a symbolic link
40 named `cwd'. Reading the content of this link immediate gives us the
41 information. But we have to take care for systems which do not have
42 the proc filesystem mounted. Use the POSIX implementation in this case. */
43static char *generic_getcwd (char *buf, size_t size);
44
45char *
46__getcwd (char *buf, size_t size)
47{
48 char *path;
49 char *result;
50
51#ifndef NO_ALLOCATION
52 size_t alloc_size = size;
53 if (size == 0)
54 {
55 if (buf != NULL)
56 {
57 __set_errno (EINVAL);
58 return NULL;
59 }
60
61 alloc_size = MAX (PATH_MAX, __getpagesize ());
62 }
63
64 if (buf == NULL)
65 {
66 path = malloc (alloc_size);
67 if (path == NULL)
68 return NULL;
69 }
70 else
71#else
72# define alloc_size size
73#endif
74 path = buf;
75
76 int retval;
77
78 retval = INLINE_SYSCALL (getcwd, 2, path, alloc_size);
79 if (retval > 0 && path[0] == '/')
80 {
81#ifndef NO_ALLOCATION
82 if (buf == NULL && size == 0)
83 /* Ensure that the buffer is only as large as necessary. */
84 buf = realloc (path, (size_t) retval);
85
86 if (buf == NULL)
87 /* Either buf was NULL all along, or `realloc' failed but
88 we still have the original string. */
89 buf = path;
90#endif
91
92 return buf;
93 }
94
95 /* The system call either cannot handle paths longer than a page
96 or can succeed without returning an absolute path. Just use the
97 generic implementation right away. */
98 if (retval >= 0 || errno == ENAMETOOLONG)
99 {
100#ifndef NO_ALLOCATION
101 if (buf == NULL && size == 0)
102 {
103 free (path);
104 path = NULL;
105 }
106#endif
107
108 result = generic_getcwd (path, size);
109
110#ifndef NO_ALLOCATION
111 if (result == NULL && buf == NULL && size != 0)
112 free (path);
113#endif
114
115 return result;
116 }
117
118 /* It should never happen that the `getcwd' syscall failed because
119 the buffer is too small if we allocated the buffer ourselves
120 large enough. */
121 assert (errno != ERANGE || buf != NULL || size != 0);
122
123#ifndef NO_ALLOCATION
124 if (buf == NULL)
125 free (path);
126#endif
127
128 return NULL;
129}
130weak_alias (__getcwd, getcwd)
131
132/* Get the code for the generic version. */
133#define GETCWD_RETURN_TYPE static char *
134#define __getcwd generic_getcwd
135#include <sysdeps/posix/getcwd.c>
136