1 /* $NetBSD: old_getcwd.c,v 1.2 2002/02/21 07:38:20 itojun Exp $ */
4 * Copyright (c) 1989, 1991, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This file copied from NetBSD's src/lib/libc/gen/getcwd.c 1.15, and edited
37 * to remove namespace stuff and the unused realpath function.
41 #include <sys/cdefs.h>
42 #if defined(LIBC_SCCS) && !defined(lint)
44 static char sccsid
[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95";
46 __RCSID("$NetBSD: old_getcwd.c,v 1.2 2002/02/21 07:38:20 itojun Exp $");
48 #endif /* LIBC_SCCS and not lint */
50 #include <sys/param.h>
64 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
65 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
68 #if defined(__SVR4) || defined(__svr4__)
69 #define d_fileno d_ino
86 size_t ptsize
, upsize
;
92 * If no buffer specified by the user, allocate one as necessary.
93 * If a buffer is specified, the size has to be non-zero. The path
94 * is built from the end of the buffer backwards.
104 if ((pt
= malloc(ptsize
= 1024 - 4)) == NULL
)
112 * Allocate bytes (1024 - malloc space) for the string of "../"'s.
113 * Should always be enough (it's 340 levels). If it's not, allocate
114 * as necessary. Special case the first stat, it's ".", not "..".
116 if ((up
= malloc(upsize
= 1024 - 4)) == NULL
)
118 eup
= up
+ MAXPATHLEN
;
123 /* Save root values, so know when to stop. */
129 errno
= 0; /* XXX readdir has no error return. */
131 for (first
= 1;; first
= 0) {
132 /* Stat the current level. */
136 /* Save current node values. */
140 /* Check for reaching root. */
141 if (root_dev
== dev
&& root_ino
== ino
) {
144 * It's unclear that it's a requirement to copy the
145 * path to the beginning of the buffer, but it's always
146 * been that way and stuff would probably break.
148 memmove(pt
, bpt
, (size_t)(ept
- bpt
));
154 * Build pointer to the parent directory, allocating memory
155 * as necessary. Max length is 3 for "../", the largest
156 * possible component name, plus a trailing NULL.
158 if (bup
+ 3 + MAXNAMLEN
+ 1 >= eup
) {
159 if ((up
= realloc(up
, upsize
*= 2)) == NULL
)
168 /* Open and stat parent directory. */
169 if (!(dir
= opendir(up
)) || fstat(dirfd(dir
), &s
))
172 /* Add trailing slash for next directory. */
176 * If it's a mount point, have to stat each element because
177 * the inode number in the directory is for the entry in the
178 * parent directory, not the inode number of the mounted file.
181 if (s
.st_dev
== dev
) {
183 if (!(dp
= readdir(dir
)))
185 if (dp
->d_fileno
== ino
) {
186 #if defined(__SVR4) || defined(__svr4__)
187 dlen
= strlen(dp
->d_name
);
196 if (!(dp
= readdir(dir
)))
200 #if defined(__SVR4) || defined(__svr4__)
201 dlen
= strlen(dp
->d_name
);
205 memmove(bup
, dp
->d_name
, dlen
+ 1);
207 /* Save the first error for later. */
214 if (s
.st_dev
== dev
&& s
.st_ino
== ino
)
219 * Check for length of the current name, preceding slash,
222 if (bpt
- pt
<= dlen
+ (first
? 1 : 2)) {
231 if ((pt
= realloc(pt
, ptsize
*= 2)) == NULL
)
235 memmove(ept
- len
, bpt
, len
);
241 memmove(bpt
, dp
->d_name
, dlen
);
244 /* Truncate any file name. */
250 * If readdir set errno, use it, not any saved error; otherwise,
251 * didn't find the current directory in its parent directory, set
255 errno
= save_errno
? save_errno
: ENOENT
;