2 * Copyright (c) 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #if !defined(HAVE_GETCWD)
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char rcsid[] = "$OpenBSD: getcwd.c,v 1.6 2000/07/19 15:25:13 deraadt Exp $";
33 #endif /* LIBC_SCCS and not lint */
35 #include <sys/param.h>
46 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
47 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
50 getcwd(char *pt,size_t size)
52 register struct dirent *dp;
53 register DIR *dir = NULL;
57 register char *bpt, *bup;
61 size_t ptsize, upsize;
66 * If no buffer specified by the user, allocate one as necessary.
67 * If a buffer is specified, the size has to be non-zero. The path
68 * is built from the end of the buffer backwards.
78 if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
86 * Allocate bytes (1024 - malloc space) for the string of "../"'s.
87 * Should always be enough (it's 340 levels). If it's not, allocate
88 * as necessary. Special * case the first stat, it's ".", not "..".
90 if ((up = malloc(upsize = 1024 - 4)) == NULL)
92 eup = up + MAXPATHLEN;
97 /* Save root values, so know when to stop. */
103 errno = 0; /* XXX readdir has no error return. */
105 for (first = 1;; first = 0) {
106 /* Stat the current level. */
110 /* Save current node values. */
114 /* Check for reaching root. */
115 if (root_dev == dev && root_ino == ino) {
118 * It's unclear that it's a requirement to copy the
119 * path to the beginning of the buffer, but it's always
120 * been that way and stuff would probably break.
122 memmove(bpt, pt, ept - bpt);
128 * Build pointer to the parent directory, allocating memory
129 * as necessary. Max length is 3 for "../", the largest
130 * possible component name, plus a trailing NULL.
132 if (bup + 3 + MAXNAMLEN + 1 >= eup) {
135 if ((nup = realloc(up, upsize *= 2)) == NULL)
145 /* Open and stat parent directory.
146 * RACE?? - replaced fstat(dirfd(dir), &s) w/ lstat(up,&s)
148 if (!(dir = opendir(up)) || lstat(up,&s))
151 /* Add trailing slash for next directory. */
155 * If it's a mount point, have to stat each element because
156 * the inode number in the directory is for the entry in the
157 * parent directory, not the inode number of the mounted file.
160 if (s.st_dev == dev) {
162 if (!(dp = readdir(dir)))
164 if (dp->d_fileno == ino)
169 if (!(dp = readdir(dir)))
173 memmove(dp->d_name, bup, dp->d_namlen + 1);
175 /* Save the first error for later. */
182 if (s.st_dev == dev && s.st_ino == ino)
187 * Check for length of the current name, preceding slash,
190 if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
200 if ((npt = realloc(pt, ptsize *= 2)) == NULL)
205 memmove(bpt, ept - len, len);
211 memmove(dp->d_name, bpt, dp->d_namlen);
214 /* Truncate any file name. */
220 * If readdir set errno, use it, not any saved error; otherwise,
221 * didn't find the current directory in its parent directory, set
225 errno = save_errno ? save_errno : ENOENT;
237 #endif /* !defined(HAVE_GETCWD) */