1 /* OPENBSD ORIGINAL: lib/libc/gen/getcwd.c */
4 * Copyright (c) 1989, 1991, 1993
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
34 #if !defined(HAVE_GETCWD)
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char rcsid[] = "$OpenBSD: getcwd.c,v 1.9 2003/06/11 21:03:10 deraadt Exp $";
38 #endif /* LIBC_SCCS and not lint */
40 #include <sys/param.h>
51 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
52 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
55 getcwd(char *pt, size_t size)
57 register struct dirent *dp;
58 register DIR *dir = NULL;
62 register char *bpt, *bup;
66 size_t ptsize, upsize;
71 * If no buffer specified by the user, allocate one as necessary.
72 * If a buffer is specified, the size has to be non-zero. The path
73 * is built from the end of the buffer backwards.
83 if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
91 * Allocate bytes (1024 - malloc space) for the string of "../"'s.
92 * Should always be enough (it's 340 levels). If it's not, allocate
93 * as necessary. Special * case the first stat, it's ".", not "..".
95 if ((up = malloc(upsize = 1024 - 4)) == NULL)
97 eup = up + MAXPATHLEN;
102 /* Save root values, so know when to stop. */
108 errno = 0; /* XXX readdir has no error return. */
110 for (first = 1;; first = 0) {
111 /* Stat the current level. */
115 /* Save current node values. */
119 /* Check for reaching root. */
120 if (root_dev == dev && root_ino == ino) {
123 * It's unclear that it's a requirement to copy the
124 * path to the beginning of the buffer, but it's always
125 * been that way and stuff would probably break.
127 memmove(pt, bpt, ept - bpt);
133 * Build pointer to the parent directory, allocating memory
134 * as necessary. Max length is 3 for "../", the largest
135 * possible component name, plus a trailing NUL.
137 if (bup + 3 + MAXNAMLEN + 1 >= eup) {
140 if ((nup = realloc(up, upsize *= 2)) == NULL)
150 /* Open and stat parent directory.
151 * RACE?? - replaced fstat(dirfd(dir), &s) w/ lstat(up,&s)
153 if (!(dir = opendir(up)) || lstat(up,&s))
156 /* Add trailing slash for next directory. */
160 * If it's a mount point, have to stat each element because
161 * the inode number in the directory is for the entry in the
162 * parent directory, not the inode number of the mounted file.
165 if (s.st_dev == dev) {
167 if (!(dp = readdir(dir)))
169 if (dp->d_fileno == ino)
174 if (!(dp = readdir(dir)))
178 memmove(bup, dp->d_name, dp->d_namlen + 1);
180 /* Save the first error for later. */
187 if (s.st_dev == dev && s.st_ino == ino)
192 * Check for length of the current name, preceding slash,
195 if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
205 if ((npt = realloc(pt, ptsize *= 2)) == NULL)
210 memmove(ept - len, bpt, len);
216 memmove(bpt, dp->d_name, dp->d_namlen);
219 /* Truncate any file name. */
225 * If readdir set errno, use it, not any saved error; otherwise,
226 * didn't find the current directory in its parent directory, set
230 errno = save_errno ? save_errno : ENOENT;
242 #endif /* !defined(HAVE_GETCWD) */