1 /* $OpenBSD: getcwd.c,v 1.14 2005/08/08 08:05:34 espie Exp $ */
3 * Copyright (c) 1989, 1991, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 /* OPENBSD ORIGINAL: lib/libc/gen/getcwd.c */
35 #if !defined(HAVE_GETCWD)
37 #include <sys/param.h>
48 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
49 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
52 getcwd(char *pt
, size_t size
)
63 size_t ptsize
, upsize
;
68 * If no buffer specified by the user, allocate one as necessary.
69 * If a buffer is specified, the size has to be non-zero. The path
70 * is built from the end of the buffer backwards.
80 if ((pt
= malloc(ptsize
= MAXPATHLEN
)) == NULL
)
88 * Allocate bytes for the string of "../"'s.
89 * Should always be enough (it's 340 levels). If it's not, allocate
90 * as necessary. Special * case the first stat, it's ".", not "..".
92 if ((up
= malloc(upsize
= MAXPATHLEN
)) == NULL
)
99 /* Save root values, so know when to stop. */
105 errno
= 0; /* XXX readdir has no error return. */
107 for (first
= 1;; first
= 0) {
108 /* Stat the current level. */
112 /* Save current node values. */
116 /* Check for reaching root. */
117 if (root_dev
== dev
&& root_ino
== ino
) {
120 * It's unclear that it's a requirement to copy the
121 * path to the beginning of the buffer, but it's always
122 * been that way and stuff would probably break.
124 memmove(pt
, bpt
, ept
- bpt
);
130 * Build pointer to the parent directory, allocating memory
131 * as necessary. Max length is 3 for "../", the largest
132 * possible component name, plus a trailing NUL.
134 if (bup
+ 3 + MAXNAMLEN
+ 1 >= eup
) {
137 if ((nup
= realloc(up
, upsize
*= 2)) == NULL
)
139 bup
= nup
+ (bup
- up
);
147 /* Open and stat parent directory. */
148 if (!(dir
= opendir(up
)) || fstat(dirfd(dir
), &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 memcpy(bup
, dp
->d_name
, 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)) {
199 if ((npt
= realloc(pt
, ptsize
*= 2)) == NULL
)
201 bpt
= npt
+ (bpt
- pt
);
204 memmove(ept
- len
, bpt
, len
);
210 memcpy(bpt
, dp
->d_name
, dp
->d_namlen
);
213 /* Truncate any file name. */
219 * If readdir set errno, use it, not any saved error; otherwise,
220 * didn't find the current directory in its parent directory, set
224 errno
= save_errno
? save_errno
: ENOENT
;
240 #endif /* !defined(HAVE_GETCWD) */