getcwd.c revision 1.5.4.2 1 /* $NetBSD: getcwd.c,v 1.5.4.2 1996/09/19 20:02:41 jtc Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #if defined(LIBC_SCCS) && !defined(lint)
37 #if 0
38 static char sccsid[] = "@(#)getcwd.c 8.1 (Berkeley) 6/4/93";
39 #else
40 static char rcsid[] = "$NetBSD: getcwd.c,v 1.5.4.2 1996/09/19 20:02:41 jtc Exp $";
41 #endif
42 #endif /* LIBC_SCCS and not lint */
43
44 #include "namespace.h"
45 #include <sys/param.h>
46 #include <sys/stat.h>
47
48 #include <errno.h>
49 #include <dirent.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #ifdef __weak_alias
56 __weak_alias(getcwd,_getcwd);
57 #endif
58
59 #define ISDOT(dp) \
60 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
61 dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
62
63 char *
64 getcwd(pt, size)
65 char *pt;
66 size_t size;
67 {
68 register struct dirent *dp;
69 register DIR *dir;
70 register dev_t dev;
71 register ino_t ino;
72 register int first;
73 register char *bpt, *bup;
74 struct stat s;
75 dev_t root_dev;
76 ino_t root_ino;
77 size_t ptsize, upsize;
78 int save_errno;
79 char *ept, *eup, *up;
80
81 /*
82 * If no buffer specified by the user, allocate one as necessary.
83 * If a buffer is specified, the size has to be non-zero. The path
84 * is built from the end of the buffer backwards.
85 */
86 if (pt) {
87 ptsize = 0;
88 if (!size) {
89 errno = EINVAL;
90 return (NULL);
91 }
92 ept = pt + size;
93 } else {
94 if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
95 return (NULL);
96 ept = pt + ptsize;
97 }
98 bpt = ept - 1;
99 *bpt = '\0';
100
101 /*
102 * Allocate bytes (1024 - malloc space) for the string of "../"'s.
103 * Should always be enough (it's 340 levels). If it's not, allocate
104 * as necessary. Special * case the first stat, it's ".", not "..".
105 */
106 if ((up = malloc(upsize = 1024 - 4)) == NULL)
107 goto err;
108 eup = up + MAXPATHLEN;
109 bup = up;
110 up[0] = '.';
111 up[1] = '\0';
112
113 /* Save root values, so know when to stop. */
114 if (stat("/", &s))
115 goto err;
116 root_dev = s.st_dev;
117 root_ino = s.st_ino;
118
119 errno = 0; /* XXX readdir has no error return. */
120
121 for (first = 1;; first = 0) {
122 /* Stat the current level. */
123 if (lstat(up, &s))
124 goto err;
125
126 /* Save current node values. */
127 ino = s.st_ino;
128 dev = s.st_dev;
129
130 /* Check for reaching root. */
131 if (root_dev == dev && root_ino == ino) {
132 *--bpt = '/';
133 /*
134 * It's unclear that it's a requirement to copy the
135 * path to the beginning of the buffer, but it's always
136 * been that way and stuff would probably break.
137 */
138 bcopy(bpt, pt, ept - bpt);
139 free(up);
140 return (pt);
141 }
142
143 /*
144 * Build pointer to the parent directory, allocating memory
145 * as necessary. Max length is 3 for "../", the largest
146 * possible component name, plus a trailing NULL.
147 */
148 if (bup + 3 + MAXNAMLEN + 1 >= eup) {
149 if ((up = realloc(up, upsize *= 2)) == NULL)
150 goto err;
151 bup = up;
152 eup = up + upsize;
153 }
154 *bup++ = '.';
155 *bup++ = '.';
156 *bup = '\0';
157
158 /* Open and stat parent directory. */
159 if (!(dir = opendir(up)) || fstat(dirfd(dir), &s))
160 goto err;
161
162 /* Add trailing slash for next directory. */
163 *bup++ = '/';
164
165 /*
166 * If it's a mount point, have to stat each element because
167 * the inode number in the directory is for the entry in the
168 * parent directory, not the inode number of the mounted file.
169 */
170 save_errno = 0;
171 if (s.st_dev == dev) {
172 for (;;) {
173 if (!(dp = readdir(dir)))
174 goto notfound;
175 if (dp->d_fileno == ino)
176 break;
177 }
178 } else
179 for (;;) {
180 if (!(dp = readdir(dir)))
181 goto notfound;
182 if (ISDOT(dp))
183 continue;
184 bcopy(dp->d_name, bup, dp->d_namlen + 1);
185
186 /* Save the first error for later. */
187 if (lstat(up, &s)) {
188 if (!save_errno)
189 save_errno = errno;
190 errno = 0;
191 continue;
192 }
193 if (s.st_dev == dev && s.st_ino == ino)
194 break;
195 }
196
197 /*
198 * Check for length of the current name, preceding slash,
199 * leading slash.
200 */
201 if (bpt - pt <= dp->d_namlen + (first ? 1 : 2)) {
202 size_t len, off;
203
204 if (!ptsize) {
205 errno = ERANGE;
206 goto err;
207 }
208 off = bpt - pt;
209 len = ept - bpt;
210 if ((pt = realloc(pt, ptsize *= 2)) == NULL)
211 goto err;
212 bpt = pt + off;
213 ept = pt + ptsize;
214 bcopy(bpt, ept - len, len);
215 bpt = ept - len;
216 }
217 if (!first)
218 *--bpt = '/';
219 bpt -= dp->d_namlen;
220 bcopy(dp->d_name, bpt, dp->d_namlen);
221 (void)closedir(dir);
222
223 /* Truncate any file name. */
224 *bup = '\0';
225 }
226
227 notfound:
228 /*
229 * If readdir set errno, use it, not any saved error; otherwise,
230 * didn't find the current directory in its parent directory, set
231 * errno to ENOENT.
232 */
233 if (!errno)
234 errno = save_errno ? save_errno : ENOENT;
235 /* FALLTHROUGH */
236 err:
237 if (ptsize)
238 free(pt);
239 free(up);
240 return (NULL);
241 }
242