2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1991, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95";
34 #endif /* LIBC_SCCS and not lint */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "namespace.h"
39 #include <sys/param.h>
49 #include "un-namespace.h"
51 #include "gen-private.h"
54 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
55 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
57 extern int __getcwd(char *, size_t);
60 getcwd(char *pt, size_t size)
77 * If no buffer specified by the user, allocate one as necessary.
78 * If a buffer is specified, the size has to be non-zero. The path
79 * is built from the end of the buffer backwards.
93 if ((pt = malloc(ptsize = PATH_MAX)) == NULL)
97 if (__getcwd(pt, ept - pt) == 0) {
100 ept = pt + strlen(pt) - 1;
112 /* Save root values, so know when to stop. */
118 errno = 0; /* XXX readdir has no error return. */
120 for (first = 1;; first = 0) {
121 /* Stat the current level. */
122 if (dir != NULL ? _fstat(_dirfd(dir), &s) : lstat(".", &s))
125 /* Save current node values. */
129 /* Check for reaching root. */
130 if (root_dev == dev && root_ino == ino) {
133 * It's unclear that it's a requirement to copy the
134 * path to the beginning of the buffer, but it's always
135 * been that way and stuff would probably break.
137 bcopy(bpt, pt, ept - bpt);
139 (void) closedir(dir);
143 /* Open and stat parent directory. */
144 fd = _openat(dir != NULL ? _dirfd(dir) : AT_FDCWD,
145 "..", O_RDONLY | O_CLOEXEC);
149 (void) closedir(dir);
150 if (!(dir = fdopendir(fd)) || _fstat(_dirfd(dir), &s)) {
156 * If it's a mount point, have to stat each element because
157 * the inode number in the directory is for the entry in the
158 * parent directory, not the inode number of the mounted file.
161 if (s.st_dev == dev) {
163 if (!(dp = readdir(dir)))
165 if (dp->d_fileno == ino)
170 if (!(dp = readdir(dir)))
175 /* Save the first error for later. */
176 if (fstatat(_dirfd(dir), dp->d_name, &s,
177 AT_SYMLINK_NOFOLLOW)) {
183 if (s.st_dev == dev && s.st_ino == ino)
188 * Check for length of the current name, preceding slash,
191 while (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
200 if ((pt = reallocf(pt, ptsize *= 2)) == NULL)
204 bcopy(bpt, ept - len, len);
210 bcopy(dp->d_name, bpt, dp->d_namlen);
215 * If readdir set errno, use it, not any saved error; otherwise,
216 * didn't find the current directory in its parent directory, set
220 errno = save_errno ? save_errno : ENOENT;
228 (void) closedir(dir);