]> git.saurik.com Git - apple/libc.git/blob - gen/getcwd-fbsd.c
Libc-498.1.7.tar.gz
[apple/libc.git] / gen / getcwd-fbsd.c
1 /*
2 * Copyright (c) 1989, 1991, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: src/lib/libc/gen/getcwd.c,v 1.25 2003/10/29 10:45:01 tjr Exp $");
39
40 #include "namespace.h"
41 #include <sys/param.h>
42 #include <sys/stat.h>
43
44 #include <dirent.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include "un-namespace.h"
52
53 #define ISDOT(dp) \
54 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
55 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
56
57 /*
58 * If __getcwd() ever becomes a syscall, we can remove this workaround.
59 * The problem here is that fcntl() assumes a buffer of size MAXPATHLEN,
60 * if size is less than MAXPATHLEN, we need to use a temporary buffer
61 * and see if it fits. We also have to assume that open() or fcntl()
62 * don't fail with errno=ERANGE.
63 */
64 static inline int
65 __getcwd(char *buf, size_t size)
66 {
67 int fd, err, save;
68 struct stat dot, pt;
69 char *b;
70
71 if ((fd = open(".", O_RDONLY)) < 0)
72 return -1;
73 if (fstat(fd, &dot) < 0) {
74 save = errno;
75 close(fd);
76 errno = save;
77 return -1;
78 }
79 /* check that the device and inode are non-zero, otherwise punt */
80 if (dot.st_dev == 0 || dot.st_ino == 0) {
81 close(fd);
82 errno = EINVAL;
83 return -1;
84 }
85 if (size < MAXPATHLEN) {
86 /* the hard case; allocate a buffer of size MAXPATHLEN to use */
87 b = (char *)alloca(MAXPATHLEN);
88 if (b == NULL) {
89 close(fd);
90 errno = ENOMEM; /* make sure it isn't ERANGE */
91 return -1;
92 }
93 } else
94 b = buf;
95
96 err = fcntl(fd, F_GETPATH, b);
97 if (err) {
98 save = errno;
99 close(fd);
100 errno = save;
101 return err;
102 }
103 close(fd);
104 /*
105 * now double-check that the path returned by fcntl() has the same
106 * device and inode number as '.'.
107 */
108 if (stat(b, &pt) < 0)
109 return -1;
110 /*
111 * Since dot.st_dev and dot.st_ino are non-zero, we don't need to
112 * separately test for pt.st_dev and pt.st_ino being non-zero, because
113 * they have to match
114 */
115 if (dot.st_dev != pt.st_dev || dot.st_ino != pt.st_ino) {
116 errno = EINVAL;
117 return -1;
118 }
119 /*
120 * For the case where we allocated a buffer, check that it can fit
121 * in the real buffer, and copy it over.
122 */
123 if (size < MAXPATHLEN) {
124 if (strlen(b) >= size) {
125 errno = ERANGE;
126 return -1;
127 }
128 strcpy(buf, b);
129 }
130 return 0;
131 }
132
133 __private_extern__ char *
134 __private_getcwd(pt, size, usegetpath)
135 char *pt;
136 size_t size;
137 int usegetpath;
138 {
139 struct dirent *dp;
140 DIR *dir = NULL;
141 dev_t dev;
142 ino_t ino;
143 int first;
144 char *bpt, *bup;
145 struct stat s;
146 dev_t root_dev;
147 ino_t root_ino;
148 size_t ptsize, upsize;
149 int save_errno;
150 char *ept, *eup, *up, c;
151
152 /*
153 * If no buffer specified by the user, allocate one as necessary.
154 * If a buffer is specified, the size has to be non-zero. The path
155 * is built from the end of the buffer backwards.
156 */
157 if (pt) {
158 ptsize = 0;
159 if (!size) {
160 errno = EINVAL;
161 return (NULL);
162 }
163 if (size == 1) {
164 errno = ERANGE;
165 return (NULL);
166 }
167 ept = pt + size;
168 } else {
169 if ((pt = malloc(ptsize = MAXPATHLEN)) == NULL)
170 return (NULL);
171 ept = pt + ptsize;
172 }
173 if (usegetpath) {
174 if (__getcwd(pt, ept - pt) == 0) {
175 return (pt);
176 } else if (errno == ERANGE) /* failed because buffer too small */
177 return NULL;
178 }
179 bpt = ept - 1;
180 *bpt = '\0';
181
182 /*
183 * Allocate bytes MAXPATHLEN) for the string of "../"'s.
184 * Should always be enough (it's 340 levels). If it's not, allocate
185 * as necessary. Special case the first stat, it's ".", not "..".
186 */
187 if ((up = malloc(upsize = MAXPATHLEN)) == NULL)
188 goto err;
189 eup = up + MAXPATHLEN;
190 bup = up;
191 up[0] = '.';
192 up[1] = '\0';
193
194 /* Save root values, so know when to stop. */
195 if (stat("/", &s))
196 goto err;
197 root_dev = s.st_dev;
198 root_ino = s.st_ino;
199
200 errno = 0; /* XXX readdir has no error return. */
201
202 for (first = 1;; first = 0) {
203 /* Stat the current level. */
204 if (lstat(up, &s))
205 goto err;
206
207 /* Save current node values. */
208 ino = s.st_ino;
209 dev = s.st_dev;
210
211 /* Check for reaching root. */
212 if (root_dev == dev && root_ino == ino) {
213 *--bpt = '/';
214 /*
215 * It's unclear that it's a requirement to copy the
216 * path to the beginning of the buffer, but it's always
217 * been that way and stuff would probably break.
218 */
219 bcopy(bpt, pt, ept - bpt);
220 free(up);
221 return (pt);
222 }
223
224 /*
225 * Build pointer to the parent directory, allocating memory
226 * as necessary. Max length is 3 for "../", the largest
227 * possible component name, plus a trailing NUL.
228 */
229 if (bup + 3 + MAXNAMLEN + 1 >= eup) {
230 if ((up = reallocf(up, upsize *= 2)) == NULL)
231 goto err;
232 bup = up;
233 eup = up + upsize;
234 }
235 *bup++ = '.';
236 *bup++ = '.';
237 *bup = '\0';
238
239 /* Open and stat parent directory. */
240 if (!(dir = opendir(up)) || _fstat(dirfd(dir), &s))
241 goto err;
242
243 /* Add trailing slash for next directory. */
244 *bup++ = '/';
245 *bup = '\0';
246
247 /*
248 * If it's a mount point, have to stat each element because
249 * the inode number in the directory is for the entry in the
250 * parent directory, not the inode number of the mounted file.
251 */
252 save_errno = 0;
253 if (s.st_dev == dev) {
254 for (;;) {
255 if (!(dp = readdir(dir)))
256 goto notfound;
257 if (dp->d_fileno == ino)
258 break;
259 }
260 } else
261 for (;;) {
262 if (!(dp = readdir(dir)))
263 goto notfound;
264 if (ISDOT(dp))
265 continue;
266 bcopy(dp->d_name, bup, dp->d_namlen + 1);
267
268 /* Save the first error for later. */
269 if (lstat(up, &s)) {
270 if (!save_errno)
271 save_errno = errno;
272 errno = 0;
273 continue;
274 }
275 if (s.st_dev == dev && s.st_ino == ino)
276 break;
277 }
278
279 /*
280 * Check for length of the current name, preceding slash,
281 * leading slash.
282 */
283 if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
284 size_t len, off;
285
286 if (!ptsize) {
287 errno = ERANGE;
288 goto err;
289 }
290 off = bpt - pt;
291 len = ept - bpt;
292 if ((pt = reallocf(pt, ptsize *= 2)) == NULL)
293 goto err;
294 bpt = pt + off;
295 ept = pt + ptsize;
296 bcopy(bpt, ept - len, len);
297 bpt = ept - len;
298 }
299 if (!first)
300 *--bpt = '/';
301 bpt -= dp->d_namlen;
302 bcopy(dp->d_name, bpt, dp->d_namlen);
303 (void) closedir(dir);
304 dir = NULL;
305
306 /* Truncate any file name. */
307 *bup = '\0';
308 }
309
310 notfound:
311 /*
312 * If readdir set errno, use it, not any saved error; otherwise,
313 * didn't find the current directory in its parent directory, set
314 * errno to ENOENT.
315 */
316 if (!errno)
317 errno = save_errno ? save_errno : ENOENT;
318 /* FALLTHROUGH */
319 err:
320 save_errno = errno;
321
322 if (ptsize)
323 free(pt);
324 if (dir)
325 (void) closedir(dir);
326 free(up);
327
328 errno = save_errno;
329 return (NULL);
330 }
331
332 char *
333 getcwd(pt, size)
334 char *pt;
335 size_t size;
336 {
337 return __private_getcwd(pt, size, 1);
338 }